Skip to content

Commit

Permalink
Fixed bug #3167 : Generic.WhiteSpace.ScopeIndent false positive when …
Browse files Browse the repository at this point in the history
…using PHP 8.0 constructor property promotion
  • Loading branch information
gsherwood committed Nov 18, 2020
1 parent 6f2a22d commit c5d48fc
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #2913 : Generic.WhiteSpace.ScopeIndent false positive when opening and closing tag on same line inside conditional
- Fixed bug #3157 : PSR2.ControlStructures.SwitchDeclaration.BreakIndent false positive when case keyword is not indented
- Fixed bug #3165 : Squiz.PHP.DisallowComparisonAssignment false positive when comparison inside closure
- Fixed bug #3167 : Generic.WhiteSpace.ScopeIndent false positive when using PHP 8.0 constructor property promotion
</notes>
<contents>
<dir name="/">
Expand Down
28 changes: 21 additions & 7 deletions src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,15 +856,29 @@ public function process(File $phpcsFile, $stackPtr)
&& $tokens[$next]['code'] !== T_VARIABLE
&& $tokens[$next]['code'] !== T_FN)
) {
if ($this->debug === true) {
$line = $tokens[$checkToken]['line'];
$type = $tokens[$checkToken]['type'];
echo "\t* method prefix ($type) found on line $line; indent set to exact *".PHP_EOL;
$isMethodPrefix = true;
if (isset($tokens[$checkToken]['nested_parenthesis']) === true) {
$parenthesis = array_keys($tokens[$checkToken]['nested_parenthesis']);
$deepestOpen = array_pop($parenthesis);
if (isset($tokens[$deepestOpen]['parenthesis_owner']) === true
&& $tokens[$tokens[$deepestOpen]['parenthesis_owner']]['code'] === T_FUNCTION
) {
// This is constructor property promotion and not a method prefix.
$isMethodPrefix = false;
}
}

$exact = true;
}
}
if ($isMethodPrefix === true) {
if ($this->debug === true) {
$line = $tokens[$checkToken]['line'];
$type = $tokens[$checkToken]['type'];
echo "\t* method prefix ($type) found on line $line; indent set to exact *".PHP_EOL;
}

$exact = true;
}
}//end if
}//end if

// JS property indentation has to be exact or else if will break
// things like function and object indentation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,14 @@ if (true) {
)?><?php
}

class Foo
{
public function __construct(
public int $intArg,
) {
}
}

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,14 @@ if (true) {
)?><?php
}

class Foo
{
public function __construct(
public int $intArg,
) {
}
}

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,14 @@ if (true) {
)?><?php
}

class Foo
{
public function __construct(
public int $intArg,
) {
}
}

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,14 @@ if (true) {
)?><?php
}

class Foo
{
public function __construct(
public int $intArg,
) {
}
}

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
1340 => 1,
1342 => 1,
1345 => 1,
1479 => 1,
1480 => 1,
1481 => 1,
1482 => 1,
1487 => 1,
1488 => 1,
1489 => 1,
1490 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit c5d48fc

Please sign in to comment.