Skip to content

Commit d6f960b

Browse files
committed
PHP 8.0 | Squiz/ScopeKeywordSpacing: add support for constructor property 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.
1 parent f49df21 commit d6f960b

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,23 @@ public function process(File $phpcsFile, $stackPtr)
8484
return;
8585
}
8686

87-
if ($nextToken !== false && $tokens[$nextToken]['code'] === T_VARIABLE) {
87+
$isInFunctionDeclaration = false;
88+
if (empty($tokens[$stackPtr]['nested_parenthesis']) === false) {
89+
// Check if this is PHP 8.0 constructor property promotion.
90+
// In that case, we can't have multi-property definitions.
91+
$nestedParens = $tokens[$stackPtr]['nested_parenthesis'];
92+
$lastCloseParens = end($nestedParens);
93+
if (isset($tokens[$lastCloseParens]['parenthesis_owner']) === true
94+
&& $tokens[$tokens[$lastCloseParens]['parenthesis_owner']]['code'] === T_FUNCTION
95+
) {
96+
$isInFunctionDeclaration = true;
97+
}
98+
}
99+
100+
if ($nextToken !== false
101+
&& $tokens[$nextToken]['code'] === T_VARIABLE
102+
&& $isInFunctionDeclaration === false
103+
) {
88104
$endOfStatement = $phpcsFile->findNext(T_SEMICOLON, ($nextToken + 1));
89105
if ($endOfStatement === false) {
90106
// Live coding.

src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,17 @@ class TypedProperties {
107107
$boolA,
108108
$boolB;
109109
}
110+
111+
// PHP 8.0 constructor property promotion.
112+
class ConstructorPropertyPromotionTest {
113+
public function __construct(
114+
public $x = 0.0,
115+
protected $y = '',
116+
private $z = null,
117+
$normalParam,
118+
) {}
119+
}
120+
121+
class ConstructorPropertyPromotionWithTypesTest {
122+
public function __construct(protected float|int $x, public?string &$y = 'test', private mixed $z) {}
123+
}

src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.inc.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,17 @@ class TypedProperties {
101101
$boolA,
102102
$boolB;
103103
}
104+
105+
// PHP 8.0 constructor property promotion.
106+
class ConstructorPropertyPromotionTest {
107+
public function __construct(
108+
public $x = 0.0,
109+
protected $y = '',
110+
private $z = null,
111+
$normalParam,
112+
) {}
113+
}
114+
115+
class ConstructorPropertyPromotionWithTypesTest {
116+
public function __construct(protected float|int $x, public ?string &$y = 'test', private mixed $z) {}
117+
}

src/Standards/Squiz/Tests/WhiteSpace/ScopeKeywordSpacingUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public function getErrorList()
4141
98 => 1,
4242
101 => 1,
4343
106 => 1,
44+
114 => 1,
45+
116 => 1,
46+
122 => 2,
4447
];
4548

4649
}//end getErrorList()

0 commit comments

Comments
 (0)