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 (webimpress#203)
  • Loading branch information
michalbundyra authored Oct 15, 2024
1 parent 710f71a commit 341543c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function in_array;

use const T_AS;
use const T_DECLARE;
use const T_FN_ARROW;
use const T_INSTANCEOF;
use const T_INSTEADOF;
Expand Down Expand Up @@ -40,16 +41,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
* @return null|int
*/
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 341543c

Please sign in to comment.