Describe the bug
PSR12.Files.DeclareStatement.SpaceFoundBeforeDirectiveValue yields a false positive when using
declare(encoding='ISO-8859-1');
Expected no space between equal sign and the directive value in a declare statement
Code sample
<?php
declare(encoding='ISO-8859-1');
$a = 1;
Custom ruleset
To reproduce
Steps to reproduce the behavior:
- Create a file called
test.php with the code sample above...
- Run
phpcs --report=code --standard=PSR12 --sniffs=PSR12.Files.DeclareStatement test.php
- See error message displayed
FILE: test.php
-----------------------------------------------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 3 LINES
-----------------------------------------------------------------------------------------------------------
LINE 1: ERROR Expected no space between the closing parenthesis and the semicolon in a declare statement
-----------------------------------------------------------------------------------------------------------
>> 1: <?php
2: declare(encoding='ISO-8859-1');
3: $a·=·1;
-----------------------------------------------------------------------------------------------------------
LINE 2: ERROR Expected no space between equal sign and the directive value in a declare statement
-----------------------------------------------------------------------------------------------------------
1: <?php
>> 2: declare(encoding='ISO-8859-1');
3: $a·=·1;
-----------------------------------------------------------------------------------------------------------
LINE 3: ERROR Expected no space between the directive value and closing parenthesis in a declare statement
-----------------------------------------------------------------------------------------------------------
1: <?php
2: declare(encoding='ISO-8859-1');
>> 3: $a·=·1;
-----------------------------------------------------------------------------------------------------------
Time: 28ms; Memory: 6MB
Expected behavior
None of these errors.
Versions (please complete the following information)
|
|
| Operating System |
Ubuntu 25.10 |
| PHP version |
8.4 |
| PHP_CodeSniffer version |
4.0.1 |
| Standard |
PSR12 |
| Install type |
composer global |
Additional context
Looking at the code here:
|
if ($equals !== false) { |
|
if ($tokens[($equals + 1)]['type'] !== 'T_LNUMBER') { |
|
$error = 'Expected no space between equal sign and the directive value in a declare statement'; |
|
|
|
if ($tokens[$value]['type'] === 'T_LNUMBER') { |
|
$fix = $phpcsFile->addFixableError($error, $value, 'SpaceFoundBeforeDirectiveValue'); |
|
|
|
if ($fix === true) { |
|
$phpcsFile->fixer->replaceToken(($equals + 1), ''); |
|
} |
|
} else { |
|
$phpcsFile->addError($error, $value, 'SpaceFoundBeforeDirectiveValue'); |
|
$value = $phpcsFile->findNext(T_LNUMBER, ($value + 1)); |
|
} |
|
} |
|
} |
It seems that a token LNUMBER is expected because tick and strict_types needs a number value.
But encoding doesn't and isn't supported yet.
Please confirm
Describe the bug
PSR12.Files.DeclareStatement.SpaceFoundBeforeDirectiveValue yields a false positive when using
Expected no space between equal sign and the directive value in a declare statement
Code sample
Custom ruleset
To reproduce
Steps to reproduce the behavior:
test.phpwith the code sample above...phpcs --report=code --standard=PSR12 --sniffs=PSR12.Files.DeclareStatement test.phpExpected behavior
None of these errors.
Versions (please complete the following information)
Additional context
Looking at the code here:
PHP_CodeSniffer/src/Standards/PSR12/Sniffs/Files/DeclareStatementSniff.php
Lines 119 to 134 in 9dfea15
It seems that a token LNUMBER is expected because tick and strict_types needs a number value.
But encoding doesn't and isn't supported yet.
Please confirm
4.xbranch of PHP_CodeSniffer.