Skip to content

Commit ace118c

Browse files
committed
Merge branch 'php-8.0/3188-squiz-scopekeywordspacing-bugfix' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 1a2dcf7 + cf69afb commit ace118c

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,33 @@ public function process(File $phpcsFile, $stackPtr)
5151
$prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
5252
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
5353

54-
if ($tokens[$stackPtr]['code'] === T_STATIC
55-
&& (($nextToken === false || $tokens[$nextToken]['code'] === T_DOUBLE_COLON)
56-
|| $tokens[$prevToken]['code'] === T_NEW)
57-
) {
58-
// Late static binding, e.g., static:: OR new static() usage or live coding.
59-
return;
60-
}
54+
if ($tokens[$stackPtr]['code'] === T_STATIC) {
55+
if (($nextToken === false || $tokens[$nextToken]['code'] === T_DOUBLE_COLON)
56+
|| $tokens[$prevToken]['code'] === T_NEW
57+
) {
58+
// Late static binding, e.g., static:: OR new static() usage or live coding.
59+
return;
60+
}
61+
62+
if ($prevToken !== false
63+
&& $tokens[$prevToken]['code'] === T_TYPE_UNION
64+
) {
65+
// Not a scope keyword, but a union return type.
66+
return;
67+
}
68+
69+
if ($prevToken !== false
70+
&& $tokens[$prevToken]['code'] === T_COLON
71+
) {
72+
$prevPrevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevToken - 1), null, true);
73+
if ($prevPrevToken !== false
74+
&& $tokens[$prevPrevToken]['code'] === T_CLOSE_PARENTHESIS
75+
) {
76+
// Not a scope keyword, but a return type.
77+
return;
78+
}
79+
}
80+
}//end if
6181

6282
if ($tokens[$prevToken]['code'] === T_AS) {
6383
// Trait visibility change, e.g., "use HelloWorld { sayHello as private; }".

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MyClass
2525

2626
public static$var = null;
2727

28-
public
28+
public
2929
static
3030
$var = null;
3131
}
@@ -82,3 +82,17 @@ class MyOtherClass
8282
$varS,
8383
$varT
8484
}
85+
86+
// Issue #3188 - static as return type.
87+
public static function fCreate($attributes = []): static
88+
{
89+
return static::factory()->create($attributes);
90+
}
91+
92+
// Also account for static used within union types.
93+
public function fCreate($attributes = []): object|static
94+
{
95+
}
96+
97+
// Ensure that static as a scope keyword when preceeded by a colon which is not for a type dclaration is still handled.
98+
$callback = $cond ? get_fn_name() : static function ($a) { return $a * 10; };

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,17 @@ class MyOtherClass
7777
$varS,
7878
$varT
7979
}
80+
81+
// Issue #3188 - static as return type.
82+
public static function fCreate($attributes = []): static
83+
{
84+
return static::factory()->create($attributes);
85+
}
86+
87+
// Also account for static used within union types.
88+
public function fCreate($attributes = []): object|static
89+
{
90+
}
91+
92+
// Ensure that static as a scope keyword when preceeded by a colon which is not for a type dclaration is still handled.
93+
$callback = $cond ? get_fn_name() : static function ($a) { return $a * 10; };

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function getErrorList()
3838
64 => 1,
3939
67 => 1,
4040
71 => 1,
41+
98 => 1,
4142
];
4243

4344
}//end getErrorList()

0 commit comments

Comments
 (0)