Skip to content

Commit c328457

Browse files
committed
Fixed bug #2641 : PSR12.Functions.NullableTypeDeclaration false positive when using new static()
1 parent 451a256 commit c328457

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3636
- Fixed bug #2623 : PSR12.ControlStructures.ControlStructureSpacing not ignoring indentation inside multi-line string arguments
3737
- Fixed bug #2624 : PSR12.Traits.UseDeclaration doesnt apply the correct indent during auto fixing
3838
- Fixed bug #2626 : PSR12.Files.FileHeader detects @var annotations as file docblocks
39+
- Fixed bug #2641 : PSR12.Functions.NullableTypeDeclaration false positive when using new static()
3940
</notes>
4041
<contents>
4142
<dir name="/">

src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ class TestTokenizingOfNullableVsInlineThen {
6565
$test = Something::one(static::CONSTANT) ?: '';
6666
}
6767
}
68+
69+
$foo = new static(
70+
is_null($a) ? foo($a) : $a,
71+
is_null($b) ? $b : $c
72+
);

src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.inc.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,8 @@ class TestTokenizingOfNullableVsInlineThen {
6363
$test = Something::one(static::CONSTANT) ?: '';
6464
}
6565
}
66+
67+
$foo = new static(
68+
is_null($a) ? foo($a) : $a,
69+
is_null($b) ? $b : $c
70+
);

src/Tokenizers/PHP.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,10 @@ protected function tokenize($string)
986986
$tokenType = $tokens[$i];
987987
}
988988

989-
if ($tokenType === T_STATIC && $lastSeenNonEmpty === T_DOUBLE_COLON) {
989+
if ($tokenType === T_STATIC
990+
&& ($lastSeenNonEmpty === T_DOUBLE_COLON
991+
|| $lastSeenNonEmpty === '(')
992+
) {
990993
$lastSeenNonEmpty = $tokenType;
991994
continue;
992995
}
@@ -998,6 +1001,11 @@ protected function tokenize($string)
9981001
if ($tokenType === ':' || $tokenType === ',') {
9991002
$newToken['code'] = T_NULLABLE;
10001003
$newToken['type'] = 'T_NULLABLE';
1004+
1005+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1006+
echo "\t\t* token $stackPtr changed from ? to T_NULLABLE".PHP_EOL;
1007+
}
1008+
10011009
break;
10021010
}
10031011

@@ -1007,10 +1015,18 @@ protected function tokenize($string)
10071015
if ($tokenType === T_FUNCTION
10081016
|| isset(Util\Tokens::$methodPrefixes[$tokenType]) === true
10091017
) {
1018+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1019+
echo "\t\t* token $stackPtr changed from ? to T_NULLABLE".PHP_EOL;
1020+
}
1021+
10101022
$newToken['code'] = T_NULLABLE;
10111023
$newToken['type'] = 'T_NULLABLE';
10121024
break;
10131025
} else if (in_array($tokenType, [T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, '=', '{', ';'], true) === true) {
1026+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1027+
echo "\t\t* token $stackPtr changed from ? to T_INLINE_THEN".PHP_EOL;
1028+
}
1029+
10141030
$newToken['code'] = T_INLINE_THEN;
10151031
$newToken['type'] = 'T_INLINE_THEN';
10161032

0 commit comments

Comments
 (0)