Describe the bug
Generic.WhiteSpace.ScopeIndent reports IncorrectExact false positives for any
statement wrapped in a nested scope (if, foreach, try, …) that lives inside
a PHP 8.4 property hook body. The same statements inside a regular method are
reported as correctly indented.
phpcbf then "fixes" the false positives by out-denting only the nested block,
leaving its sibling statements in the hook body untouched. The result is a file
with broken, internally inconsistent indentation.
The reported expected 4 spaces figure suggests the sniff does not register the
property hook's opening brace as a scope opener, so its expected-indent stack is
off for scope conditions nested inside the hook.
Code sample
<?php
class Foo
{
public int $bar {
set(int $value) {
if ($value < 1) {
$this->bar = 1;
}
}
}
}
Custom ruleset
None required. Reproduced with the bundled Generic standard and a single sniff:
<?xml version="1.0"?>
<ruleset name="repro">
<rule ref="Generic.WhiteSpace.ScopeIndent"/>
</ruleset>
To reproduce
- Save the code sample as
test.php.
- Run
phpcs -s --standard=Generic --sniffs=Generic.WhiteSpace.ScopeIndent test.php
FILE: /path/to/test.php
--------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
--------------------------------------------------------------------------------
7 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 12
| | (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
9 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 12
| | (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
--------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------
- Run
phpcbf --standard=Generic --sniffs=Generic.WhiteSpace.ScopeIndent test.php. The file becomes:
public int $bar {
set(int $value) {
if ($value < 1) {
$this->bar = 1;
}
}
}
Contrast (not flagged)
Identical body in a normal method produces no errors:
<?php
class Foo
{
public function setBar(int $value): void
{
if ($value < 1) {
$this->bar = 1;
}
}
}
Expected behavior
Generic.WhiteSpace.ScopeIndent should treat a property hook body as an indent
scope, the same way it treats a method body. For the code sample, the if line
and its closing brace are correctly indented at 12 spaces and no error should be
reported. phpcbf should leave the file unchanged.
Versions (please complete the following information)
|
|
| Operating System |
Alpine Linux v3.24 (official composer:2 Docker image) |
| PHP version |
8.5.9 |
| PHP_CodeSniffer version |
4.0.4; also reproduced on the 4.x branch (4.0.5) |
| Standard |
Generic (single sniff Generic.WhiteSpace.ScopeIndent) |
| Install type |
Composer (local) |
Additional context
- Reproduces for
get { … }, set { … } and set(Type $value) { … } full-body
hooks. Short-arrow hooks (get => expr;) and full bodies without a nested
scope are not affected.
- Discovered via a real class whose hooks validate the assigned value
(set(int $value) { if ($value < 1) { throw … } }). The destructive phpcbf
behavior is the more serious half of this report: running the fixer over such a
file corrupts it rather than skipping the unfixable case.
Please confirm
Describe the bug
Generic.WhiteSpace.ScopeIndentreportsIncorrectExactfalse positives for anystatement wrapped in a nested scope (
if,foreach,try, …) that lives insidea PHP 8.4 property hook body. The same statements inside a regular method are
reported as correctly indented.
phpcbfthen "fixes" the false positives by out-denting only the nested block,leaving its sibling statements in the hook body untouched. The result is a file
with broken, internally inconsistent indentation.
The reported
expected 4 spacesfigure suggests the sniff does not register theproperty hook's opening brace as a scope opener, so its expected-indent stack is
off for scope conditions nested inside the hook.
Code sample
Custom ruleset
None required. Reproduced with the bundled
Genericstandard and a single sniff:To reproduce
test.php.phpcs -s --standard=Generic --sniffs=Generic.WhiteSpace.ScopeIndent test.phpphpcbf --standard=Generic --sniffs=Generic.WhiteSpace.ScopeIndent test.php. The file becomes:Contrast (not flagged)
Identical body in a normal method produces no errors:
Expected behavior
Generic.WhiteSpace.ScopeIndentshould treat a property hook body as an indentscope, the same way it treats a method body. For the code sample, the
iflineand its closing brace are correctly indented at 12 spaces and no error should be
reported.
phpcbfshould leave the file unchanged.Versions (please complete the following information)
composer:2Docker image)4.xbranch (4.0.5)Generic.WhiteSpace.ScopeIndent)Additional context
get { … },set { … }andset(Type $value) { … }full-bodyhooks. Short-arrow hooks (
get => expr;) and full bodies without a nestedscope are not affected.
(
set(int $value) { if ($value < 1) { throw … } }). The destructivephpcbfbehavior is the more serious half of this report: running the fixer over such a
file corrupts it rather than skipping the unfixable case.
Please confirm
4.xbranch of PHP_CodeSniffer.