Skip to content

Commit a5578a2

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

File tree

6 files changed

+57
-11
lines changed

6 files changed

+57
-11
lines changed

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -740,15 +740,29 @@ public function process(File $phpcsFile, $stackPtr)
740740
&& $tokens[$next]['code'] !== T_VARIABLE
741741
&& $tokens[$next]['code'] !== T_FN)
742742
) {
743-
if ($this->debug === true) {
744-
$line = $tokens[$checkToken]['line'];
745-
$type = $tokens[$checkToken]['type'];
746-
Common::printStatusMessage("* method prefix ($type) found on line $line; indent set to exact *", 1);
743+
$isMethodPrefix = true;
744+
if (isset($tokens[$checkToken]['nested_parenthesis']) === true) {
745+
$parenthesis = array_keys($tokens[$checkToken]['nested_parenthesis']);
746+
$deepestOpen = array_pop($parenthesis);
747+
if (isset($tokens[$deepestOpen]['parenthesis_owner']) === true
748+
&& $tokens[$tokens[$deepestOpen]['parenthesis_owner']]['code'] === T_FUNCTION
749+
) {
750+
// This is constructor property promotion and not a method prefix.
751+
$isMethodPrefix = false;
752+
}
747753
}
748754

749-
$exact = true;
750-
}
751-
}
755+
if ($isMethodPrefix === true) {
756+
if ($this->debug === true) {
757+
$line = $tokens[$checkToken]['line'];
758+
$type = $tokens[$checkToken]['type'];
759+
Common::printStatusMessage("* method prefix ($type) found on line $line; indent set to exact *", 1);
760+
}
761+
762+
$exact = true;
763+
}
764+
}//end if
765+
}//end if
752766

753767
// Open PHP tags needs to be indented to exact column positions
754768
// so they don't cause problems with indent checks for the code

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
@@ -151,10 +151,10 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
151151
1340 => 1,
152152
1342 => 1,
153153
1345 => 1,
154-
1479 => 1,
155-
1480 => 1,
156-
1481 => 1,
157-
1482 => 1,
154+
1487 => 1,
155+
1488 => 1,
156+
1489 => 1,
157+
1490 => 1,
158158
];
159159

160160
}//end getErrorList()

0 commit comments

Comments
 (0)