Skip to content

Commit

Permalink
fix: WhiteSpace\OperatorAndKeywordSpacingSniff - conflict with declar…
Browse files Browse the repository at this point in the history
…e statement since PHP_CodeSniffer 3.9.1

PR PHPCSStandards/PHP_CodeSniffer#289 changes a lot around operator sniff, and now declare statement must be omitted (as it was done also for PSR-12 rule there). There is another sniff to verify declare statement, and we do not want to check it here.
  • Loading branch information
michalbundyra committed Oct 15, 2024
1 parent 710f71a commit e34c1cd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,30 @@ public function register() : array
$tokens[] = T_INSTEADOF;
$tokens[] = T_FN_ARROW;

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

return $tokens;
}

/**
* @param int $stackPtr
*/
public function process(File $phpcsFile, $stackPtr) : void
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'];
}


$originalValue = $this->ignoreNewlines;
if (in_array($tokens[$stackPtr]['code'], $this->doNotIgnoreNewLineForTokens, true)) {
$this->ignoreNewlines = 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=
2 changes: 2 additions & 0 deletions test/Sniffs/WhiteSpace/OperatorAndKeywordSpacingUnitTest.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

$c
=
$a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

$c
= $a
+ $b;
Expand Down
22 changes: 13 additions & 9 deletions test/Sniffs/WhiteSpace/OperatorAndKeywordSpacingUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ class OperatorAndKeywordSpacingUnitTest extends AbstractTestCase
{
protected function getErrorList(string $testFile = '') : array
{
if ($testFile === 'OperatorAndKeywordSpacingUnitTest.1.inc') {
return [];
}

return [
4 => 1,
6 => 1,
11 => 2,
12 => 2,
17 => 2,
21 => 1,
8 => 1,
13 => 2,
14 => 2,
19 => 2,
23 => 1,
27 => 1,
31 => 1,
35 => 1,
39 => 2,
25 => 1,
29 => 1,
33 => 1,
37 => 1,
41 => 2,
];
}

Expand Down

0 comments on commit e34c1cd

Please sign in to comment.