Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: WhiteSpace\OperatorAndKeywordSpacingSniff - conflict with declare statement since PHP_CodeSniffer 3.9.1 #203

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading