Skip to content

Commit

Permalink
PHP 8.0 | Squiz/ScopeKeywordSpacing: fix false positive on static as …
Browse files Browse the repository at this point in the history
…return type

This adds some additional safeguards to the sniff to prevent it from triggering when `static` is used in a return type declaration, as allowed since PHP 8.0.

Includes unit tests.

Fixes 3188
  • Loading branch information
jrfnl committed Dec 24, 2020
1 parent 9bcb29c commit cf69afb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
34 changes: 27 additions & 7 deletions src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,33 @@ public function process(File $phpcsFile, $stackPtr)
$prevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);

if ($tokens[$stackPtr]['code'] === T_STATIC
&& (($nextToken === false || $tokens[$nextToken]['code'] === T_DOUBLE_COLON)
|| $tokens[$prevToken]['code'] === T_NEW)
) {
// Late static binding, e.g., static:: OR new static() usage or live coding.
return;
}
if ($tokens[$stackPtr]['code'] === T_STATIC) {
if (($nextToken === false || $tokens[$nextToken]['code'] === T_DOUBLE_COLON)
|| $tokens[$prevToken]['code'] === T_NEW
) {
// Late static binding, e.g., static:: OR new static() usage or live coding.
return;
}

if ($prevToken !== false
&& $tokens[$prevToken]['code'] === T_TYPE_UNION
) {
// Not a scope keyword, but a union return type.
return;
}

if ($prevToken !== false
&& $tokens[$prevToken]['code'] === T_COLON
) {
$prevPrevToken = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevToken - 1), null, true);
if ($prevPrevToken !== false
&& $tokens[$prevPrevToken]['code'] === T_CLOSE_PARENTHESIS
) {
// Not a scope keyword, but a return type.
return;
}
}
}//end if

if ($tokens[$prevToken]['code'] === T_AS) {
// Trait visibility change, e.g., "use HelloWorld { sayHello as private; }".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MyClass

public static$var = null;

public
public
static
$var = null;
}
Expand Down Expand Up @@ -82,3 +82,17 @@ class MyOtherClass
$varS,
$varT
}

// Issue #3188 - static as return type.
public static function fCreate($attributes = []): static
{
return static::factory()->create($attributes);
}

// Also account for static used within union types.
public function fCreate($attributes = []): object|static
{
}

// Ensure that static as a scope keyword when preceeded by a colon which is not for a type dclaration is still handled.
$callback = $cond ? get_fn_name() : static function ($a) { return $a * 10; };
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,17 @@ class MyOtherClass
$varS,
$varT
}

// Issue #3188 - static as return type.
public static function fCreate($attributes = []): static
{
return static::factory()->create($attributes);
}

// Also account for static used within union types.
public function fCreate($attributes = []): object|static
{
}

// Ensure that static as a scope keyword when preceeded by a colon which is not for a type dclaration is still handled.
$callback = $cond ? get_fn_name() : static function ($a) { return $a * 10; };
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function getErrorList()
64 => 1,
67 => 1,
71 => 1,
98 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit cf69afb

Please sign in to comment.