Skip to content

Commit

Permalink
PHP 8.0 | Squiz/ScopeKeywordSpacing: add support for constructor prop…
Browse files Browse the repository at this point in the history
…erty promotion

Prevent the sniff from ignoring the spacing after the scope keyword in case of PHP 8.0 constructor property promotion.

As it was, the sniff would presume "normal" property syntax, which meant that in the case of constructor property promotion in a multi-line constructor declaration, the sniff would look for a semicolon to end the statement and bow out when the semicolon wasn't found.

By explicitly checking for constructor property promotion and skipping the next above mentioned check in that case, we ensure that scope keywords in constructors are still handled correctly.

Includes tests.
  • Loading branch information
jrfnl committed Mar 5, 2021
1 parent f49df21 commit d6f960b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,23 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

if ($nextToken !== false && $tokens[$nextToken]['code'] === T_VARIABLE) {
$isInFunctionDeclaration = false;
if (empty($tokens[$stackPtr]['nested_parenthesis']) === false) {
// Check if this is PHP 8.0 constructor property promotion.
// In that case, we can't have multi-property definitions.
$nestedParens = $tokens[$stackPtr]['nested_parenthesis'];
$lastCloseParens = end($nestedParens);
if (isset($tokens[$lastCloseParens]['parenthesis_owner']) === true
&& $tokens[$tokens[$lastCloseParens]['parenthesis_owner']]['code'] === T_FUNCTION
) {
$isInFunctionDeclaration = true;
}
}

if ($nextToken !== false
&& $tokens[$nextToken]['code'] === T_VARIABLE
&& $isInFunctionDeclaration === false
) {
$endOfStatement = $phpcsFile->findNext(T_SEMICOLON, ($nextToken + 1));
if ($endOfStatement === false) {
// Live coding.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,17 @@ class TypedProperties {
$boolA,
$boolB;
}

// PHP 8.0 constructor property promotion.
class ConstructorPropertyPromotionTest {
public function __construct(
public $x = 0.0,
protected $y = '',
private $z = null,
$normalParam,
) {}
}

class ConstructorPropertyPromotionWithTypesTest {
public function __construct(protected float|int $x, public?string &$y = 'test', private mixed $z) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,17 @@ class TypedProperties {
$boolA,
$boolB;
}

// PHP 8.0 constructor property promotion.
class ConstructorPropertyPromotionTest {
public function __construct(
public $x = 0.0,
protected $y = '',
private $z = null,
$normalParam,
) {}
}

class ConstructorPropertyPromotionWithTypesTest {
public function __construct(protected float|int $x, public ?string &$y = 'test', private mixed $z) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public function getErrorList()
98 => 1,
101 => 1,
106 => 1,
114 => 1,
116 => 1,
122 => 2,
];

}//end getErrorList()
Expand Down

0 comments on commit d6f960b

Please sign in to comment.