Skip to content

Commit

Permalink
Merge pull request #289 from fredden/fixer-conflict/PSR-12/strict-typ…
Browse files Browse the repository at this point in the history
…es-parse-error

Improve context detection for `PSR12.Operators.OperatorSpacing` / `Squiz.WhiteSpace.OperatorSpacing` - avoids fixer conflict, bow out on parse error
  • Loading branch information
jrfnl authored Mar 5, 2024
2 parents 0f90d83 + 73ca839 commit f0a45e5
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 38 deletions.
18 changes: 17 additions & 1 deletion src/Standards/PSR12/Sniffs/Operators/OperatorSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function register()
$targets[] = T_STRING_CONCAT;
$targets[] = T_INSTANCEOF;

// Also register the contexts we want to specifically skip over.
$targets[] = T_DECLARE;

return $targets;

}//end register()
Expand All @@ -47,12 +50,25 @@ public function register()
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
* @return void|int Optionally returns a stack pointer. The sniff will not be
* called again on the current file until the returned stack
* pointer is reached. Return `$phpcsFile->numTokens` to skip
* the rest of the file.
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

// Skip over declare statements as those should be handled by different sniffs.
if ($tokens[$stackPtr]['code'] === T_DECLARE) {
if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
// Parse error / live coding.
return $phpcsFile->numTokens;
}

return $tokens[$stackPtr]['parenthesis_closer'];
}

if ($this->isOperator($phpcsFile, $stackPtr) === false) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ function setDefault(#[ImportValue(
{
// Do something
}

declare(strict_types=1);
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ function setDefault(#[ImportValue(
{
// Do something
}

declare(strict_types=1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
// Safeguard to ensure that sniff handles parse error/live coding correctly.
declare(strict_types=
53 changes: 30 additions & 23 deletions src/Standards/PSR12/Tests/Operators/OperatorSpacingUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,39 @@ final class OperatorSpacingUnitTest extends AbstractSniffUnitTest
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [
2 => 1,
3 => 2,
4 => 1,
5 => 2,
6 => 4,
9 => 3,
10 => 2,
11 => 3,
13 => 3,
14 => 2,
18 => 1,
20 => 1,
22 => 2,
23 => 2,
26 => 1,
37 => 4,
39 => 1,
40 => 1,
44 => 2,
47 => 2,
];
switch ($testFile) {
case 'OperatorSpacingUnitTest.1.inc':
return [
2 => 1,
3 => 2,
4 => 1,
5 => 2,
6 => 4,
9 => 3,
10 => 2,
11 => 3,
13 => 3,
14 => 2,
18 => 1,
20 => 1,
22 => 2,
23 => 2,
26 => 1,
37 => 4,
39 => 1,
40 => 1,
44 => 2,
47 => 2,
];
default:
return [];
}//end switch

}//end getErrorList()

Expand Down
27 changes: 22 additions & 5 deletions src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public function register()
$targets[] = T_INLINE_ELSE;
$targets[] = T_INSTANCEOF;

// Also register the contexts we want to specifically skip over.
$targets[] = T_DECLARE;

return $targets;

}//end register()
Expand All @@ -126,12 +129,25 @@ public function register()
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
* @return void|int Optionally returns a stack pointer. The sniff will not be
* called again on the current file until the returned stack
* pointer is reached. Return `$phpcsFile->numTokens` to skip
* the rest of the file.
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

// Skip over declare statements as those should be handled by different sniffs.
if ($tokens[$stackPtr]['code'] === T_DECLARE) {
if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
// Parse error / live coding.
return $phpcsFile->numTokens;
}

return $tokens[$stackPtr]['parenthesis_closer'];
}

if ($this->isOperator($phpcsFile, $stackPtr) === false) {
return;
}
Expand Down Expand Up @@ -327,11 +343,13 @@ protected function isOperator(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if ($tokens[$stackPtr]['code'] === T_DECLARE) {
return false;
}

// Skip default values in function declarations.
// Skip declare statements.
if ($tokens[$stackPtr]['code'] === T_EQUAL
|| $tokens[$stackPtr]['code'] === T_MINUS
) {
if ($tokens[$stackPtr]['code'] === T_EQUAL) {
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
$parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']);
$bracket = array_pop($parenthesis);
Expand All @@ -340,7 +358,6 @@ protected function isOperator(File $phpcsFile, $stackPtr)
if ($tokens[$function]['code'] === T_FUNCTION
|| $tokens[$function]['code'] === T_CLOSURE
|| $tokens[$function]['code'] === T_FN
|| $tokens[$function]['code'] === T_DECLARE
) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
// Safeguard to ensure that sniff handles parse error/live coding correctly.
declare(strict_types=
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class OperatorSpacingUnitTest extends AbstractSniffUnitTest
public function getErrorList($testFile='')
{
switch ($testFile) {
case 'OperatorSpacingUnitTest.inc':
case 'OperatorSpacingUnitTest.1.inc':
return [
4 => 1,
5 => 2,
Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Filters/AbstractFilterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ protected static function getFakeFileList()
$basedir.'/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php',
$basedir.'/src/Standards/Squiz/Tests',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc.fixed',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js.fixed',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php',
Expand Down
6 changes: 3 additions & 3 deletions tests/Core/Filters/GitModifiedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ public static function dataAcceptOnlyGitModified()
'.yamllint.yml',
'autoload.php',
'src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc.fixed',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js.fixed',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php',
Expand All @@ -191,7 +191,7 @@ public static function dataAcceptOnlyGitModified()
$basedir.'/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php',
$basedir.'/src/Standards/Squiz/Tests',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php',
],
Expand Down
6 changes: 3 additions & 3 deletions tests/Core/Filters/GitStagedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ public static function dataAcceptOnlyGitStaged()
'.yamllint.yml',
'autoload.php',
'src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc.fixed',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc.fixed',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js.fixed',
'src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php',
Expand All @@ -191,7 +191,7 @@ public static function dataAcceptOnlyGitStaged()
$basedir.'/src/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php',
$basedir.'/src/Standards/Squiz/Tests',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.1.inc',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.js',
$basedir.'/src/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.php',
],
Expand Down

0 comments on commit f0a45e5

Please sign in to comment.