From 566ff067e8a04ebc4b2160fccb6b3d7b9f625237 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 23 Dec 2019 02:14:03 +0100 Subject: [PATCH 1/2] :lipstick: Use strict type --- .../Sniffs/Arrays/ArrayDeclarationSniff.php | 154 +++++++++--------- .../Sniffs/Classes/ClassDeclarationSniff.php | 2 + .../Classes/PropertyDeclarationSniff.php | 2 + .../Sniffs/Commenting/ClassCommentSniff.php | 2 + .../DocCommentForbiddenTagsSniff.php | 2 + .../DocCommentGroupSameTypeSniff.php | 2 + .../Sniffs/Commenting/DocCommentSniff.php | 24 +-- .../Commenting/FunctionCommentSniff.php | 20 ++- .../Commenting/VariableCommentSniff.php | 10 +- .../Sniffs/Errors/ExceptionMessageSniff.php | 2 + .../Sniffs/Errors/UserDeprecatedSniff.php | 2 + .../Formatting/BlankLineBeforeReturnSniff.php | 2 + .../ConditionalReturnOrThrowSniff.php | 2 + .../Formatting/StrictComparisonSniff.php | 2 + .../Sniffs/Formatting/YodaConditionSniff.php | 10 +- .../Sniffs/Functions/ScopeOrderSniff.php | 2 + .../AlphabeticallySortedUseSniff.php | 2 + .../Namespaces/NamespaceDeclarationSniff.php | 2 + .../Sniffs/Namespaces/UnusedUseSniff.php | 2 + .../UseWithoutStartingBackslashSniff.php | 2 + .../NamingConventions/ValidClassNameSniff.php | 2 + .../NamingConventions/ValidFileNameSniff.php | 2 + .../ValidScalarTypeNameSniff.php | 2 + .../Objects/ObjectInstantiationSniff.php | 2 + .../Sniffs/PHP/DiscourageEmptySniff.php | 2 + SymfonyCustom/Sniffs/SniffHelper.php | 4 +- .../WhiteSpace/CloseBracketSpacingSniff.php | 8 +- .../WhiteSpace/DocCommentTagSpacingSniff.php | 24 +-- .../Sniffs/WhiteSpace/EmptyLinesSniff.php | 2 + .../WhiteSpace/OpenBracketSpacingSniff.php | 10 +- .../WhiteSpace/UnaryOperatorSpacingSniff.php | 12 +- .../Tests/Arrays/ArrayDeclarationUnitTest.php | 2 + .../Classes/ClassDeclarationUnitTest.php | 2 + .../Classes/PropertyDeclarationUnitTest.php | 2 + .../Tests/Commenting/ClassCommentUnitTest.php | 2 + .../DocCommentForbiddenTagsUnitTest.php | 2 + .../DocCommentGroupSameTypeUnitTest.php | 2 + .../Tests/Commenting/DocCommentUnitTest.php | 2 + .../Commenting/FunctionCommentUnitTest.php | 2 + .../Commenting/VariableCommentUnitTest.php | 2 + .../Tests/Errors/ExceptionMessageUnitTest.php | 2 + .../Tests/Errors/UserDeprecatedUnitTest.php | 2 + .../BlankLineBeforeReturnUnitTest.php | 2 + .../ConditionalReturnOrThrowUnitTest.php | 2 + .../Formatting/StrictComparisonUnitTest.php | 2 + .../Formatting/YodaConditionUnitTest.php | 2 + .../Tests/Functions/ScopeOrderUnitTest.php | 2 + .../AlphabeticallySortedUseUnitTest.php | 2 + .../NamespaceDeclarationUnitTest.php | 2 + .../Tests/Namespaces/UnusedUseUnitTest.php | 2 + .../UseWithoutStartingBackslashUnitTest.php | 2 + .../ValidClassNameUnitTest.php | 2 + .../ValidFileNameUnitTest.php | 2 + .../ValidScalarTypeNameUnitTest.php | 2 + .../Objects/ObjectInstantiationUnitTest.php | 2 + .../Tests/PHP/DiscourageEmptyUnitTest.php | 2 + .../CloseBracketSpacingUnitTest.php | 2 + .../DocCommentTagSpacingUnitTest.php | 2 + .../Tests/WhiteSpace/EmptyLinesUnitTest.php | 2 + .../WhiteSpace/OpenBracketSpacingUnitTest.php | 2 + .../UnaryOperatorSpacingUnitTest.php | 2 + TwigCS/Command/TwigCSCommand.php | 2 + TwigCS/Config/Config.php | 4 +- TwigCS/Environment/StubbedEnvironment.php | 2 + TwigCS/Report/Report.php | 31 ++-- TwigCS/Report/SniffViolation.php | 21 ++- TwigCS/Report/TextFormatter.php | 20 ++- TwigCS/Ruleset/Generic/BlankEOFSniff.php | 2 + .../Ruleset/Generic/DelimiterSpacingSniff.php | 2 + .../Generic/DisallowCommentedCodeSniff.php | 2 + TwigCS/Ruleset/Generic/EmptyLinesSniff.php | 2 + .../Ruleset/Generic/OperatorSpacingSniff.php | 2 + TwigCS/Ruleset/Ruleset.php | 2 + TwigCS/Runner/Fixer.php | 18 +- TwigCS/Runner/Linter.php | 2 + TwigCS/Sniff/AbstractSniff.php | 10 +- TwigCS/Sniff/AbstractSpacingSniff.php | 2 + TwigCS/Sniff/SniffInterface.php | 2 + TwigCS/Tests/AbstractSniffTest.php | 2 + TwigCS/Tests/Ruleset/Generic/BlankEOFTest.php | 2 + .../Ruleset/Generic/DelimiterSpacingTest.php | 2 + .../Generic/DisallowCommentedCodeTest.php | 2 + .../Tests/Ruleset/Generic/EmptyLinesTest.php | 2 + .../Ruleset/Generic/OperatorSpacingTest.php | 2 + TwigCS/Token/Token.php | 40 ++--- TwigCS/Token/TokenParser.php | 2 + TwigCS/Token/Tokenizer.php | 65 ++++---- TwigCS/Token/TokenizerHelper.php | 4 +- bootstrap.php | 2 +- 89 files changed, 397 insertions(+), 232 deletions(-) diff --git a/SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php b/SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php index 21911c5..f2d1489 100644 --- a/SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/SymfonyCustom/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -1,5 +1,7 @@ addFixableError( 'There must be no space between the "array" keyword and the opening parenthesis', $stackPtr, @@ -73,7 +75,7 @@ public function process(File $phpcsFile, $stackPtr): void if ($fix) { $phpcsFile->fixer->beginChangeset(); - for ($i = ($stackPtr + 1); $i < $arrayStart; $i++) { + for ($i = $stackPtr + 1; $i < $arrayStart; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } @@ -83,14 +85,14 @@ public function process(File $phpcsFile, $stackPtr): void } else { $phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'yes'); $arrayStart = $stackPtr; - $arrayEnd = $tokens[$stackPtr]['bracket_closer']; + $arrayEnd = $tokens[$stackPtr]['bracket_closer']; } // Check for empty arrays. - $content = $phpcsFile->findNext(T_WHITESPACE, ($arrayStart + 1), ($arrayEnd + 1), true); + $content = $phpcsFile->findNext(T_WHITESPACE, $arrayStart + 1, $arrayEnd + 1, true); if ($content === $arrayEnd) { // Empty array, but if the brackets aren't together, there's a problem. - if (($arrayEnd - $arrayStart) !== 1) { + if (1 !== $arrayEnd - $arrayStart) { $fix = $phpcsFile->addFixableError( 'Empty array declaration must have no space between the parentheses', $stackPtr, @@ -99,7 +101,7 @@ public function process(File $phpcsFile, $stackPtr): void if ($fix) { $phpcsFile->fixer->beginChangeset(); - for ($i = ($arrayStart + 1); $i < $arrayEnd; $i++) { + for ($i = $arrayStart + 1; $i < $arrayEnd; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } @@ -132,8 +134,8 @@ public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $star // Check if there are multiple values. If so, then it has to be multiple lines // unless it is contained inside a function call or condition. $valueCount = 0; - $commas = []; - for ($i = ($start + 1); $i < $end; $i++) { + $commas = []; + for ($i = $start + 1; $i < $end; $i++) { // Skip bracketed statements, like function calls. if (T_OPEN_PARENTHESIS === $tokens[$i]['code']) { $i = $tokens[$i]['parenthesis_closer']; @@ -142,7 +144,7 @@ public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $star if (T_COMMA === $tokens[$i]['code']) { // Before counting this comma, make sure we are not at the end of the array. - $next = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), $end, true); + $next = $phpcsFile->findNext(T_WHITESPACE, $i + 1, $end, true); if (false !== $next) { $valueCount++; $commas[] = $i; @@ -162,105 +164,105 @@ public function processSingleLineArray(File $phpcsFile, int $stackPtr, int $star } // Now check each of the double arrows (if any). - $nextArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, ($start + 1), $end); + $nextArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $start + 1, $end); while (false !== $nextArrow) { - if (T_WHITESPACE !== $tokens[($nextArrow - 1)]['code']) { + if (T_WHITESPACE !== $tokens[$nextArrow - 1]['code']) { $fix = $phpcsFile->addFixableError( 'Expected 1 space between "%s" and double arrow; 0 found', $nextArrow, 'NoSpaceBeforeDoubleArrow', - [$tokens[($nextArrow - 1)]['content']] + [$tokens[$nextArrow - 1]['content']] ); if ($fix) { $phpcsFile->fixer->addContentBefore($nextArrow, ' '); } } else { - $spaceLength = $tokens[($nextArrow - 1)]['length']; + $spaceLength = $tokens[$nextArrow - 1]['length']; if (1 !== $spaceLength) { $fix = $phpcsFile->addFixableError( 'Expected 1 space between "%s" and double arrow; %s found', $nextArrow, 'SpaceBeforeDoubleArrow', - [$tokens[($nextArrow - 2)]['content'], $spaceLength] + [$tokens[$nextArrow - 2]['content'], $spaceLength] ); if ($fix) { - $phpcsFile->fixer->replaceToken(($nextArrow - 1), ' '); + $phpcsFile->fixer->replaceToken($nextArrow - 1, ' '); } } } - if (T_WHITESPACE !== $tokens[($nextArrow + 1)]['code']) { + if (T_WHITESPACE !== $tokens[$nextArrow + 1]['code']) { $fix = $phpcsFile->addFixableError( 'Expected 1 space between double arrow and "%s"; 0 found', $nextArrow, 'NoSpaceAfterDoubleArrow', - [$tokens[($nextArrow + 1)]['content']] + [$tokens[$nextArrow + 1]['content']] ); if ($fix) { $phpcsFile->fixer->addContent($nextArrow, ' '); } } else { - $spaceLength = $tokens[($nextArrow + 1)]['length']; + $spaceLength = $tokens[$nextArrow + 1]['length']; if (1 !== $spaceLength) { $fix = $phpcsFile->addFixableError( 'Expected 1 space between double arrow and "%s"; %s found', $nextArrow, 'SpaceAfterDoubleArrow', - [$tokens[($nextArrow + 2)]['content'], $spaceLength] + [$tokens[$nextArrow + 2]['content'], $spaceLength] ); if ($fix) { - $phpcsFile->fixer->replaceToken(($nextArrow + 1), ' '); + $phpcsFile->fixer->replaceToken($nextArrow + 1, ' '); } } } - $nextArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, ($nextArrow + 1), $end); + $nextArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $nextArrow + 1, $end); } if ($valueCount > 0) { // We have a multiple value array foreach ($commas as $comma) { - if (T_WHITESPACE !== $tokens[($comma + 1)]['code']) { + if (T_WHITESPACE !== $tokens[$comma + 1]['code']) { $fix = $phpcsFile->addFixableError( 'Expected 1 space between comma and "%s"; 0 found', $comma, 'NoSpaceAfterComma', - [$tokens[($comma + 1)]['content']] + [$tokens[$comma + 1]['content']] ); if ($fix) { $phpcsFile->fixer->addContent($comma, ' '); } } else { - $spaceLength = $tokens[($comma + 1)]['length']; + $spaceLength = $tokens[$comma + 1]['length']; if (1 !== $spaceLength) { $fix = $phpcsFile->addFixableError( 'Expected 1 space between comma and "%s"; %s found', $comma, 'SpaceAfterComma', - [$tokens[($comma + 2)]['content'], $spaceLength] + [$tokens[$comma + 2]['content'], $spaceLength] ); if ($fix) { - $phpcsFile->fixer->replaceToken(($comma + 1), ' '); + $phpcsFile->fixer->replaceToken($comma + 1, ' '); } } } - if (T_WHITESPACE === $tokens[($comma - 1)]['code']) { + if (T_WHITESPACE === $tokens[$comma - 1]['code']) { $fix = $phpcsFile->addFixableError( 'Expected 0 spaces between "%s" and comma; %s found', $comma, 'SpaceBeforeComma', - [$tokens[($comma - 2)]['content'], $tokens[($comma - 1)]['length']] + [$tokens[$comma - 2]['content'], $tokens[$comma - 1]['length']] ); if ($fix) { - $phpcsFile->fixer->replaceToken(($comma - 1), ''); + $phpcsFile->fixer->replaceToken($comma - 1, ''); } } } @@ -285,7 +287,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start } // Check the closing bracket is on a new line. - $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($end - 1), $start, true); + $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, $end - 1, $start, true); if ($tokens[$lastContent]['line'] === $tokens[$end]['line']) { $fix = $phpcsFile->addFixableError( 'Closing parenthesis of array declaration must be on a new line', @@ -299,7 +301,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start } elseif ($tokens[$end]['column'] !== $currentIndent + 1) { // Check the closing bracket is lined up under the "a" in array. $expected = $currentIndent; - $found = ($tokens[$end]['column'] - 1); + $found = $tokens[$end]['column'] - 1; $fix = $phpcsFile->addFixableError( 'Closing parenthesis not aligned correctly; expected %s space(s) but found %s', @@ -309,17 +311,17 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start ); if ($fix) { if (0 === $found) { - $phpcsFile->fixer->addContent(($end - 1), str_repeat(' ', $expected)); + $phpcsFile->fixer->addContent($end - 1, str_repeat(' ', $expected)); } else { - $phpcsFile->fixer->replaceToken(($end - 1), str_repeat(' ', $expected)); + $phpcsFile->fixer->replaceToken($end - 1, str_repeat(' ', $expected)); } } } - $keyUsed = false; + $keyUsed = false; $singleUsed = false; - $indices = []; - $maxLength = 0; + $indices = []; + $maxLength = 0; if (T_ARRAY === $tokens[$stackPtr]['code']) { $lastToken = $tokens[$stackPtr]['parenthesis_opener']; @@ -328,7 +330,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start } // Find all the double arrows that reside in this scope. - for ($nextToken = ($stackPtr + 1); $nextToken < $end; $nextToken++) { + for ($nextToken = $stackPtr + 1; $nextToken < $end; $nextToken++) { // Skip array or function calls switch ($tokens[$nextToken]['code']) { case T_ARRAY: @@ -382,7 +384,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start $valueContent = $phpcsFile->findNext( Tokens::$emptyTokens, - ($lastToken + 1), + $lastToken + 1, $nextToken, true ); @@ -402,22 +404,22 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start } if (T_COMMA === $tokens[$nextToken]['code'] - && T_WHITESPACE === $tokens[($nextToken - 1)]['code'] + && T_WHITESPACE === $tokens[$nextToken - 1]['code'] ) { - if ($tokens[($nextToken - 1)]['content'] === $phpcsFile->eolChar) { + if ($tokens[$nextToken - 1]['content'] === $phpcsFile->eolChar) { $spaceLength = 'newline'; } else { - $spaceLength = $tokens[($nextToken - 1)]['length']; + $spaceLength = $tokens[$nextToken - 1]['length']; } $fix = $phpcsFile->addFixableError( 'Expected 0 spaces between "%s" and comma; %s found', $nextToken, 'SpaceBeforeComma', - [$tokens[($nextToken - 2)]['content'], $spaceLength] + [$tokens[$nextToken - 2]['content'], $spaceLength] ); if ($fix) { - $phpcsFile->fixer->replaceToken(($nextToken - 1), ''); + $phpcsFile->fixer->replaceToken($nextToken - 1, ''); } } @@ -439,17 +441,17 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start $currentEntry['arrow'] = $nextToken; // Find the start of index that uses this double arrow. - $indexEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($nextToken - 1), $start, true); + $indexEnd = $phpcsFile->findPrevious(T_WHITESPACE, $nextToken - 1, $start, true); $indexStart = $phpcsFile->findStartOfStatement($indexEnd); if ($indexStart === $indexEnd) { - $currentEntry['index'] = $indexEnd; + $currentEntry['index'] = $indexEnd; $currentEntry['index_content'] = $tokens[$indexEnd]['content']; } else { - $currentEntry['index'] = $indexStart; + $currentEntry['index'] = $indexStart; $currentEntry['index_content'] = $phpcsFile->getTokensAsString( $indexStart, - ($indexEnd - $indexStart + 1) + $indexEnd - $indexStart + 1 ); } @@ -461,7 +463,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start // Find the value of this index. $nextContent = $phpcsFile->findNext( Tokens::$emptyTokens, - ($nextToken + 1), + $nextToken + 1, $end, true ); @@ -474,11 +476,11 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start $count = count($indices); if ($count > 0) { - $lastIndex = $indices[($count - 1)]['value']; + $lastIndex = $indices[$count - 1]['value']; $trailingContent = $phpcsFile->findPrevious( Tokens::$emptyTokens, - ($end - 1), + $end - 1, $lastIndex, true ); @@ -520,17 +522,17 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start 'ValueNoNewline' ); if ($fix) { - if (T_WHITESPACE === $tokens[($value['value'] - 1)]['code']) { - $phpcsFile->fixer->replaceToken(($value['value'] - 1), ''); + if (T_WHITESPACE === $tokens[$value['value'] - 1]['code']) { + $phpcsFile->fixer->replaceToken($value['value'] - 1, ''); } $phpcsFile->fixer->addNewlineBefore($value['value']); } - } elseif (T_WHITESPACE === $tokens[($value['value'] - 1)]['code']) { + } elseif (T_WHITESPACE === $tokens[$value['value'] - 1]['code']) { $expected = $currentIndent + $this->indent; $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $value['value'], true); - $found = ($tokens[$first]['column'] - 1); + $found = $tokens[$first]['column'] - 1; if ($found !== $expected) { $fix = $phpcsFile->addFixableError( 'Array value not aligned correctly; expected %s spaces but found %s', @@ -541,9 +543,9 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start if ($fix) { if (0 === $found) { - $phpcsFile->fixer->addContent(($value['value'] - 1), str_repeat(' ', $expected)); + $phpcsFile->fixer->addContent($value['value'] - 1, str_repeat(' ', $expected)); } else { - $phpcsFile->fixer->replaceToken(($value['value'] - 1), str_repeat(' ', $expected)); + $phpcsFile->fixer->replaceToken($value['value'] - 1, str_repeat(' ', $expected)); } } } @@ -579,9 +581,9 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start */ $numValues = count($indices); - $indicesStart = ($currentIndent + $this->indent + 1); - $arrowStart = ($indicesStart + $maxLength + 1); - $valueStart = ($arrowStart + 3); + $indicesStart = $currentIndent + $this->indent + 1; + $arrowStart = $indicesStart + $maxLength + 1; + $valueStart = $arrowStart + 3; $lastIndexLine = $tokens[$stackPtr]['line']; foreach ($indices as $index) { if (!isset($index['index'])) { @@ -603,7 +605,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start $lastIndex = $phpcsFile->findPrevious(T_COMMA, $index['index'] - 1, $lastIndexLine); $lastIndexLine = $lastIndex ? $tokens[$lastIndex]['line'] : false; - $indexLine = $tokens[$index['index']]['line']; + $indexLine = $tokens[$index['index']]['line']; if ($indexLine === $tokens[$stackPtr]['line']) { $fix = $phpcsFile->addFixableError( @@ -627,8 +629,8 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start ); if ($fix) { - if (T_WHITESPACE === $tokens[($index['index'] - 1)]['code']) { - $phpcsFile->fixer->replaceToken(($index['index'] - 1), ''); + if (T_WHITESPACE === $tokens[$index['index'] - 1]['code']) { + $phpcsFile->fixer->replaceToken($index['index'] - 1, ''); } $phpcsFile->fixer->addNewlineBefore($index['index']); @@ -650,9 +652,9 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start if ($fix) { if (0 === $found) { - $phpcsFile->fixer->addContent(($index['index'] - 1), str_repeat(' ', $expected)); + $phpcsFile->fixer->addContent($index['index'] - 1, str_repeat(' ', $expected)); } else { - $phpcsFile->fixer->replaceToken(($index['index'] - 1), str_repeat(' ', $expected)); + $phpcsFile->fixer->replaceToken($index['index'] - 1, str_repeat(' ', $expected)); } } @@ -678,18 +680,18 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start if ($fix) { if ('newline' === $found) { - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($index['value'] - 1), null, true); + $prev = $phpcsFile->findPrevious(T_WHITESPACE, $index['value'] - 1, null, true); $phpcsFile->fixer->beginChangeset(); - for ($i = ($prev + 1); $i < $index['value']; $i++) { + for ($i = $prev + 1; $i < $index['value']; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } - $phpcsFile->fixer->replaceToken(($index['value'] - 1), str_repeat(' ', $expected)); + $phpcsFile->fixer->replaceToken($index['value'] - 1, str_repeat(' ', $expected)); $phpcsFile->fixer->endChangeset(); } elseif (0 === $found) { - $phpcsFile->fixer->addContent(($index['arrow'] - 1), str_repeat(' ', $expected)); + $phpcsFile->fixer->addContent($index['arrow'] - 1, str_repeat(' ', $expected)); } else { - $phpcsFile->fixer->replaceToken(($index['arrow'] - 1), str_repeat(' ', $expected)); + $phpcsFile->fixer->replaceToken($index['arrow'] - 1, str_repeat(' ', $expected)); } } @@ -697,8 +699,8 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start } if ($tokens[$index['value']]['column'] !== $valueStart) { - $expected = ($valueStart - ($tokens[$index['arrow']]['length'] + $tokens[$index['arrow']]['column'])); - $found = $tokens[$index['value']]['column'] + $expected = $valueStart - ($tokens[$index['arrow']]['length'] + $tokens[$index['arrow']]['column']); + $found = $tokens[$index['value']]['column'] - ($tokens[$index['arrow']]['length'] + $tokens[$index['arrow']]['column']); if ($found < 0) { @@ -714,18 +716,18 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start if ($fix) { if ('newline' === $found) { - $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($index['value'] - 1), null, true); + $prev = $phpcsFile->findPrevious(T_WHITESPACE, $index['value'] - 1, null, true); $phpcsFile->fixer->beginChangeset(); - for ($i = ($prev + 1); $i < $index['value']; $i++) { + for ($i = $prev + 1; $i < $index['value']; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } - $phpcsFile->fixer->replaceToken(($index['value'] - 1), str_repeat(' ', $expected)); + $phpcsFile->fixer->replaceToken($index['value'] - 1, str_repeat(' ', $expected)); $phpcsFile->fixer->endChangeset(); } elseif (0 === $found) { - $phpcsFile->fixer->addContent(($index['value'] - 1), str_repeat(' ', $expected)); + $phpcsFile->fixer->addContent($index['value'] - 1, str_repeat(' ', $expected)); } else { - $phpcsFile->fixer->replaceToken(($index['value'] - 1), str_repeat(' ', $expected)); + $phpcsFile->fixer->replaceToken($index['value'] - 1, str_repeat(' ', $expected)); } } } diff --git a/SymfonyCustom/Sniffs/Classes/ClassDeclarationSniff.php b/SymfonyCustom/Sniffs/Classes/ClassDeclarationSniff.php index 09eda36..6c30077 100644 --- a/SymfonyCustom/Sniffs/Classes/ClassDeclarationSniff.php +++ b/SymfonyCustom/Sniffs/Classes/ClassDeclarationSniff.php @@ -1,5 +1,7 @@ numTokens - 1) === $tokens[$stackPtr]['comment_closer']) + && $phpcsFile->numTokens - 1 === $tokens[$stackPtr]['comment_closer']) ) { // Don't process an unfinished comment during live coding. return; @@ -41,7 +43,7 @@ public function process(File $phpcsFile, $stackPtr): void T_DOC_COMMENT_STAR, ]; - $short = $phpcsFile->findNext($empty, ($stackPtr + 1), $commentEnd, true); + $short = $phpcsFile->findNext($empty, $stackPtr + 1, $commentEnd, true); if (false === $short) { // No content at all. $next = $phpcsFile->findNext(T_WHITESPACE, $commentEnd + 1, null, true); @@ -87,7 +89,7 @@ public function process(File $phpcsFile, $stackPtr): void if ($fix) { $phpcsFile->fixer->beginChangeset(); - for ($i = ($stackPtr + 1); $i < $short; $i++) { + for ($i = $stackPtr + 1; $i < $short; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } $phpcsFile->fixer->addNewline($stackPtr); @@ -104,7 +106,7 @@ public function process(File $phpcsFile, $stackPtr): void } // Check for additional blank lines at the beginning of the comment. - if ($tokens[$stackPtr]['line'] < ($tokens[$short]['line'] - 1)) { + if ($tokens[$stackPtr]['line'] < $tokens[$short]['line'] - 1) { $fix = $phpcsFile->addFixableError( 'Additional blank lines found at beginning of doc comment', $stackPtr, @@ -113,8 +115,8 @@ public function process(File $phpcsFile, $stackPtr): void if ($fix) { $phpcsFile->fixer->beginChangeset(); - for ($i = ($stackPtr + 1); $i < $short; $i++) { - if ($tokens[($i + 1)]['line'] === $tokens[$short]['line']) { + for ($i = $stackPtr + 1; $i < $short; $i++) { + if ($tokens[$i + 1]['line'] === $tokens[$short]['line']) { break; } @@ -126,7 +128,7 @@ public function process(File $phpcsFile, $stackPtr): void } // The last line of the comment should just be the */ code. - $prev = $phpcsFile->findPrevious($empty, ($commentEnd - 1), $stackPtr, true); + $prev = $phpcsFile->findPrevious($empty, $commentEnd - 1, $stackPtr, true); if (!$isSingleLine && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) { $fix = $phpcsFile->addFixableError( 'The close comment tag must be the only content on the line', @@ -136,7 +138,7 @@ public function process(File $phpcsFile, $stackPtr): void if ($fix) { $phpcsFile->fixer->beginChangeset(); - for ($i = ($prev + 1); $i < $commentEnd; $i++) { + for ($i = $prev + 1; $i < $commentEnd; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } $phpcsFile->fixer->replaceToken( @@ -153,7 +155,7 @@ public function process(File $phpcsFile, $stackPtr): void } // Check for additional blank lines at the end of the comment. - if ($tokens[$prev]['line'] < ($tokens[$commentEnd]['line'] - 1)) { + if ($tokens[$prev]['line'] < $tokens[$commentEnd]['line'] - 1) { $fix = $phpcsFile->addFixableError( 'Additional blank lines found at end of doc comment', $commentEnd, @@ -162,8 +164,8 @@ public function process(File $phpcsFile, $stackPtr): void if ($fix) { $phpcsFile->fixer->beginChangeset(); - for ($i = ($prev + 1); $i < $commentEnd; $i++) { - if ($tokens[($i + 1)]['line'] === $tokens[$commentEnd]['line']) { + for ($i = $prev + 1; $i < $commentEnd; $i++) { + if ($tokens[$i + 1]['line'] === $tokens[$commentEnd]['line']) { break; } diff --git a/SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php b/SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php index fefbad4..a0b2232 100644 --- a/SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php +++ b/SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php @@ -1,5 +1,7 @@ getTokens(); - $find = Tokens::$methodPrefixes; + $find = Tokens::$methodPrefixes; $find[] = T_WHITESPACE; - $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); + $commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true); if (T_COMMENT === $tokens[$commentEnd]['code']) { // Inline comments might just be closing comments for control structures or functions // instead of function comments using the wrong comment type. // If there is other code on the line, assume they relate to that code. - $prev = $phpcsFile->findPrevious($find, ($commentEnd - 1), null, true); + $prev = $phpcsFile->findPrevious($find, $commentEnd - 1, null, true); if (false !== $prev && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) { $commentEnd = $prev; } @@ -46,7 +48,7 @@ public function process(File $phpcsFile, $stackPtr): void return; } - if (($tokens[$stackPtr]['line'] - 1) !== $tokens[$commentEnd]['line']) { + if ($tokens[$stackPtr]['line'] - 1 !== $tokens[$commentEnd]['line']) { $phpcsFile->addError( 'There must be no blank lines after the function comment', $commentEnd, @@ -183,9 +185,9 @@ protected function processThrows(File $phpcsFile, $stackPtr, $commentStart): voi if (null !== $throw) { $exception = null; - if (T_DOC_COMMENT_STRING === $tokens[($throw + 2)]['code']) { + if (T_DOC_COMMENT_STRING === $tokens[$throw + 2]['code']) { $matches = []; - preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($throw + 2)]['content'], $matches); + preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[$throw + 2]['content'], $matches); $exception = $matches[1]; } @@ -221,7 +223,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart): voi private function processWhitespace(File $phpcsFile, int $commentStart, bool $hasComment = true): void { $tokens = $phpcsFile->getTokens(); - $before = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true); + $before = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true); $startLine = $tokens[$commentStart]['line']; $prevLine = $tokens[$before]['line']; @@ -248,14 +250,14 @@ private function processWhitespace(File $phpcsFile, int $commentStart, bool $has if ($found > 1) { $phpcsFile->fixer->beginChangeset(); - for ($i = ($before + 1); $i < ($commentStart - 1); $i++) { + for ($i = $before + 1; $i < $commentStart - 1; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } $phpcsFile->fixer->endChangeset(); } else { // Try and maintain indentation. - if (T_WHITESPACE === $tokens[($commentStart - 1)]['code']) { + if (T_WHITESPACE === $tokens[$commentStart - 1]['code']) { $phpcsFile->fixer->addNewlineBefore($commentStart - 1); } else { $phpcsFile->fixer->addNewlineBefore($commentStart); diff --git a/SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php b/SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php index 3ae9f29..7dac343 100644 --- a/SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php +++ b/SymfonyCustom/Sniffs/Commenting/VariableCommentSniff.php @@ -1,5 +1,7 @@ findPrevious($ignore, ($stackPtr - 1), null, true); + $commentEnd = $phpcsFile->findPrevious($ignore, $stackPtr - 1, null, true); if (false === $commentEnd || (T_DOC_COMMENT_CLOSE_TAG !== $tokens[$commentEnd]['code'] && T_COMMENT !== $tokens[$commentEnd]['code']) @@ -135,7 +137,7 @@ protected function processVariableInString(File $phpcsFile, $stackPtr): void private function processWhitespace(File $phpcsFile, int $commentStart): void { $tokens = $phpcsFile->getTokens(); - $before = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true); + $before = $phpcsFile->findPrevious(T_WHITESPACE, $commentStart - 1, null, true); $startLine = $tokens[$commentStart]['line']; $prevLine = $tokens[$before]['line']; @@ -159,14 +161,14 @@ private function processWhitespace(File $phpcsFile, int $commentStart): void if ($found > 1) { $phpcsFile->fixer->beginChangeset(); - for ($i = ($before + 1); $i < ($commentStart - 1); $i++) { + for ($i = $before + 1; $i < $commentStart - 1; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } $phpcsFile->fixer->endChangeset(); } else { // Try and maintain indentation. - if (T_WHITESPACE === $tokens[($commentStart - 1)]['code']) { + if (T_WHITESPACE === $tokens[$commentStart - 1]['code']) { $phpcsFile->fixer->addNewlineBefore($commentStart - 1); } else { $phpcsFile->fixer->addNewlineBefore($commentStart); diff --git a/SymfonyCustom/Sniffs/Errors/ExceptionMessageSniff.php b/SymfonyCustom/Sniffs/Errors/ExceptionMessageSniff.php index 0fc5b5c..63d83c8 100644 --- a/SymfonyCustom/Sniffs/Errors/ExceptionMessageSniff.php +++ b/SymfonyCustom/Sniffs/Errors/ExceptionMessageSniff.php @@ -1,5 +1,7 @@ getTokens(); - $beginners = Tokens::$booleanOperators; + $beginners = Tokens::$booleanOperators; $beginners[] = T_IF; $beginners[] = T_ELSEIF; $beginners[] = T_EQUAL; @@ -65,12 +67,12 @@ public function process(File $phpcsFile, $stackPtr): void } // Check if this is a var to var comparison, e.g.: if ( $var1 == $var2 ). - $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true); if (isset(Tokens::$castTokens[$tokens[$nextNonEmpty]['code']])) { $nextNonEmpty = $phpcsFile->findNext( Tokens::$emptyTokens, - ($nextNonEmpty + 1), + $nextNonEmpty + 1, null, true ); @@ -79,7 +81,7 @@ public function process(File $phpcsFile, $stackPtr): void 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), + $nextNonEmpty + 1, null, true ); diff --git a/SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php b/SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php index 2a9d79a..cc1c630 100644 --- a/SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php +++ b/SymfonyCustom/Sniffs/Functions/ScopeOrderSniff.php @@ -1,5 +1,7 @@ findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $stackPtr + 1, null, true); return !$nextNonEmpty || T_NS_SEPARATOR !== $tokens[$nextNonEmpty]['code']; } diff --git a/SymfonyCustom/Sniffs/WhiteSpace/CloseBracketSpacingSniff.php b/SymfonyCustom/Sniffs/WhiteSpace/CloseBracketSpacingSniff.php index 41d4a36..a59582f 100644 --- a/SymfonyCustom/Sniffs/WhiteSpace/CloseBracketSpacingSniff.php +++ b/SymfonyCustom/Sniffs/WhiteSpace/CloseBracketSpacingSniff.php @@ -1,5 +1,7 @@ getTokens(); - if (isset($tokens[($stackPtr - 1)]) && T_WHITESPACE === $tokens[($stackPtr - 1)]['code']) { - $before = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); + 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), + $stackPtr - 1, 'ClosingWhitespace', [$tokens[$stackPtr]['content']] ); diff --git a/SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php b/SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php index b0c156c..4c1e9d3 100644 --- a/SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php +++ b/SymfonyCustom/Sniffs/WhiteSpace/DocCommentTagSpacingSniff.php @@ -1,5 +1,7 @@ getTokens(); - if (isset($tokens[($stackPtr - 1)]) - && $tokens[$stackPtr]['line'] === $tokens[($stackPtr - 1)]['line'] + if (isset($tokens[$stackPtr - 1]) + && $tokens[$stackPtr]['line'] === $tokens[$stackPtr - 1]['line'] ) { - if (T_DOC_COMMENT_WHITESPACE !== $tokens[($stackPtr - 1)]['code']) { + if (T_DOC_COMMENT_WHITESPACE !== $tokens[$stackPtr - 1]['code']) { $error = 'There should be a space before a doc comment tag "%s"'; $fix = $phpcsFile->addFixableError( $error, - ($stackPtr - 1), + $stackPtr - 1, 'DocCommentTagSpacing', [$tokens[$stackPtr]['content']] ); @@ -42,7 +44,7 @@ public function process(File $phpcsFile, $stackPtr): void if ($fix) { $phpcsFile->fixer->addContentBefore($stackPtr, ' '); } - } elseif (1 < $tokens[($stackPtr - 1)]['length']) { + } elseif (1 < $tokens[$stackPtr - 1]['length']) { $isCustomTag = !in_array($tokens[$stackPtr]['content'], SniffHelper::TAGS); // Custom tags are not checked cause there is annotation with array params @@ -50,7 +52,7 @@ public function process(File $phpcsFile, $stackPtr): void $error = 'There should be only one space before a doc comment tag "%s"'; $fix = $phpcsFile->addFixableError( $error, - ($stackPtr + 1), + $stackPtr + 1, 'DocCommentTagSpacing', [$tokens[$stackPtr]['content']] ); @@ -62,15 +64,15 @@ public function process(File $phpcsFile, $stackPtr): void } } - if (isset($tokens[($stackPtr + 1)]) - && $tokens[$stackPtr]['line'] === $tokens[($stackPtr + 1)]['line'] - && T_DOC_COMMENT_WHITESPACE === $tokens[($stackPtr + 1)]['code'] - && 1 < $tokens[($stackPtr + 1)]['length'] + if (isset($tokens[$stackPtr + 1]) + && $tokens[$stackPtr]['line'] === $tokens[$stackPtr + 1]['line'] + && T_DOC_COMMENT_WHITESPACE === $tokens[$stackPtr + 1]['code'] + && 1 < $tokens[$stackPtr + 1]['length'] ) { $error = 'There should be only one space after a doc comment tag "%s"'; $fix = $phpcsFile->addFixableError( $error, - ($stackPtr + 1), + $stackPtr + 1, 'DocCommentTagSpacing', [$tokens[$stackPtr]['content']] ); diff --git a/SymfonyCustom/Sniffs/WhiteSpace/EmptyLinesSniff.php b/SymfonyCustom/Sniffs/WhiteSpace/EmptyLinesSniff.php index 892ef69..0654cc4 100644 --- a/SymfonyCustom/Sniffs/WhiteSpace/EmptyLinesSniff.php +++ b/SymfonyCustom/Sniffs/WhiteSpace/EmptyLinesSniff.php @@ -1,5 +1,7 @@ getTokens(); - if (isset($tokens[($stackPtr + 1)]) - && T_WHITESPACE === $tokens[($stackPtr + 1)]['code'] - && false === strpos($tokens[($stackPtr + 1)]['content'], $phpcsFile->eolChar) + 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), + $stackPtr + 1, 'OpeningWhitespace', [$tokens[$stackPtr]['content']] ); diff --git a/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php b/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php index 670d675..89b2928 100644 --- a/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php +++ b/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php @@ -1,5 +1,7 @@ addFixableError( 'There must not be a single space before a unary operator statement', $stackPtr, @@ -45,7 +47,7 @@ public function process(File $phpcsFile, $stackPtr): void return; } - if (!$modifyLeft && substr($tokens[($stackPtr + 1)]['content'], 0, 1) !== '$') { + if (!$modifyLeft && substr($tokens[$stackPtr + 1]['content'], 0, 1) !== '$') { $fix = $phpcsFile->addFixableError( 'A unary operator statement must not be followed by a single space', $stackPtr, @@ -99,7 +101,7 @@ public function process(File $phpcsFile, $stackPtr): void // Check plus / minus value assignments or comparisons. if (T_MINUS === $tokens[$stackPtr]['code'] || T_PLUS === $tokens[$stackPtr]['code']) { - if (!$operatorSuffixAllowed && T_WHITESPACE === $tokens[($stackPtr + 1)]['code']) { + if (!$operatorSuffixAllowed && T_WHITESPACE === $tokens[$stackPtr + 1]['code']) { $fix = $phpcsFile->addFixableError( 'A unary operator statement must not be followed by a space', $stackPtr, diff --git a/SymfonyCustom/Tests/Arrays/ArrayDeclarationUnitTest.php b/SymfonyCustom/Tests/Arrays/ArrayDeclarationUnitTest.php index 0b01c7c..b7757c0 100644 --- a/SymfonyCustom/Tests/Arrays/ArrayDeclarationUnitTest.php +++ b/SymfonyCustom/Tests/Arrays/ArrayDeclarationUnitTest.php @@ -1,5 +1,7 @@ messages = []; - $this->files = []; - $this->totalNotices = 0; - $this->totalWarnings = 0; - $this->totalErrors = 0; - } + protected $totalErrors = 0; /** * @param SniffViolation $sniffViolation * - * @return $this + * @return self */ public function addMessage(SniffViolation $sniffViolation): Report { diff --git a/TwigCS/Report/SniffViolation.php b/TwigCS/Report/SniffViolation.php index 39fcf6a..0f9040b 100644 --- a/TwigCS/Report/SniffViolation.php +++ b/TwigCS/Report/SniffViolation.php @@ -1,5 +1,7 @@ level = $level; - $this->message = $message; - $this->line = $line; - $this->filename = $filename; - - $this->sniff = null; - $this->linePosition = null; + $this->level = $level; + $this->message = $message; + $this->line = $line; + $this->filename = $filename; } /** diff --git a/TwigCS/Report/TextFormatter.php b/TwigCS/Report/TextFormatter.php index 842f73f..3b0a70d 100644 --- a/TwigCS/Report/TextFormatter.php +++ b/TwigCS/Report/TextFormatter.php @@ -1,5 +1,7 @@ >'; - const ERROR_LINE_FORMAT = '%-5s| %s'; - const ERROR_CONTEXT_LIMIT = 2; - const ERROR_LINE_WIDTH = 120; + private const ERROR_CURSOR_CHAR = '>>'; + private const ERROR_LINE_FORMAT = '%-5s| %s'; + private const ERROR_CONTEXT_LIMIT = 2; + private const ERROR_LINE_WIDTH = 120; /** * @var SymfonyStyle @@ -50,7 +52,7 @@ public function display(Report $report, string $level = null): void $rows = []; foreach ($fileMessages as $message) { - $lines = $this->getContext(file_get_contents($file), $message->getLine(), $this::ERROR_CONTEXT_LIMIT); + $lines = $this->getContext(file_get_contents($file), $message->getLine(), self::ERROR_CONTEXT_LIMIT); $formattedText = []; if (!$message->getLine()) { @@ -58,7 +60,7 @@ public function display(Report $report, string $level = null): void } foreach ($lines as $no => $code) { - $formattedText[] = sprintf($this::ERROR_LINE_FORMAT, $no, wordwrap($code, $this::ERROR_LINE_WIDTH)); + $formattedText[] = sprintf(self::ERROR_LINE_FORMAT, $no, wordwrap($code, self::ERROR_LINE_WIDTH)); if ($no === $message->getLine()) { $formattedText[] = $this->formatErrorMessage($message); @@ -145,9 +147,9 @@ protected function getContext(string $template, int $line, int $context): array protected function formatErrorMessage(SniffViolation $message): string { return sprintf( - ''.$this::ERROR_LINE_FORMAT.'', - $this::ERROR_CURSOR_CHAR, - wordwrap($message->getMessage(), $this::ERROR_LINE_WIDTH) + ''.self::ERROR_LINE_FORMAT.'', + self::ERROR_CURSOR_CHAR, + wordwrap($message->getMessage(), self::ERROR_LINE_WIDTH) ); } } diff --git a/TwigCS/Ruleset/Generic/BlankEOFSniff.php b/TwigCS/Ruleset/Generic/BlankEOFSniff.php index debc7f6..0ecb29b 100644 --- a/TwigCS/Ruleset/Generic/BlankEOFSniff.php +++ b/TwigCS/Ruleset/Generic/BlankEOFSniff.php @@ -1,5 +1,7 @@ getContents(); - $tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer'); + $tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer'); $fixedFile = fopen($tempName, 'w'); fwrite($fixedFile, $contents); // We must use something like shell_exec() because whitespace at the end // of lines is critical to diff files. $filename = escapeshellarg($filename); - $cmd = "diff -u -L$filename -LPHP_CodeSniffer $filename \"$tempName\""; + $cmd = "diff -u -L$filename -LPHP_CodeSniffer $filename \"$tempName\""; $diff = shell_exec($cmd); @@ -202,7 +204,7 @@ public function generateDiff(string $filePath): string unlink($tempName); } - $diffLines = explode(PHP_EOL, $diff); + $diffLines = null !== $diff ? explode(PHP_EOL, $diff) : []; if (count($diffLines) === 1) { // Seems to be required for cygwin. $diffLines = explode("\n", $diff); @@ -265,7 +267,7 @@ public function beginChangeset(): void return; } - $this->changeset = []; + $this->changeset = []; $this->inChangeset = true; } diff --git a/TwigCS/Runner/Linter.php b/TwigCS/Runner/Linter.php index 050c7c1..ff5d2a4 100644 --- a/TwigCS/Runner/Linter.php +++ b/TwigCS/Runner/Linter.php @@ -1,5 +1,7 @@ currentPosition = 0; $this->tokens = []; $this->state = []; + $this->bracketsAndTernary = []; $this->code = str_replace(["\r\n", "\r"], "\n", $source->getCode()); $this->end = strlen($this->code); @@ -281,23 +284,23 @@ protected function lexExpression(): void $this->lexWhitespace(); } elseif (PHP_EOL === $currentToken) { $this->lexEOL(); - } elseif (preg_match($this->regexes['operator'], $this->code, $match, null, $this->cursor)) { + } elseif (preg_match($this->regexes['operator'], $this->code, $match, 0, $this->cursor)) { $this->lexOperator($match[0]); - } elseif (preg_match(self::REGEX_NAME, $this->code, $match, null, $this->cursor)) { + } elseif (preg_match(self::REGEX_NAME, $this->code, $match, 0, $this->cursor)) { // names $this->pushToken(Token::NAME_TYPE, $match[0]); $this->moveCursor($match[0]); - } elseif (preg_match(self::REGEX_NUMBER, $this->code, $match, null, $this->cursor)) { + } elseif (preg_match(self::REGEX_NUMBER, $this->code, $match, 0, $this->cursor)) { // numbers $number = (float) $match[0]; // floats if (ctype_digit($match[0]) && $number <= PHP_INT_MAX) { $number = (int) $match[0]; // integers lower than the maximum } - $this->pushToken(Token::NUMBER_TYPE, $number); + $this->pushToken(Token::NUMBER_TYPE, (string) $number); $this->moveCursor($match[0]); } elseif (false !== strpos(self::PUNCTUATION, $this->code[$this->cursor])) { $this->lexPunctuation(); - } elseif (preg_match(self::REGEX_STRING, $this->code, $match, null, $this->cursor)) { + } elseif (preg_match(self::REGEX_STRING, $this->code, $match, 0, $this->cursor)) { // strings $this->pushToken(Token::STRING_TYPE, addcslashes(stripcslashes($match[0]), '\\')); $this->moveCursor($match[0]); @@ -382,7 +385,7 @@ protected function lexData(int $limit = 0): void $this->lexWhitespace(); } elseif (PHP_EOL === $currentToken) { $this->lexEOL(); - } elseif (preg_match('/\S+/', $this->code, $match, null, $this->cursor)) { + } elseif (preg_match('/\S+/', $this->code, $match, 0, $this->cursor)) { $value = $match[0]; // Stop if cursor reaches the next token start. if (0 !== $limit && $limit <= ($this->cursor + strlen($value))) { diff --git a/TwigCS/Token/TokenizerHelper.php b/TwigCS/Token/TokenizerHelper.php index 7b40f03..6cab9c8 100644 --- a/TwigCS/Token/TokenizerHelper.php +++ b/TwigCS/Token/TokenizerHelper.php @@ -1,5 +1,7 @@ Date: Mon, 23 Dec 2019 02:19:11 +0100 Subject: [PATCH 2/2] :package: Start using some PSR12 rules --- .../UseWithoutStartingBackslashSniff.php | 72 ------------------- .../Objects/ObjectInstantiationSniff.php | 55 -------------- .../WhiteSpace/UnaryOperatorSpacingSniff.php | 38 +--------- .../UseWithoutStartingBackslashUnitTest.inc | 18 ----- ...WithoutStartingBackslashUnitTest.inc.fixed | 18 ----- .../UseWithoutStartingBackslashUnitTest.php | 35 --------- .../Objects/ObjectInstantiationUnitTest.inc | 31 -------- .../Objects/ObjectInstantiationUnitTest.php | 39 ---------- .../Tests/PHP/DiscourageEmptyUnitTest.php | 2 +- .../UnaryOperatorSpacingUnitTest.inc | 5 -- .../UnaryOperatorSpacingUnitTest.inc.fixed | 5 -- .../UnaryOperatorSpacingUnitTest.php | 4 +- SymfonyCustom/ruleset.xml | 25 +++++-- 13 files changed, 23 insertions(+), 324 deletions(-) delete mode 100644 SymfonyCustom/Sniffs/Namespaces/UseWithoutStartingBackslashSniff.php delete mode 100644 SymfonyCustom/Sniffs/Objects/ObjectInstantiationSniff.php delete mode 100644 SymfonyCustom/Tests/Namespaces/UseWithoutStartingBackslashUnitTest.inc delete mode 100644 SymfonyCustom/Tests/Namespaces/UseWithoutStartingBackslashUnitTest.inc.fixed delete mode 100644 SymfonyCustom/Tests/Namespaces/UseWithoutStartingBackslashUnitTest.php delete mode 100644 SymfonyCustom/Tests/Objects/ObjectInstantiationUnitTest.inc delete mode 100644 SymfonyCustom/Tests/Objects/ObjectInstantiationUnitTest.php diff --git a/SymfonyCustom/Sniffs/Namespaces/UseWithoutStartingBackslashSniff.php b/SymfonyCustom/Sniffs/Namespaces/UseWithoutStartingBackslashSniff.php deleted file mode 100644 index 15c11bc..0000000 --- a/SymfonyCustom/Sniffs/Namespaces/UseWithoutStartingBackslashSniff.php +++ /dev/null @@ -1,72 +0,0 @@ -getTokens(); - $classPtr = $phpcsFile->findNext( - Tokens::$emptyTokens, - $stackPtr + 1, - null, - true - ); - - $lowerContent = strtolower($tokens[$classPtr]['content']); - if ('function' === $lowerContent || 'const' === $lowerContent) { - $classPtr = $phpcsFile->findNext( - Tokens::$emptyTokens, - $classPtr + 1, - null, - true - ); - } - - if (T_NS_SEPARATOR === $tokens[$classPtr]['code'] - || (T_STRING === $tokens[$classPtr]['code'] - && '\\' === $tokens[$classPtr]['content']) - ) { - $fix = $phpcsFile->addFixableError( - 'Use statement cannot start with a backslash', - $classPtr, - 'BackslashAtStart' - ); - - if ($fix) { - if (T_WHITESPACE !== $tokens[$classPtr - 1]['code']) { - $phpcsFile->fixer->replaceToken($classPtr, ' '); - } else { - $phpcsFile->fixer->replaceToken($classPtr, ''); - } - } - } - } -} diff --git a/SymfonyCustom/Sniffs/Objects/ObjectInstantiationSniff.php b/SymfonyCustom/Sniffs/Objects/ObjectInstantiationSniff.php deleted file mode 100644 index b5d8802..0000000 --- a/SymfonyCustom/Sniffs/Objects/ObjectInstantiationSniff.php +++ /dev/null @@ -1,55 +0,0 @@ -getTokens(); - $allowed = [ - T_STRING, - T_NS_SEPARATOR, - T_VARIABLE, - T_STATIC, - T_SELF, - T_OPEN_SQUARE_BRACKET, - T_CLOSE_SQUARE_BRACKET, - ]; - - $object = $stackPtr; - $line = $tokens[$object]['line']; - - while ($object && $tokens[$object]['line'] === $line) { - $object = $phpcsFile->findNext($allowed, $object + 1); - - if ($tokens[$object]['line'] === $line && !in_array($tokens[$object + 1]['code'], $allowed)) { - if (T_OPEN_PARENTHESIS !== $tokens[$object + 1]['code']) { - $phpcsFile->addError('Use parentheses when instantiating classes', $stackPtr, 'Invalid'); - } - - break; - } - } - } -} diff --git a/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php b/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php index 89b2928..35384d5 100644 --- a/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php +++ b/SymfonyCustom/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php @@ -8,7 +8,7 @@ use PHP_CodeSniffer\Sniffs\Sniff; /** - * Ensures there are no spaces on ++/-- or on +/- sign operators or "!" boolean negators. + * Ensures there are no spaces +/- sign operators or "!" boolean negators. */ class UnaryOperatorSpacingSniff implements Sniff { @@ -17,7 +17,7 @@ class UnaryOperatorSpacingSniff implements Sniff */ public function register(): array { - return [T_DEC, T_INC, T_MINUS, T_PLUS, T_BOOLEAN_NOT]; + return [T_MINUS, T_PLUS, T_BOOLEAN_NOT]; } /** @@ -28,40 +28,6 @@ public function process(File $phpcsFile, $stackPtr): void { $tokens = $phpcsFile->getTokens(); - // Check decrement / increment. - if (T_DEC === $tokens[$stackPtr]['code'] || T_INC === $tokens[$stackPtr]['code']) { - $modifyLeft = substr($tokens[$stackPtr - 1]['content'], 0, 1) === '$' - || ';' === $tokens[$stackPtr + 1]['content']; - - if ($modifyLeft && T_WHITESPACE === $tokens[$stackPtr - 1]['code']) { - $fix = $phpcsFile->addFixableError( - 'There must not be a single space before a unary operator statement', - $stackPtr, - 'IncDecLeft' - ); - - if ($fix) { - $phpcsFile->fixer->replaceToken($stackPtr - 1, ''); - } - - return; - } - - if (!$modifyLeft && substr($tokens[$stackPtr + 1]['content'], 0, 1) !== '$') { - $fix = $phpcsFile->addFixableError( - 'A unary operator statement must not be followed by a single space', - $stackPtr, - 'IncDecRight' - ); - - if ($fix) { - $phpcsFile->fixer->replaceToken($stackPtr + 1, ''); - } - - return; - } - } - // Check "!" operator. if (T_BOOLEAN_NOT === $tokens[$stackPtr]['code'] && T_WHITESPACE === $tokens[$stackPtr + 1]['code']) { $fix = $phpcsFile->addFixableError( diff --git a/SymfonyCustom/Tests/Namespaces/UseWithoutStartingBackslashUnitTest.inc b/SymfonyCustom/Tests/Namespaces/UseWithoutStartingBackslashUnitTest.inc deleted file mode 100644 index 2c7c391..0000000 --- a/SymfonyCustom/Tests/Namespaces/UseWithoutStartingBackslashUnitTest.inc +++ /dev/null @@ -1,18 +0,0 @@ - 1, - 6 => 1, - 7 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/Tests/Objects/ObjectInstantiationUnitTest.inc b/SymfonyCustom/Tests/Objects/ObjectInstantiationUnitTest.inc deleted file mode 100644 index d2453bb..0000000 --- a/SymfonyCustom/Tests/Objects/ObjectInstantiationUnitTest.inc +++ /dev/null @@ -1,31 +0,0 @@ -foo); -new Foo\Bar(); -new Foo\Bar(true); -new Foo\Bar($this->foo); -new \Foo\Bar(); -new \Foo\Bar(true); -new \Foo\Bar($this->foo); -new $foo(); -new $foo(true); -new $foo($this->foo); -new static(); -new static(true); -new static($this->foo); -new self(); -new self(true); -new self($this->foo); diff --git a/SymfonyCustom/Tests/Objects/ObjectInstantiationUnitTest.php b/SymfonyCustom/Tests/Objects/ObjectInstantiationUnitTest.php deleted file mode 100644 index 52dbd73..0000000 --- a/SymfonyCustom/Tests/Objects/ObjectInstantiationUnitTest.php +++ /dev/null @@ -1,39 +0,0 @@ - 1, - 5 => 1, - 6 => 1, - 7 => 1, - 8 => 1, - 9 => 1, - 10 => 1, - ]; - } - - /** - * @return array - */ - protected function getWarningList(): array - { - return []; - } -} diff --git a/SymfonyCustom/Tests/PHP/DiscourageEmptyUnitTest.php b/SymfonyCustom/Tests/PHP/DiscourageEmptyUnitTest.php index 85cdbb1..2cec178 100644 --- a/SymfonyCustom/Tests/PHP/DiscourageEmptyUnitTest.php +++ b/SymfonyCustom/Tests/PHP/DiscourageEmptyUnitTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace SymfonyCustom\Tests\Objects; +namespace SymfonyCustom\Tests\PHP; use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; diff --git a/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc b/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc index 0378e5b..ffe2aa1 100644 --- a/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc +++ b/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc @@ -3,11 +3,6 @@ $a = !$b; $a = ! $b; -$a++; -$a ++; -++$a; -++ $a; - 1+-2; 1+- 2; 1+ -2; diff --git a/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc.fixed b/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc.fixed index 18efa57..fecdd2a 100644 --- a/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc.fixed +++ b/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.inc.fixed @@ -3,11 +3,6 @@ $a = !$b; $a = !$b; -$a++; -$a++; -++$a; -++$a; - 1+-2; 1+-2; 1+ -2; diff --git a/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.php b/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.php index f6c4b0b..9145bdc 100644 --- a/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.php +++ b/SymfonyCustom/Tests/WhiteSpace/UnaryOperatorSpacingUnitTest.php @@ -23,9 +23,7 @@ protected function getErrorList(): array 7 => 1, 9 => 1, 12 => 1, - 14 => 1, - 17 => 1, - 18 => 1, + 13 => 1, ]; } diff --git a/SymfonyCustom/ruleset.xml b/SymfonyCustom/ruleset.xml index 28c09ed..f4072b2 100755 --- a/SymfonyCustom/ruleset.xml +++ b/SymfonyCustom/ruleset.xml @@ -25,11 +25,6 @@ - - - 5 - - @@ -52,7 +47,6 @@ - @@ -110,6 +104,25 @@ + + + + + + + + + + + + + + + + 5 + + +