Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"type": "phpcodesniffer-standard",
"require": {
"php": "^7.2",
"squizlabs/php_codesniffer": "^3.5",
"nette/utils": "^3.0",
"slevomat/coding-standard": "^6.0"
"squizlabs/php_codesniffer": "^3.5"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
Expand Down
3 changes: 1 addition & 2 deletions src/ObjectCalisthenics/Helper/NamingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ObjectCalisthenics\Helper;

use Nette\Utils\Strings;
use PHP_CodeSniffer\Files\File;

final class NamingHelper
Expand Down Expand Up @@ -40,7 +39,7 @@ public static function getElementName(File $file, int $position): string
{
$name = $file->getTokens()[$position]['content'];

if (Strings::startsWith($name, '$')) {
if (strncmp($name, '$', strlen('$')) === 0) {
return trim($name, '$');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ObjectCalisthenics\Sniffs\Classes;

use Nette\Utils\Strings;
use ObjectCalisthenics\Helper\PropertyHelper;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
Expand Down Expand Up @@ -50,7 +49,7 @@ private function isSniffClass(File $file, int $position): bool

$classNameToken = $file->getTokens()[$classNameTokenPosition];

return Strings::endsWith($classNameToken['content'], 'Sniff');
return substr($classNameToken['content'], -strlen('Sniff')) === 'Sniff';
}

/**
Expand Down
143 changes: 137 additions & 6 deletions src/ObjectCalisthenics/Sniffs/NamingConventions/NoSetterSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace ObjectCalisthenics\Sniffs\NamingConventions;

use Nette\Utils\Strings;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\ClassHelper;

final class NoSetterSniff implements Sniff
{
Expand All @@ -20,11 +18,44 @@ final class NoSetterSniff implements Sniff
*/
private const SETTER_REGEX = '#^set[A-Z0-9]#';

/**
* @var string
*/
public const NAMESPACE_SEPARATOR = '\\';

/**
* @var string[]
*/
public $allowedClasses = [];

/**
* @var (int|string)[]
*/
public static $nameTokenCodes = [
T_NS_SEPARATOR,
T_STRING,
];

/**
* @var (int|string)[]
*/
public static $ineffectiveTokenCodes = [
T_WHITESPACE,
T_COMMENT,
T_DOC_COMMENT,
T_DOC_COMMENT_OPEN_TAG,
T_DOC_COMMENT_CLOSE_TAG,
T_DOC_COMMENT_STAR,
T_DOC_COMMENT_STRING,
T_DOC_COMMENT_TAG,
T_DOC_COMMENT_WHITESPACE,
T_PHPCS_DISABLE,
T_PHPCS_ENABLE,
T_PHPCS_IGNORE,
T_PHPCS_IGNORE_FILE,
T_PHPCS_SET,
];

/**
* @return int[]
*/
Expand All @@ -48,14 +79,14 @@ public function process(File $file, $position): void
return;
}

if (! (bool) Strings::match($methodName, self::SETTER_REGEX)) {
if (! (bool) preg_match(self::SETTER_REGEX, $methodName)) {
return;
}

$file->addError(self::ERROR_MESSAGE, $position, self::class);
}

private function getClassName(File $file): string
private function getClassName(File $file)
{
$classTokenPosition = $file->findNext(T_CLASS, 0);

Expand All @@ -64,12 +95,12 @@ private function getClassName(File $file): string
return 'anonymous';
}

$className = ClassHelper::getFullyQualifiedName($file, $classTokenPosition);
$className = $this->getFullyQualifiedName($file, $classTokenPosition);

return ltrim($className, '\\');
}

private function shouldSkip(string $methodName, string $className): bool
private function shouldSkip(string $methodName, string $className)
{
// not really a setter, but usually test "setup" method
if ($methodName === 'setUp') {
Expand All @@ -84,4 +115,104 @@ private function shouldSkip(string $methodName, string $className): bool

return false;
}

private function getFullyQualifiedName(File $phpcsFile, int $classPointer)
{
$className = $this->getName($phpcsFile, $classPointer);

$tokens = $phpcsFile->getTokens();
if ($tokens[$classPointer]['code'] === T_ANON_CLASS) {
return $className;
}

$name = sprintf('%s%s', self::NAMESPACE_SEPARATOR, $className);
$namespace = $this->findCurrentNamespaceName($phpcsFile, $classPointer);
return $namespace !== null ? sprintf('%s%s%s', self::NAMESPACE_SEPARATOR, $namespace, $name) : $name;
}

private function getName(File $phpcsFile, int $classPointer): string
{
$tokens = $phpcsFile->getTokens();

if ($tokens[$classPointer]['code'] === T_ANON_CLASS) {
return 'class@anonymous';
}

return $tokens[$this->findNext(
$phpcsFile,
T_STRING,
$classPointer + 1,
$tokens[$classPointer]['scope_opener']
)]['content'];
}

private function findCurrentNamespaceName(File $phpcsFile, int $anyPointer)
{
$namespacePointer = $this->findPrevious($phpcsFile, T_NAMESPACE, $anyPointer);
if ($namespacePointer === null) {
return null;
}

/** @var int $namespaceNameStartPointer */
$namespaceNameStartPointer = $this->findNextEffective(
$phpcsFile,
$namespacePointer + 1
);

$namespaceNameEndPointer = $this->findNextExcluding(
$phpcsFile,
self::$nameTokenCodes,
$namespaceNameStartPointer + 1
) - 1;

return $this->getContent($phpcsFile, $namespaceNameStartPointer, $namespaceNameEndPointer);
}

private function findNext(File $phpcsFile, $types, int $startPointer, ?int $endPointer = null): ?int
{
/** @var int|false $token */
$token = $phpcsFile->findNext($types, $startPointer, $endPointer, false);
return $token === false ? null : $token;
}

private function findPrevious(File $phpcsFile, $types, int $startPointer, ?int $endPointer = null): ?int
{
/** @var int|false $token */
$token = $phpcsFile->findPrevious($types, $startPointer, $endPointer, false);
return $token === false ? null : $token;
}

private function findNextEffective(File $phpcsFile, int $startPointer, ?int $endPointer = null)
{
return self::findNextExcluding($phpcsFile, self::$ineffectiveTokenCodes, $startPointer, $endPointer);
}

private function findNextExcluding(File $phpcsFile, $types, int $startPointer, ?int $endPointer = null)
{
/** @var int|false $token */
$token = $phpcsFile->findNext($types, $startPointer, $endPointer, true);
return $token === false ? null : $token;
}

private function getContent(File $phpcsFile, int $startPointer, ?int $endPointer = null): string
{
$tokens = $phpcsFile->getTokens();
$endPointer = $endPointer ?? $this->getLastTokenPointer($phpcsFile);

$content = '';
for ($i = $startPointer; $i <= $endPointer; ++$i) {
$content .= $tokens[$i]['content'];
}

return $content;
}

private function getLastTokenPointer(File $phpcsFile): int
{
$tokenCount = count($phpcsFile->getTokens());
if ($tokenCount === 0) {
throw new Exception($phpcsFile->getFilename());
}
return $tokenCount - 1;
}
}
9 changes: 7 additions & 2 deletions tests/FileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ObjectCalisthenics\Tests;

use Nette\Utils\FileSystem;
use Exception;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Ruleset;
Expand Down Expand Up @@ -31,7 +31,12 @@ public function createFile(string $filePath): File
$ruleset = new Ruleset($config);

$file = new File($filePath, $ruleset, $config);
$fileContent = FileSystem::read($filePath);

$fileContent = @file_get_contents($file);
if ($fileContent === false) {
throw new Exception("Unable to read file '$file'. ");
}

$file->setContent($fileContent);
$file->parse();

Expand Down