Skip to content

Commit e34c1cd

Browse files
committed
fix: WhiteSpace\OperatorAndKeywordSpacingSniff - conflict with declare 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.
1 parent 710f71a commit e34c1cd

5 files changed

+35
-10
lines changed

src/WebimpressCodingStandard/Sniffs/WhiteSpace/OperatorAndKeywordSpacingSniff.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,30 @@ public function register() : array
4040
$tokens[] = T_INSTEADOF;
4141
$tokens[] = T_FN_ARROW;
4242

43+
// Also register the contexts we want to specifically skip over.
44+
$tokens[] = T_DECLARE;
45+
4346
return $tokens;
4447
}
4548

4649
/**
4750
* @param int $stackPtr
4851
*/
49-
public function process(File $phpcsFile, $stackPtr) : void
52+
public function process(File $phpcsFile, $stackPtr)
5053
{
5154
$tokens = $phpcsFile->getTokens();
5255

56+
// Skip over declare statements as those should be handled by different sniffs.
57+
if ($tokens[$stackPtr]['code'] === T_DECLARE) {
58+
if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
59+
// Parse error / live coding.
60+
return $phpcsFile->numTokens;
61+
}
62+
63+
return $tokens[$stackPtr]['parenthesis_closer'];
64+
}
65+
66+
5367
$originalValue = $this->ignoreNewlines;
5468
if (in_array($tokens[$stackPtr]['code'], $this->doNotIgnoreNewLineForTokens, true)) {
5569
$this->ignoreNewlines = false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Safeguard to ensure that sniff handles parse error/live coding correctly.
3+
declare(strict_types=

test/Sniffs/WhiteSpace/OperatorAndKeywordSpacingUnitTest.inc

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
$c
46
=
57
$a

test/Sniffs/WhiteSpace/OperatorAndKeywordSpacingUnitTest.inc.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
$c
46
= $a
57
+ $b;

test/Sniffs/WhiteSpace/OperatorAndKeywordSpacingUnitTest.php

+13-9
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ class OperatorAndKeywordSpacingUnitTest extends AbstractTestCase
1010
{
1111
protected function getErrorList(string $testFile = '') : array
1212
{
13+
if ($testFile === 'OperatorAndKeywordSpacingUnitTest.1.inc') {
14+
return [];
15+
}
16+
1317
return [
14-
4 => 1,
1518
6 => 1,
16-
11 => 2,
17-
12 => 2,
18-
17 => 2,
19-
21 => 1,
19+
8 => 1,
20+
13 => 2,
21+
14 => 2,
22+
19 => 2,
2023
23 => 1,
21-
27 => 1,
22-
31 => 1,
23-
35 => 1,
24-
39 => 2,
24+
25 => 1,
25+
29 => 1,
26+
33 => 1,
27+
37 => 1,
28+
41 => 2,
2529
];
2630
}
2731

0 commit comments

Comments
 (0)