Skip to content

Commit c5d48fc

Browse files
committed
Fixed bug #3167 : Generic.WhiteSpace.ScopeIndent false positive when using PHP 8.0 constructor property promotion
1 parent 6f2a22d commit c5d48fc

File tree

7 files changed

+58
-11
lines changed

7 files changed

+58
-11
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
6464
- Fixed bug #2913 : Generic.WhiteSpace.ScopeIndent false positive when opening and closing tag on same line inside conditional
6565
- Fixed bug #3157 : PSR2.ControlStructures.SwitchDeclaration.BreakIndent false positive when case keyword is not indented
6666
- Fixed bug #3165 : Squiz.PHP.DisallowComparisonAssignment false positive when comparison inside closure
67+
- Fixed bug #3167 : Generic.WhiteSpace.ScopeIndent false positive when using PHP 8.0 constructor property promotion
6768
</notes>
6869
<contents>
6970
<dir name="/">

src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -856,15 +856,29 @@ public function process(File $phpcsFile, $stackPtr)
856856
&& $tokens[$next]['code'] !== T_VARIABLE
857857
&& $tokens[$next]['code'] !== T_FN)
858858
) {
859-
if ($this->debug === true) {
860-
$line = $tokens[$checkToken]['line'];
861-
$type = $tokens[$checkToken]['type'];
862-
echo "\t* method prefix ($type) found on line $line; indent set to exact *".PHP_EOL;
859+
$isMethodPrefix = true;
860+
if (isset($tokens[$checkToken]['nested_parenthesis']) === true) {
861+
$parenthesis = array_keys($tokens[$checkToken]['nested_parenthesis']);
862+
$deepestOpen = array_pop($parenthesis);
863+
if (isset($tokens[$deepestOpen]['parenthesis_owner']) === true
864+
&& $tokens[$tokens[$deepestOpen]['parenthesis_owner']]['code'] === T_FUNCTION
865+
) {
866+
// This is constructor property promotion and not a method prefix.
867+
$isMethodPrefix = false;
868+
}
863869
}
864870

865-
$exact = true;
866-
}
867-
}
871+
if ($isMethodPrefix === true) {
872+
if ($this->debug === true) {
873+
$line = $tokens[$checkToken]['line'];
874+
$type = $tokens[$checkToken]['type'];
875+
echo "\t* method prefix ($type) found on line $line; indent set to exact *".PHP_EOL;
876+
}
877+
878+
$exact = true;
879+
}
880+
}//end if
881+
}//end if
868882

869883
// JS property indentation has to be exact or else if will break
870884
// things like function and object indentation.

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,14 @@ if (true) {
14681468
)?><?php
14691469
}
14701470

1471+
class Foo
1472+
{
1473+
public function __construct(
1474+
public int $intArg,
1475+
) {
1476+
}
1477+
}
1478+
14711479
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
14721480
?>
14731481

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,14 @@ if (true) {
14681468
)?><?php
14691469
}
14701470

1471+
class Foo
1472+
{
1473+
public function __construct(
1474+
public int $intArg,
1475+
) {
1476+
}
1477+
}
1478+
14711479
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
14721480
?>
14731481

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,14 @@ if (true) {
14681468
)?><?php
14691469
}
14701470

1471+
class Foo
1472+
{
1473+
public function __construct(
1474+
public int $intArg,
1475+
) {
1476+
}
1477+
}
1478+
14711479
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
14721480
?>
14731481

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,14 @@ if (true) {
14681468
)?><?php
14691469
}
14701470

1471+
class Foo
1472+
{
1473+
public function __construct(
1474+
public int $intArg,
1475+
) {
1476+
}
1477+
}
1478+
14711479
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
14721480
?>
14731481

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
178178
1340 => 1,
179179
1342 => 1,
180180
1345 => 1,
181-
1479 => 1,
182-
1480 => 1,
183-
1481 => 1,
184-
1482 => 1,
181+
1487 => 1,
182+
1488 => 1,
183+
1489 => 1,
184+
1490 => 1,
185185
];
186186

187187
}//end getErrorList()

0 commit comments

Comments
 (0)