Skip to content

Commit e59e569

Browse files
committed
Merge branch 'php-8.0/squiz-scopekeywordspacing-support-cconstructor-prop-prom' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 6972a56 + d6f960b commit e59e569

File tree

4 files changed

+86
-16
lines changed

4 files changed

+86
-16
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: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,30 @@ public function fCreate($attributes = []): object|static
9494
{
9595
}
9696

97-
// Ensure that static as a scope keyword when preceeded by a colon which is not for a type dclaration is still handled.
97+
// Ensure that static as a scope keyword when preceeded by a colon which is not for a type declaration is still handled.
9898
$callback = $cond ? get_fn_name() : static function ($a) { return $a * 10; };
99+
100+
class TypedProperties {
101+
public
102+
int $var;
103+
104+
protected string $stringA, $stringB;
105+
106+
private bool
107+
$boolA,
108+
$boolB;
109+
}
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: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,29 @@ public function fCreate($attributes = []): object|static
8989
{
9090
}
9191

92-
// Ensure that static as a scope keyword when preceeded by a colon which is not for a type dclaration is still handled.
92+
// Ensure that static as a scope keyword when preceeded by a colon which is not for a type declaration is still handled.
9393
$callback = $cond ? get_fn_name() : static function ($a) { return $a * 10; };
94+
95+
class TypedProperties {
96+
public int $var;
97+
98+
protected string $stringA, $stringB;
99+
100+
private bool
101+
$boolA,
102+
$boolB;
103+
}
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: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,24 @@ class ScopeKeywordSpacingUnitTest extends AbstractSniffUnitTest
2626
public function getErrorList()
2727
{
2828
return [
29-
7 => 2,
30-
8 => 1,
31-
13 => 1,
32-
14 => 1,
33-
15 => 1,
34-
17 => 2,
35-
26 => 1,
36-
28 => 1,
37-
29 => 1,
38-
64 => 1,
39-
67 => 1,
40-
71 => 1,
41-
98 => 1,
29+
7 => 2,
30+
8 => 1,
31+
13 => 1,
32+
14 => 1,
33+
15 => 1,
34+
17 => 2,
35+
26 => 1,
36+
28 => 1,
37+
29 => 1,
38+
64 => 1,
39+
67 => 1,
40+
71 => 1,
41+
98 => 1,
42+
101 => 1,
43+
106 => 1,
44+
114 => 1,
45+
116 => 1,
46+
122 => 2,
4247
];
4348

4449
}//end getErrorList()

0 commit comments

Comments
 (0)