From bb59c3d316437cfc594d51dd52c38e85b3dea78d Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 4 Apr 2020 18:48:06 +0200 Subject: [PATCH] Wip : Use Slevomat --- SymfonyCustom/Helpers/SniffHelper.php | 55 +- .../Sniffs/Arrays/ArrayDeclarationSniff.php | 26 +- .../Sniffs/Classes/ClassDeclarationSniff.php | 52 -- .../DocCommentForbiddenTagsSniff.php | 46 -- .../Formatting/BlankLineBeforeReturnSniff.php | 90 ---- .../Formatting/StrictComparisonSniff.php | 54 -- .../Sniffs/Formatting/YodaConditionSniff.php | 98 ---- .../AlphabeticallySortedUseSniff.php | 165 ------ .../Sniffs/Namespaces/UnusedUseSniff.php | 500 ------------------ .../WhiteSpace/CloseBracketSpacingSniff.php | 52 -- .../WhiteSpace/OpenBracketSpacingSniff.php | 51 -- .../WhiteSpace/UnaryOperatorSpacingSniff.php | 68 --- .../Classes/ClassDeclarationUnitTest.inc | 17 - .../ClassDeclarationUnitTest.inc.fixed | 14 - .../Classes/ClassDeclarationUnitTest.php | 32 -- .../DocCommentForbiddenTagsUnitTest.inc | 22 - .../DocCommentForbiddenTagsUnitTest.php | 36 -- .../BlankLineBeforeReturnUnitTest.inc | 85 --- .../BlankLineBeforeReturnUnitTest.inc.fixed | 88 --- .../BlankLineBeforeReturnUnitTest.php | 33 -- .../ConditionalReturnOrThrowUnitTest.inc | 1 - .../Formatting/StrictComparisonUnitTest.inc | 9 - .../Formatting/StrictComparisonUnitTest.php | 32 -- .../Formatting/YodaConditionUnitTest.inc | 100 ---- .../Formatting/YodaConditionUnitTest.php | 43 -- .../AlphabeticallySortedUseUnitTest.inc | 92 ---- .../AlphabeticallySortedUseUnitTest.php | 33 -- .../Tests/Namespaces/UnusedUseUnitTest.inc | 152 ------ .../Namespaces/UnusedUseUnitTest.inc.fixed | 139 ----- .../Tests/Namespaces/UnusedUseUnitTest.php | 48 -- .../CloseBracketSpacingUnitTest.inc | 9 - .../CloseBracketSpacingUnitTest.inc.fixed | 9 - .../CloseBracketSpacingUnitTest.php | 34 -- .../WhiteSpace/OpenBracketSpacingUnitTest.inc | 9 - .../OpenBracketSpacingUnitTest.inc.fixed | 9 - .../WhiteSpace/OpenBracketSpacingUnitTest.php | 34 -- .../UnaryOperatorSpacingUnitTest.inc | 10 - .../UnaryOperatorSpacingUnitTest.inc.fixed | 10 - .../UnaryOperatorSpacingUnitTest.php | 34 -- SymfonyCustom/ruleset.xml | 419 +++++++++++++-- TwigCS/src/Report/SniffViolation.php | 2 +- TwigCS/src/Report/TextFormatter.php | 2 +- TwigCS/src/Sniff/AbstractSpacingSniff.php | 2 +- TwigCS/src/Token/Tokenizer.php | 20 +- composer.json | 1 + 45 files changed, 425 insertions(+), 2412 deletions(-) delete mode 100644 SymfonyCustom/Sniffs/Classes/ClassDeclarationSniff.php delete mode 100644 SymfonyCustom/Sniffs/Commenting/DocCommentForbiddenTagsSniff.php delete mode 100644 SymfonyCustom/Sniffs/Formatting/BlankLineBeforeReturnSniff.php delete mode 100644 SymfonyCustom/Sniffs/Formatting/StrictComparisonSniff.php delete mode 100644 SymfonyCustom/Sniffs/Formatting/YodaConditionSniff.php delete mode 100644 SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php delete mode 100644 SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php delete mode 100644 SymfonyCustom/Sniffs/WhiteSpace/CloseBracketSpacingSniff.php delete mode 100644 SymfonyCustom/Sniffs/WhiteSpace/OpenBracketSpacingSniff.php delete mode 100644 SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php delete mode 100644 SymfonyCustom/Tests/Classes/ClassDeclarationUnitTest.inc delete mode 100644 SymfonyCustom/Tests/Classes/ClassDeclarationUnitTest.inc.fixed delete mode 100644 SymfonyCustom/Tests/Classes/ClassDeclarationUnitTest.php delete mode 100644 SymfonyCustom/Tests/Commenting/DocCommentForbiddenTagsUnitTest.inc delete mode 100644 SymfonyCustom/Tests/Commenting/DocCommentForbiddenTagsUnitTest.php delete mode 100644 SymfonyCustom/Tests/Formatting/BlankLineBeforeReturnUnitTest.inc delete mode 100644 SymfonyCustom/Tests/Formatting/BlankLineBeforeReturnUnitTest.inc.fixed delete mode 100644 SymfonyCustom/Tests/Formatting/BlankLineBeforeReturnUnitTest.php delete mode 100644 SymfonyCustom/Tests/Formatting/StrictComparisonUnitTest.inc delete mode 100644 SymfonyCustom/Tests/Formatting/StrictComparisonUnitTest.php delete mode 100644 SymfonyCustom/Tests/Formatting/YodaConditionUnitTest.inc delete mode 100644 SymfonyCustom/Tests/Formatting/YodaConditionUnitTest.php delete mode 100644 SymfonyCustom/Tests/Namespaces/AlphabeticallySortedUseUnitTest.inc delete mode 100644 SymfonyCustom/Tests/Namespaces/AlphabeticallySortedUseUnitTest.php delete mode 100644 SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.inc delete mode 100644 SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.inc.fixed delete mode 100644 SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.php delete mode 100644 SymfonyCustom/Tests/WhiteSpace/CloseBracketSpacingUnitTest.inc delete mode 100644 SymfonyCustom/Tests/WhiteSpace/CloseBracketSpacingUnitTest.inc.fixed delete mode 100644 SymfonyCustom/Tests/WhiteSpace/CloseBracketSpacingUnitTest.php delete mode 100644 SymfonyCustom/Tests/WhiteSpace/OpenBracketSpacingUnitTest.inc delete mode 100644 SymfonyCustom/Tests/WhiteSpace/OpenBracketSpacingUnitTest.inc.fixed delete mode 100644 SymfonyCustom/Tests/WhiteSpace/OpenBracketSpacingUnitTest.php delete mode 100644 SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc delete mode 100644 SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc.fixed delete mode 100644 SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.php diff --git a/SymfonyCustom/Helpers/SniffHelper.php b/SymfonyCustom/Helpers/SniffHelper.php index ee5e305..7147540 100644 --- a/SymfonyCustom/Helpers/SniffHelper.php +++ b/SymfonyCustom/Helpers/SniffHelper.php @@ -13,36 +13,49 @@ class SniffHelper extends AbstractHelper { public const TAGS = [ - '@api', - '@author', + '@required', // Symfony + + '@filesource', + '@source', '@category', + '@package', + '@subpackage', + '@author', + '@created', '@copyright', - '@covers', - '@dataProvider', + '@license', + '@version', + '@since', + + '@api', + '@internal', '@deprecated', - '@example', - '@filesource', - '@global', '@ignore', - '@internal', - '@license', + '@todo', + + '@example', '@link', - '@method', - '@package', - '@param', + '@see', + + '@global', + '@name', '@property', '@property-read', '@property-write', - '@return', - '@see', - '@since', - '@source', - '@subpackage', - '@throws', - '@todo', + '@method', '@uses', + '@var', - '@version', + '@param', + '@return', + '@throws', + + '@abstract', + '@final', + '@public', + '@protected', + '@private', + '@static', ]; public const TAGS_WITH_TYPE = [ @@ -195,7 +208,7 @@ public static function isGlobalUse(File $phpcsFile, int $stackPtr): bool public static function parseTypeHint(string $content): array { preg_match( - '{^'.SniffHelper::REGEX_TYPES.'(?[\s\t]*)?(?.*)?$}six', + '{^'.self::REGEX_TYPES.'(?[\s\t]*)?(?.*)?$}six', $content, $matches ); diff --git a/SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php b/SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php index 54309f9..4635313 100644 --- a/SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -144,10 +144,10 @@ public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $star if (1 !== $spaceBefore) { $fix = $phpcsFile->addFixableError( - 'Expected 1 space between "%s" and double arrow; %s found', + 'Expected 1 space before double arrow; %s found', $nextArrow, 'SpaceBeforeDoubleArrow', - [$tokens[$nextArrow - 1]['content'], $spaceBefore] + [$spaceBefore] ); if ($fix) { @@ -163,10 +163,10 @@ public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $star if (1 !== $spaceAfter) { $fix = $phpcsFile->addFixableError( - 'Expected 1 space between double arrow and "%s"; %s found', + 'Expected 1 space after double arrow; %s found', $nextArrow, 'SpaceAfterDoubleArrow', - [$tokens[$nextArrow + 1]['content'], $spaceAfter] + [$spaceAfter] ); if ($fix) { @@ -188,10 +188,10 @@ public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $star if (1 !== $spaceAfter) { $fix = $phpcsFile->addFixableError( - 'Expected 1 space between comma and "%s"; %s found', + 'Expected 1 space after comma; %s found', $comma, 'SpaceAfterComma', - [$tokens[$comma + 1]['content'], $spaceAfter] + [$spaceAfter] ); if ($fix) { @@ -200,11 +200,17 @@ public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $star } if (T_WHITESPACE === $tokens[$comma - 1]['code']) { + if ($tokens[$comma - 1]['content'] === $phpcsFile->eolChar) { + $spaceLength = 'newline'; + } else { + $spaceLength = $tokens[$comma - 1]['length']; + } + $fix = $phpcsFile->addFixableError( - 'Expected 0 spaces between "%s" and comma; %s found', + 'Expected 0 spaces before comma; %s found', $comma, 'SpaceBeforeComma', - [$tokens[$comma - 2]['content'], $tokens[$comma - 1]['length']] + [$spaceLength] ); if ($fix) { @@ -357,10 +363,10 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start } $fix = $phpcsFile->addFixableError( - 'Expected 0 spaces between "%s" and comma; %s found', + 'Expected 0 space before comma; %s found', $nextToken, 'SpaceBeforeComma', - [$tokens[$nextToken - 2]['content'], $spaceLength] + [$spaceLength] ); if ($fix) { $phpcsFile->fixer->replaceToken($nextToken - 1, ''); diff --git a/SymfonyCustom/Sniffs/Classes/ClassDeclarationSniff.php b/SymfonyCustom/Sniffs/Classes/ClassDeclarationSniff.php deleted file mode 100644 index ab4c532..0000000 --- a/SymfonyCustom/Sniffs/Classes/ClassDeclarationSniff.php +++ /dev/null @@ -1,52 +0,0 @@ -getTokens(); - $openingBrace = $tokens[$stackPtr]['scope_opener']; - - if (!isset($openingBrace)) { - return; - } - - $nextElement = $phpcsFile->findNext(T_WHITESPACE, $openingBrace + 1, null, true); - - if ($tokens[$openingBrace]['line'] + 1 < $tokens[$nextElement]['line']) { - $fix = $phpcsFile->addFixableError( - 'The opening brace should not be followed by a blank line', - $openingBrace, - 'Invalid' - ); - - if ($fix) { - $phpcsFile->fixer->replaceToken($openingBrace + 1, ''); - } - } - } -} diff --git a/SymfonyCustom/Sniffs/Commenting/DocCommentForbiddenTagsSniff.php b/SymfonyCustom/Sniffs/Commenting/DocCommentForbiddenTagsSniff.php deleted file mode 100644 index 86ca211..0000000 --- a/SymfonyCustom/Sniffs/Commenting/DocCommentForbiddenTagsSniff.php +++ /dev/null @@ -1,46 +0,0 @@ -getTokens(); - if (in_array($tokens[$stackPtr]['content'], $this->forbiddenTags)) { - $phpcsFile->addError( - 'The %s annotation is forbidden to use', - $stackPtr, - 'Invalid', - [$tokens[$stackPtr]['content']] - ); - } - } -} diff --git a/SymfonyCustom/Sniffs/Formatting/BlankLineBeforeReturnSniff.php b/SymfonyCustom/Sniffs/Formatting/BlankLineBeforeReturnSniff.php deleted file mode 100644 index 4f845eb..0000000 --- a/SymfonyCustom/Sniffs/Formatting/BlankLineBeforeReturnSniff.php +++ /dev/null @@ -1,90 +0,0 @@ -getTokens(); - $current = $stackPtr - 1; - $prevToken = null; - $returnOrCommentLine = $tokens[$stackPtr]['line']; - - while ($current >= 0 && null === $prevToken) { - if (T_WHITESPACE !== $tokens[$current]['code']) { - if ($this->isComment($tokens[$current])) { - if ($returnOrCommentLine > $tokens[$current]['line'] + 1) { - $prevToken = $tokens[$current]; - } else { - $returnOrCommentLine = $tokens[$current]['line']; - } - } else { - $prevToken = $tokens[$current]; - } - } - $current--; - } - - if (!$prevToken) { - return; - } - - if (T_OPEN_CURLY_BRACKET === $prevToken['code'] || T_COLON === $prevToken['code']) { - return; - } - - if ($returnOrCommentLine - 1 === $prevToken['line']) { - $fix = $phpcsFile->addFixableError( - 'Missing blank line before return statement', - $stackPtr, - 'MissedBlankLineBeforeReturn' - ); - - if ($fix) { - $phpcsFile->fixer->beginChangeset(); - $i = 1; - while (T_WHITESPACE === $tokens[$stackPtr - $i]['code'] - || $this->isComment($tokens[$stackPtr - $i]) - ) { - $i++; - } - $phpcsFile->fixer->addNewLine($stackPtr - $i); - $phpcsFile->fixer->endChangeset(); - } - } - } - - /** - * @param array $token - * - * @return bool - */ - private function isComment(array $token): bool - { - return in_array($token['code'], Tokens::$commentTokens); - } -} diff --git a/SymfonyCustom/Sniffs/Formatting/StrictComparisonSniff.php b/SymfonyCustom/Sniffs/Formatting/StrictComparisonSniff.php deleted file mode 100644 index 87837c7..0000000 --- a/SymfonyCustom/Sniffs/Formatting/StrictComparisonSniff.php +++ /dev/null @@ -1,54 +0,0 @@ - '===', - T_IS_NOT_EQUAL => '!==', - ]; - - /** - * @return int[] - */ - public function register(): array - { - return [T_IS_EQUAL, T_IS_NOT_EQUAL]; - } - - /** - * @param File $phpcsFile - * @param int $stackPtr - * - * @return void - */ - public function process(File $phpcsFile, $stackPtr): void - { - $tokens = $phpcsFile->getTokens(); - - // This error is fixable, but it's too dangerous to add automatically fixer - $phpcsFile->addError( - 'The %s comparator is forbidden, use %s instead', - $stackPtr, - 'NotStrict', - [ - $tokens[$stackPtr]['content'], - $this->operators[$tokens[$stackPtr]['code']], - ] - ); - } -} diff --git a/SymfonyCustom/Sniffs/Formatting/YodaConditionSniff.php b/SymfonyCustom/Sniffs/Formatting/YodaConditionSniff.php deleted file mode 100644 index 3ed8308..0000000 --- a/SymfonyCustom/Sniffs/Formatting/YodaConditionSniff.php +++ /dev/null @@ -1,98 +0,0 @@ -getTokens(); - - $beginners = Tokens::$booleanOperators; - $beginners[] = T_IF; - $beginners[] = T_ELSEIF; - $beginners[] = T_EQUAL; - - $beginning = $phpcsFile->findPrevious($beginners, $stackPtr, null, false, null, true); - - $needsYoda = false; - - // Note: going backwards! - for ($i = $stackPtr; $i > $beginning; $i--) { - // Ignore whitespace. - if (isset(Tokens::$emptyTokens[$tokens[$i]['code']])) { - continue; - } - - // If this is a variable or array, we've seen all we need to see. - if (T_VARIABLE === $tokens[$i]['code'] || T_CLOSE_SQUARE_BRACKET === $tokens[$i]['code']) { - $needsYoda = true; - break; - } - - // If this is a function call or something, we are OK. - if (in_array( - $tokens[$i]['code'], - [T_CONSTANT_ENCAPSED_STRING, T_CLOSE_PARENTHESIS, T_OPEN_PARENTHESIS, T_RETURN], - true - ) - ) { - return; - } - } - - if (!$needsYoda) { - return; - } - - // Check if this is a var to var comparison, e.g.: if ( $var1 == $var2 ). - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true); - - if (isset(Tokens::$castTokens[$tokens[$nextNonEmpty]['code']])) { - $nextNonEmpty = $phpcsFile->findNext( - Tokens::$emptyTokens, - $nextNonEmpty + 1, - null, - true - ); - } - - if (in_array($tokens[$nextNonEmpty]['code'], [T_SELF, T_PARENT, T_STATIC], true)) { - $nextNonEmpty = $phpcsFile->findNext( - array_merge(Tokens::$emptyTokens, [T_DOUBLE_COLON]), - $nextNonEmpty + 1, - null, - true - ); - } - - if (T_VARIABLE === $tokens[$nextNonEmpty]['code']) { - return; - } - - $phpcsFile->addError('Use Yoda Condition checks, you must.', $stackPtr, 'NotYoda'); - } -} diff --git a/SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php b/SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php deleted file mode 100644 index 1bf3f95..0000000 --- a/SymfonyCustom/Sniffs/Namespaces/AlphabeticallySortedUseSniff.php +++ /dev/null @@ -1,165 +0,0 @@ -getTokens(); - - if (!SniffHelper::isNamespace($phpcsFile, $stackPtr)) { - $namespace = $phpcsFile->findNext(T_NAMESPACE, $stackPtr + 1); - - if ($namespace) { - return $namespace; - } - } - - $uses = $this->getUseStatements($phpcsFile, $stackPtr); - - $lastUse = null; - foreach ($uses as $use) { - if (!$lastUse) { - $lastUse = $use; - continue; - } - - $order = $this->compareUseStatements($use, $lastUse); - - if ($order < 0) { - $phpcsFile->addError( - 'Use statements are incorrectly ordered. The first wrong one is %s', - $use['ptrUse'], - 'IncorrectOrder', - [$use['name']] - ); - - return $stackPtr + 1; - } - - $lastUse = $use; - } - - return T_OPEN_TAG === $tokens[$stackPtr]['code'] ? $phpcsFile->numTokens + 1 : $stackPtr + 1; - } - - /** - * @param File $phpcsFile - * @param int $scopePtr - * - * @return array - */ - private function getUseStatements(File $phpcsFile, int $scopePtr): array - { - $tokens = $phpcsFile->getTokens(); - - $uses = []; - - if (isset($tokens[$scopePtr]['scope_opener'])) { - $start = $tokens[$scopePtr]['scope_opener']; - $end = $tokens[$scopePtr]['scope_closer']; - } else { - $start = $scopePtr; - $end = null; - } - - $use = $phpcsFile->findNext(T_USE, $start + 1, $end); - while (false !== $use && T_USE === $tokens[$use]['code']) { - if (!SniffHelper::isGlobalUse($phpcsFile, $use) - || (null !== $end - && (!isset($tokens[$use]['conditions'][$scopePtr]) - || $tokens[$use]['level'] !== $tokens[$scopePtr]['level'] + 1)) - ) { - $use = $phpcsFile->findNext(Tokens::$emptyTokens, $use + 1, $end, true); - continue; - } - - // find semicolon as the end of the global use scope - $endOfScope = $phpcsFile->findNext(T_SEMICOLON, $use + 1); - - $startOfName = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], $use + 1, $endOfScope); - - $type = 'class'; - if (T_STRING === $tokens[$startOfName]['code']) { - $lowerContent = strtolower($tokens[$startOfName]['content']); - if ('function' === $lowerContent || 'const' === $lowerContent) { - $type = $lowerContent; - - $startOfName = $phpcsFile->findNext([T_STRING, T_NS_SEPARATOR], $startOfName + 1, $endOfScope); - } - } - - $uses[] = [ - 'ptrUse' => $use, - 'name' => trim($phpcsFile->getTokensAsString($startOfName, $endOfScope - $startOfName)), - 'ptrEnd' => $endOfScope, - 'string' => trim($phpcsFile->getTokensAsString($use, $endOfScope - $use + 1)), - 'type' => $type, - ]; - - $use = $phpcsFile->findNext(Tokens::$emptyTokens, $endOfScope + 1, $end, true); - } - - return $uses; - } - - /** - * @param array $a - * @param array $b - * - * @return int - */ - private function compareUseStatements(array $a, array $b): int - { - if ($a['type'] === $b['type']) { - return strcasecmp( - $this->clearName($a['name']), - $this->clearName($b['name']) - ); - } - - if ('class' === $a['type'] || ('function' === $a['type'] && 'const' === $b['type'])) { - return -1; - } - - return 1; - } - - /** - * @param string $name - * - * @return string - */ - private function clearName(string $name): string - { - // Handle grouped use - $name = explode('{', $name)[0]; - - return str_replace('\\', ' ', $name); - } -} diff --git a/SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php b/SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php deleted file mode 100644 index c6d9850..0000000 --- a/SymfonyCustom/Sniffs/Namespaces/UnusedUseSniff.php +++ /dev/null @@ -1,500 +0,0 @@ -getTokens(); - - $semiColon = $phpcsFile->findEndOfStatement($stackPtr); - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $semiColon - 1, null, true); - - if (T_CLOSE_USE_GROUP === $tokens[$prev]['code']) { - $to = $prev; - $from = $phpcsFile->findPrevious(T_OPEN_USE_GROUP, $prev - 1); - - // Empty group is invalid syntax - if ($phpcsFile->findNext(Tokens::$emptyTokens, $from + 1, null, true) === $to) { - $fix = $phpcsFile->addFixableError('Empty use group', $stackPtr, 'EmptyUseGroup'); - if ($fix) { - $this->removeUse($phpcsFile, $stackPtr, $semiColon); - } - - return; - } - - $comma = $phpcsFile->findNext(T_COMMA, $from + 1, $to); - if (false === $comma - || !$phpcsFile->findNext(Tokens::$emptyTokens, $comma + 1, $to, true) - ) { - $fix = $phpcsFile->addFixableError( - 'Redundant use group for one declaration', - $stackPtr, - 'RedundantUseGroup' - ); - - if ($fix) { - $phpcsFile->fixer->beginChangeset(); - $phpcsFile->fixer->replaceToken($from, ''); - $i = $from + 1; - - while (T_WHITESPACE === $tokens[$i]['code']) { - $phpcsFile->fixer->replaceToken($i, ''); - ++$i; - } - - if (false !== $comma) { - $phpcsFile->fixer->replaceToken($comma, ''); - } - - $phpcsFile->fixer->replaceToken($to, ''); - $i = $to - 1; - while (T_WHITESPACE === $tokens[$i]['code']) { - $phpcsFile->fixer->replaceToken($i, ''); - --$i; - } - $phpcsFile->fixer->endChangeset(); - } - - return; - } - - $skip = Tokens::$emptyTokens + [T_COMMA => T_COMMA]; - - $classPtr = $phpcsFile->findPrevious($skip, $to - 1, $from + 1, true); - while ($classPtr) { - $to = $phpcsFile->findPrevious(T_COMMA, $classPtr - 1, $from + 1); - - if (!$this->isClassUsed($phpcsFile, $stackPtr, $classPtr)) { - $fix = $phpcsFile->addFixableError( - 'Unused use statement "%s"', - $classPtr, - 'UnusedUseInGroup', - [$tokens[$classPtr]['content']] - ); - - if ($fix) { - $first = false === $to ? $from + 1 : $to; - $last = $classPtr; - if (false === $to) { - $next = $phpcsFile->findNext(Tokens::$emptyTokens, $classPtr + 1, null, true); - if (T_COMMA === $tokens[$next]['code']) { - $last = $next; - } - } - - $phpcsFile->fixer->beginChangeset(); - for ($i = $first; $i <= $last; ++$i) { - $phpcsFile->fixer->replaceToken($i, ''); - } - $phpcsFile->fixer->endChangeset(); - } - } - - if (false === $to) { - break; - } - - $classPtr = $phpcsFile->findPrevious($skip, $to - 1, $from + 1, true); - } - - return; - } - - do { - $classPtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $semiColon - 1, null, true); - if (!$this->isClassUsed($phpcsFile, $stackPtr, $classPtr)) { - $warning = 'Unused use statement "%s"'; - $data = [$tokens[$classPtr]['content']]; - $fix = $phpcsFile->addFixableError($warning, $stackPtr, 'UnusedUse', $data); - - if ($fix) { - $prev = $phpcsFile->findPrevious( - Tokens::$emptyTokens + [ - T_STRING => T_STRING, - T_NS_SEPARATOR => T_NS_SEPARATOR, - T_AS => T_AS, - ], - $classPtr, - null, - true - ); - - $to = $semiColon; - if (T_COMMA === $tokens[$prev]['code']) { - $from = $prev; - $to = $classPtr; - } elseif (T_SEMICOLON === $tokens[$semiColon]['code']) { - $from = $stackPtr; - } else { - $from = $phpcsFile->findNext(Tokens::$emptyTokens, $prev + 1, null, true); - if (T_STRING === $tokens[$from]['code'] - && in_array(strtolower($tokens[$from]['content']), ['const', 'function'], true) - ) { - $from = $phpcsFile->findNext(Tokens::$emptyTokens, $from + 1, null, true); - } - } - - $this->removeUse($phpcsFile, $from, $to); - } - } - - if (T_SEMICOLON === $tokens[$semiColon]['code']) { - break; - } - - $semiColon = $phpcsFile->findEndOfStatement($semiColon + 1); - } while ($semiColon); - } - - /** - * @param File $phpcsFile - * @param int $from - * @param int $to - * - * @return void - */ - private function removeUse(File $phpcsFile, int $from, int $to): void - { - $tokens = $phpcsFile->getTokens(); - - $phpcsFile->fixer->beginChangeset(); - - // Remote whitespaces before in the same line - if (T_WHITESPACE === $tokens[$from - 1]['code'] - && $tokens[$from - 1]['line'] === $tokens[$from]['line'] - && $tokens[$from - 2]['line'] !== $tokens[$from]['line'] - ) { - $phpcsFile->fixer->replaceToken($from - 1, ''); - } - - for ($i = $from; $i <= $to; ++$i) { - $phpcsFile->fixer->replaceToken($i, ''); - } - - // Also remove whitespace after the semicolon (new lines). - if (isset($tokens[$to + 1]) && T_WHITESPACE === $tokens[$to + 1]['code']) { - $phpcsFile->fixer->replaceToken($to + 1, ''); - } - $phpcsFile->fixer->endChangeset(); - } - - /** - * @param File $phpcsFile - * @param int $usePtr - * @param int $classPtr - * - * @return bool - */ - private function isClassUsed(File $phpcsFile, int $usePtr, int $classPtr): bool - { - $tokens = $phpcsFile->getTokens(); - - // Search where the class name is used. PHP treats class names case - // insensitive, that's why we cannot search for the exact class name string - // and need to iterate over all T_STRING tokens in the file. - $classUsed = $phpcsFile->findNext( - [T_STRING, T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG, T_NAMESPACE], - $classPtr + 1 - ); - $className = $tokens[$classPtr]['content']; - - // Check if the referenced class is in the same namespace as the current - // file. If it is then the use statement is not necessary. - $namespacePtr = $phpcsFile->findPrevious(T_NAMESPACE, $usePtr); - while (false !== $namespacePtr && false === SniffHelper::isNamespace($phpcsFile, $namespacePtr)) { - $phpcsFile->findPrevious(T_NAMESPACE, $namespacePtr - 1); - } - - $namespaceEnd = false !== $namespacePtr && isset($tokens[$namespacePtr]['scope_closer']) - ? $tokens[$namespacePtr]['scope_closer'] - : null; - - $type = 'class'; - $next = $phpcsFile->findNext(Tokens::$emptyTokens, $usePtr + 1, null, true); - if (T_STRING === $tokens[$next]['code'] - && in_array(strtolower($tokens[$next]['content']), ['const', 'function'], true) - ) { - $type = strtolower($tokens[$next]['content']); - } - - $searchName = 'const' === $type ? $className : strtolower($className); - - $prev = $phpcsFile->findPrevious( - Tokens::$emptyTokens + [ - T_STRING => T_STRING, - T_NS_SEPARATOR => T_NS_SEPARATOR, - ], - $classPtr - 1, - null, - $usePtr - ); - - // Only if alias is not used. - if (T_AS !== $tokens[$prev]['code']) { - $isGroup = T_OPEN_USE_GROUP === $tokens[$prev]['code'] - || false !== $phpcsFile->findPrevious(T_OPEN_USE_GROUP, $prev, $usePtr); - - $useNamespace = ''; - if ($isGroup || T_COMMA !== $tokens[$prev]['code']) { - $useNamespacePtr = 'class' === $type ? $next : $next + 1; - $useNamespace = $this->getNamespace( - $phpcsFile, - $useNamespacePtr, - [T_OPEN_USE_GROUP, T_COMMA, T_AS, T_SEMICOLON] - ); - - if ($isGroup) { - $useNamespace .= '\\'; - } - } - - if (T_COMMA === $tokens[$prev]['code'] || T_OPEN_USE_GROUP === $tokens[$prev]['code']) { - $useNamespace .= $this->getNamespace( - $phpcsFile, - $prev + 1, - [T_CLOSE_USE_GROUP, T_COMMA, T_AS, T_SEMICOLON] - ); - } - - $useNamespace = substr($useNamespace, 0, strrpos($useNamespace, '\\') ?: 0); - - if (false !== $namespacePtr) { - $namespace = $this->getNamespace($phpcsFile, $namespacePtr + 1, [T_CURLY_OPEN, T_SEMICOLON]); - - if (strcasecmp($namespace, $useNamespace) === 0) { - $classUsed = false; - } - } elseif (false === $namespacePtr && '' === $useNamespace) { - $classUsed = false; - } - } - - $emptyTokens = Tokens::$emptyTokens; - unset($emptyTokens[T_DOC_COMMENT_TAG]); - - while (false !== $classUsed && false === SniffHelper::isNamespace($phpcsFile, $classUsed)) { - $isStringToken = T_STRING === $tokens[$classUsed]['code']; - - $match = null; - - if (($isStringToken - && (('const' !== $type && strtolower($tokens[$classUsed]['content']) === $searchName) - || ('const' === $type && $tokens[$classUsed]['content'] === $searchName))) - || ('class' === $type - && ((T_DOC_COMMENT_STRING === $tokens[$classUsed]['code'] - && preg_match( - '/(\s|\||\(|\<|\,|^)'.preg_quote($searchName, '/').'(\s|\||\\\\|\<|\,|\>|$|\[\])/i', - $tokens[$classUsed]['content'] - )) - || (T_DOC_COMMENT_TAG === $tokens[$classUsed]['code'] - && preg_match( - '/@'.preg_quote($searchName, '/').'(\(|\\\\|$)/i', - $tokens[$classUsed]['content'] - )) - || (!$isStringToken - && !preg_match( - '/"[^"]*'.preg_quote($searchName, '/').'\b[^"]*"/i', - $tokens[$classUsed]['content'] - ) - && preg_match( - '/(?findPrevious( - $isStringToken ? Tokens::$emptyTokens : $emptyTokens, - $classUsed - 1, - null, - true - ); - - if ($isStringToken) { - if ($this->determineType($phpcsFile, $beforeUsage, $classUsed) === $type) { - return true; - } - } elseif (T_DOC_COMMENT_STRING === $tokens[$classUsed]['code']) { - if (T_DOC_COMMENT_TAG === $tokens[$beforeUsage]['code'] - && in_array($tokens[$beforeUsage]['content'], SniffHelper::TAGS_WITH_TYPE, true) - ) { - return true; - } - - if ($match) { - return true; - } - } else { - return true; - } - } - - $classUsed = $phpcsFile->findNext( - [T_STRING, T_DOC_COMMENT_STRING, T_DOC_COMMENT_TAG, T_NAMESPACE], - $classUsed + 1, - $namespaceEnd - ); - } - - return false; - } - - /** - * @param File $phpcsFile - * @param int $ptr - * @param array $stop - * - * @return string - */ - private function getNamespace(File $phpcsFile, int $ptr, array $stop): string - { - $tokens = $phpcsFile->getTokens(); - - $result = ''; - while (!in_array($tokens[$ptr]['code'], $stop, true)) { - if (in_array($tokens[$ptr]['code'], [T_STRING, T_NS_SEPARATOR], true)) { - $result .= $tokens[$ptr]['content']; - } - - ++$ptr; - } - - return trim(trim($result), '\\'); - } - - /** - * @param File $phpcsFile - * @param int $beforePtr - * @param int $ptr - * - * @return string|null - */ - private function determineType(File $phpcsFile, int $beforePtr, int $ptr): ?string - { - $tokens = $phpcsFile->getTokens(); - - $beforeCode = $tokens[$beforePtr]['code']; - - if (in_array( - $beforeCode, - [T_NS_SEPARATOR, T_OBJECT_OPERATOR, T_DOUBLE_COLON, T_FUNCTION, T_CONST, T_AS, T_INSTEADOF], - true - )) { - return null; - } - - if (in_array( - $beforeCode, - [T_NEW, T_NULLABLE, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF], - true - )) { - return 'class'; - } - - // Trait usage - if (T_USE === $beforeCode) { - if (SniffHelper::isTraitUse($phpcsFile, $beforePtr)) { - return 'class'; - } - - return null; - } - - if (T_COMMA === $beforeCode) { - $prev = $phpcsFile->findPrevious( - Tokens::$emptyTokens + [ - T_STRING => T_STRING, - T_NS_SEPARATOR => T_NS_SEPARATOR, - T_COMMA => T_COMMA, - ], - $beforePtr - 1, - null, - true - ); - - if (T_IMPLEMENTS === $tokens[$prev]['code'] || T_EXTENDS === $tokens[$prev]['code']) { - return 'class'; - } - } - - $afterPtr = $phpcsFile->findNext(Tokens::$emptyTokens, $ptr + 1, null, true); - $afterCode = $tokens[$afterPtr]['code']; - - if (T_AS === $afterCode) { - return null; - } - - if (T_OPEN_PARENTHESIS === $afterCode) { - return 'function'; - } - - if (in_array( - $afterCode, - [T_DOUBLE_COLON, T_VARIABLE, T_ELLIPSIS, T_NS_SEPARATOR, T_OPEN_CURLY_BRACKET], - true - )) { - return 'class'; - } - - if (T_COLON === $beforeCode) { - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $beforePtr - 1, null, true); - if (false !== $prev - && T_CLOSE_PARENTHESIS === $tokens[$prev]['code'] - && isset($tokens[$prev]['parenthesis_owner']) - && T_FUNCTION === $tokens[$tokens[$prev]['parenthesis_owner']]['code'] - ) { - return 'class'; - } - } - - if (T_BITWISE_OR === $afterCode) { - $next = $phpcsFile->findNext( - Tokens::$emptyTokens + [ - T_BITWISE_OR => T_BITWISE_OR, - T_STRING => T_STRING, - T_NS_SEPARATOR => T_NS_SEPARATOR, - ], - $afterPtr, - null, - true - ); - - if (T_VARIABLE === $tokens[$next]['code']) { - return 'class'; - } - } - - return 'const'; - } -} diff --git a/SymfonyCustom/Sniffs/WhiteSpace/CloseBracketSpacingSniff.php b/SymfonyCustom/Sniffs/WhiteSpace/CloseBracketSpacingSniff.php deleted file mode 100644 index 553b1a9..0000000 --- a/SymfonyCustom/Sniffs/WhiteSpace/CloseBracketSpacingSniff.php +++ /dev/null @@ -1,52 +0,0 @@ -getTokens(); - - if (isset($tokens[$stackPtr - 1]) && T_WHITESPACE === $tokens[$stackPtr - 1]['code']) { - $before = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true); - if (false !== $before && $tokens[$stackPtr]['line'] === $tokens[$before]['line']) { - $error = 'There should be no space before a closing "%s"'; - $fix = $phpcsFile->addFixableError( - $error, - $stackPtr - 1, - 'ClosingWhitespace', - [$tokens[$stackPtr]['content']] - ); - - if ($fix) { - $phpcsFile->fixer->replaceToken($stackPtr - 1, ''); - } - } - } - } -} diff --git a/SymfonyCustom/Sniffs/WhiteSpace/OpenBracketSpacingSniff.php b/SymfonyCustom/Sniffs/WhiteSpace/OpenBracketSpacingSniff.php deleted file mode 100644 index 9a5b27c..0000000 --- a/SymfonyCustom/Sniffs/WhiteSpace/OpenBracketSpacingSniff.php +++ /dev/null @@ -1,51 +0,0 @@ -getTokens(); - - if (isset($tokens[$stackPtr + 1]) - && T_WHITESPACE === $tokens[$stackPtr + 1]['code'] - && false === strpos($tokens[$stackPtr + 1]['content'], $phpcsFile->eolChar) - ) { - $error = 'There should be no space after an opening "%s"'; - $fix = $phpcsFile->addFixableError( - $error, - $stackPtr + 1, - 'OpeningWhitespace', - [$tokens[$stackPtr]['content']] - ); - - if ($fix) { - $phpcsFile->fixer->replaceToken($stackPtr + 1, ''); - } - } - } -} diff --git a/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php b/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php deleted file mode 100644 index 452c13e..0000000 --- a/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php +++ /dev/null @@ -1,68 +0,0 @@ -getTokens(); - - // Find the last syntax item to determine if this is an unary operator. - $lastSyntaxItem = $phpcsFile->findPrevious( - T_WHITESPACE, - $stackPtr - 1, - ($tokens[$stackPtr]['column']) * -1, - true, - null, - true - ); - $operatorSuffixAllowed = in_array( - $tokens[$lastSyntaxItem]['code'], - [ - T_LNUMBER, - T_DNUMBER, - T_CLOSE_PARENTHESIS, - T_CLOSE_CURLY_BRACKET, - T_CLOSE_SQUARE_BRACKET, - T_VARIABLE, - T_STRING, - ] - ); - - // Check plus / minus value assignments or comparisons. - if (!$operatorSuffixAllowed && T_WHITESPACE === $tokens[$stackPtr + 1]['code']) { - $fix = $phpcsFile->addFixableError( - 'A unary operator statement must not be followed by a space', - $stackPtr, - 'Invalid' - ); - - if ($fix) { - $phpcsFile->fixer->replaceToken($stackPtr + 1, ''); - } - } - } -} diff --git a/SymfonyCustom/Tests/Classes/ClassDeclarationUnitTest.inc b/SymfonyCustom/Tests/Classes/ClassDeclarationUnitTest.inc deleted file mode 100644 index 09a3fe2..0000000 --- a/SymfonyCustom/Tests/Classes/ClassDeclarationUnitTest.inc +++ /dev/null @@ -1,17 +0,0 @@ - 1, - 13 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/Tests/Commenting/DocCommentForbiddenTagsUnitTest.inc b/SymfonyCustom/Tests/Commenting/DocCommentForbiddenTagsUnitTest.inc deleted file mode 100644 index 39ba48e..0000000 --- a/SymfonyCustom/Tests/Commenting/DocCommentForbiddenTagsUnitTest.inc +++ /dev/null @@ -1,22 +0,0 @@ - 1, - 7 => 1, - 11 => 1, - 15 => 1, - 20 => 1, - 21 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/Tests/Formatting/BlankLineBeforeReturnUnitTest.inc b/SymfonyCustom/Tests/Formatting/BlankLineBeforeReturnUnitTest.inc deleted file mode 100644 index a0811b5..0000000 --- a/SymfonyCustom/Tests/Formatting/BlankLineBeforeReturnUnitTest.inc +++ /dev/null @@ -1,85 +0,0 @@ - 1, - 67 => 1, - 76 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/Tests/Formatting/ConditionalReturnOrThrowUnitTest.inc b/SymfonyCustom/Tests/Formatting/ConditionalReturnOrThrowUnitTest.inc index 2cd8615..475f22d 100644 --- a/SymfonyCustom/Tests/Formatting/ConditionalReturnOrThrowUnitTest.inc +++ b/SymfonyCustom/Tests/Formatting/ConditionalReturnOrThrowUnitTest.inc @@ -95,4 +95,3 @@ class Test } } } - diff --git a/SymfonyCustom/Tests/Formatting/StrictComparisonUnitTest.inc b/SymfonyCustom/Tests/Formatting/StrictComparisonUnitTest.inc deleted file mode 100644 index 5d879f7..0000000 --- a/SymfonyCustom/Tests/Formatting/StrictComparisonUnitTest.inc +++ /dev/null @@ -1,9 +0,0 @@ - 1, - 5 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/Tests/Formatting/YodaConditionUnitTest.inc b/SymfonyCustom/Tests/Formatting/YodaConditionUnitTest.inc deleted file mode 100644 index 563b190..0000000 --- a/SymfonyCustom/Tests/Formatting/YodaConditionUnitTest.inc +++ /dev/null @@ -1,100 +0,0 @@ -num_rows === 0) { -} // Bad. - -if ($true == strtolower($check)) { -} // Bad. - -$update = 'yes' === strtolower($this->from_post('update')); // Ok. - -$currentIsCustom = false === in_array($currentType, $this->tags); // Ok. diff --git a/SymfonyCustom/Tests/Formatting/YodaConditionUnitTest.php b/SymfonyCustom/Tests/Formatting/YodaConditionUnitTest.php deleted file mode 100644 index e3bd653..0000000 --- a/SymfonyCustom/Tests/Formatting/YodaConditionUnitTest.php +++ /dev/null @@ -1,43 +0,0 @@ - 2, - 4 => 2, - 11 => 1, - 18 => 1, - 25 => 1, - 32 => 1, - 49 => 1, - 55 => 1, - 62 => 1, - 68 => 1, - 87 => 1, - 92 => 1, - 95 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/Tests/Namespaces/AlphabeticallySortedUseUnitTest.inc b/SymfonyCustom/Tests/Namespaces/AlphabeticallySortedUseUnitTest.inc deleted file mode 100644 index 1a33fab..0000000 --- a/SymfonyCustom/Tests/Namespaces/AlphabeticallySortedUseUnitTest.inc +++ /dev/null @@ -1,92 +0,0 @@ - $foo; - }; - } - - public function anonym() - { - return new class() { - use AndAnotherTrait; - }; - } -} - -namespace FooBis; - -use D; -use PHP_CodeSniffer\Files\{File, FileList}; -use PHP_CodeSniffer\Files\LocalFile; diff --git a/SymfonyCustom/Tests/Namespaces/AlphabeticallySortedUseUnitTest.php b/SymfonyCustom/Tests/Namespaces/AlphabeticallySortedUseUnitTest.php deleted file mode 100644 index 5458608..0000000 --- a/SymfonyCustom/Tests/Namespaces/AlphabeticallySortedUseUnitTest.php +++ /dev/null @@ -1,33 +0,0 @@ - 1, - 22 => 1, - 58 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.inc b/SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.inc deleted file mode 100644 index 8a1fa37..0000000 --- a/SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.inc +++ /dev/null @@ -1,152 +0,0 @@ -SomethingElse]; - - return $client; - } - - function test4(ClassD $classD): F - { - $d = fn_d(ConstD); - - return $d; - } - - function test5() - { - try { - throw new SomeException(); - } catch (SomeFooException $e) { - return; - } - } -} - -class SecondContainer -{ - /** - * @var Test|TestA|TestB|TestC|TestD|TestE - */ - public $test; -} - -namespace AnotherProject; - -use Unused; -use Somewhere\SomethingElse; -use AnotherProject\Thing; - -echo namespace\my_function(); - -class ThirdContainer -{ - /** - * @var Thing|SomethingElse - */ - public $test; -} - -function () use ($tax) {} - -namespace App\Entity; - -use Doctrine\ORM\Mapping as ORM; -use Doctrine\ORM\Mapping\UniqueConstraint; - -/** - * @ORM\Table(name="whatever", uniqueConstraints={@UniqueConstraint(name="idx_whatever_code_UNIQUE", columns={"code"})}) - * @ORM\Entity - */ -class Whatever {} - -namespace MyApp\ /* comment */ MyNamespace; - -use MyApp\ /* comment */ MyClass1 ; -use \Hello\MyClass2; -use MyApp\MyNamespace\MyClass3; -use MyApp\ /* comment */ MyClass4; -use \Hello\MyClass5; -use MyApp /* comment */ \MyNamespace\MyClass6; -use \MyApp/*comment*/\MyNamespace\MyClass7; -use /* comment */DateTime; - -new MyClass1(); -new MyClass2(); -new MyClass3(); - -namespace BugCollection; - -use Doctrine\Collection; -use Doctrine\Value; -use Doctrine\Value2; -use Doctrine\Key; -use Doctrine\Key2; - -/** - * @param Collection $a - * @param Collection $a - * - * @return null - */ -function ($a) -{ - return null; -} diff --git a/SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.inc.fixed b/SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.inc.fixed deleted file mode 100644 index a2ad6fb..0000000 --- a/SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.inc.fixed +++ /dev/null @@ -1,139 +0,0 @@ -SomethingElse]; - - return $client; - } - - function test4(ClassD $classD): F - { - $d = fn_d(ConstD); - - return $d; - } - - function test5() - { - try { - throw new SomeException(); - } catch (SomeFooException $e) { - return; - } - } -} - -class SecondContainer -{ - /** - * @var Test|TestA|TestB|TestC|TestD|TestE - */ - public $test; -} - -namespace AnotherProject; - -use Somewhere\SomethingElse; - -echo namespace\my_function(); - -class ThirdContainer -{ - /** - * @var Thing|SomethingElse - */ - public $test; -} - -function () use ($tax) {} - -namespace App\Entity; - -use Doctrine\ORM\Mapping as ORM; -use Doctrine\ORM\Mapping\UniqueConstraint; - -/** - * @ORM\Table(name="whatever", uniqueConstraints={@UniqueConstraint(name="idx_whatever_code_UNIQUE", columns={"code"})}) - * @ORM\Entity - */ -class Whatever {} - -namespace MyApp\ /* comment */ MyNamespace; - -use MyApp\ /* comment */ MyClass1 ; -use \Hello\MyClass2; - -new MyClass1(); -new MyClass2(); -new MyClass3(); - -namespace BugCollection; - -use Doctrine\Collection; -use Doctrine\Value; -use Doctrine\Value2; -use Doctrine\Key; -use Doctrine\Key2; - -/** - * @param Collection $a - * @param Collection $a - * - * @return null - */ -function ($a) -{ - return null; -} diff --git a/SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.php b/SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.php deleted file mode 100644 index 376bd55..0000000 --- a/SymfonyCustom/Tests/Namespaces/UnusedUseUnitTest.php +++ /dev/null @@ -1,48 +0,0 @@ - 1, - 13 => 1, - 14 => 1, - 15 => 2, - 16 => 2, - 18 => 1, - 20 => 1, - 21 => 1, - 22 => 1, - 23 => 1, - 93 => 1, - 95 => 1, - 124 => 1, - 125 => 1, - 126 => 1, - 127 => 1, - 128 => 1, - 129 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/Tests/WhiteSpace/CloseBracketSpacingUnitTest.inc b/SymfonyCustom/Tests/WhiteSpace/CloseBracketSpacingUnitTest.inc deleted file mode 100644 index 187ed40..0000000 --- a/SymfonyCustom/Tests/WhiteSpace/CloseBracketSpacingUnitTest.inc +++ /dev/null @@ -1,9 +0,0 @@ - 1, - 5 => 2, - 7 => 1, - 9 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/Tests/WhiteSpace/OpenBracketSpacingUnitTest.inc b/SymfonyCustom/Tests/WhiteSpace/OpenBracketSpacingUnitTest.inc deleted file mode 100644 index 187ed40..0000000 --- a/SymfonyCustom/Tests/WhiteSpace/OpenBracketSpacingUnitTest.inc +++ /dev/null @@ -1,9 +0,0 @@ - 1, - 5 => 2, - 7 => 1, - 9 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc b/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc deleted file mode 100644 index 7909d3d..0000000 --- a/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc +++ /dev/null @@ -1,10 +0,0 @@ - 1, - 6 => 1, - 9 => 1, - 10 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/ruleset.xml b/SymfonyCustom/ruleset.xml index ce93360..4cea822 100755 --- a/SymfonyCustom/ruleset.xml +++ b/SymfonyCustom/ruleset.xml @@ -6,15 +6,33 @@ */vendor/* - + + + - + + + + + + + + + + + + + + + + + @@ -24,19 +42,6 @@ - - - - - error - - - - - - - - @@ -52,84 +57,382 @@ - - - - - - + - - - - - - - - - - - - - - - - - - - - - error error - - - - - - - error - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + error + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + 0 + + - + + + + + + + + - + + + error + + + + + + + + + + + - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TwigCS/src/Report/SniffViolation.php b/TwigCS/src/Report/SniffViolation.php index 01b3d69..5aa1148 100644 --- a/TwigCS/src/Report/SniffViolation.php +++ b/TwigCS/src/Report/SniffViolation.php @@ -55,7 +55,7 @@ class SniffViolation * * @return void */ - public function __construct(int $level, string $message, string $filename, int $line = null) + public function __construct(int $level, string $message, string $filename, ?int $line = null) { $this->level = $level; $this->message = $message; diff --git a/TwigCS/src/Report/TextFormatter.php b/TwigCS/src/Report/TextFormatter.php index f2c2f28..a3b4e40 100644 --- a/TwigCS/src/Report/TextFormatter.php +++ b/TwigCS/src/Report/TextFormatter.php @@ -42,7 +42,7 @@ public function __construct(InputInterface $input, OutputInterface $output) * * @return void */ - public function display(Report $report, string $level = null): void + public function display(Report $report, ?string $level = null): void { foreach ($report->getFiles() as $file) { $fileMessages = $report->getMessages([ diff --git a/TwigCS/src/Sniff/AbstractSpacingSniff.php b/TwigCS/src/Sniff/AbstractSpacingSniff.php index 3620cb4..009d50c 100644 --- a/TwigCS/src/Sniff/AbstractSpacingSniff.php +++ b/TwigCS/src/Sniff/AbstractSpacingSniff.php @@ -75,7 +75,7 @@ private function checkSpaceAfter(int $tokenPosition, array $tokens, int $expecte $count = 0; } - if ($expected !== $count) { + if ($expected === $count) { $fix = $this->addFixableError( sprintf('Expecting %d whitespace after "%s"; found %d', $expected, $token->getValue(), $count), $token diff --git a/TwigCS/src/Token/Tokenizer.php b/TwigCS/src/Token/Tokenizer.php index 6631c79..814e453 100644 --- a/TwigCS/src/Token/Tokenizer.php +++ b/TwigCS/src/Token/Tokenizer.php @@ -43,52 +43,52 @@ class Tokenizer /** * @var int */ - protected $cursor = 0; + private $cursor = 0; /** * @var int|null */ - protected $end; + private $end; /** * @var int */ - protected $line = 1; + private $line = 1; /** * @var int */ - protected $currentPosition = 0; + private $currentPosition = 0; /** * @var Token[] */ - protected $tokens = []; + private $tokens = []; /** * @var array */ - protected $tokenPositions = []; + private $tokenPositions = []; /** * @var array[] */ - protected $state = []; + private $state = []; /** * @var array */ - protected $bracketsAndTernary = []; + private $bracketsAndTernary = []; /** * @var string|null */ - protected $code; + private $code; /** * @var string|null */ - protected $filename; + private $filename; /** * @param Environment $env diff --git a/composer.json b/composer.json index 7c8adbd..13e8b4b 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "bin": ["bin/twigcs"], "require": { "php": ">=7.2", + "slevomat/coding-standard": "^6.2", "squizlabs/php_codesniffer": "^3.5", "symfony/console": "^4.4 || ^5.0", "symfony/twig-bridge": "^4.4 || ^5.0",