Skip to content

PSR12.Files.DeclareStatement.SpaceFoundBeforeDirectiveValue #1402

@LLyaudet

Description

@LLyaudet

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:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs --report=code --standard=PSR12 --sniffs=PSR12.Files.DeclareStatement test.php
  3. 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

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the 4.x branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions