Skip to content

Commit dd309dc

Browse files
authored
Merge pull request #2 from php-collective/nullable
Fix WrongNullable false positive
2 parents 27484c3 + 83f123b commit dd309dc

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

PhpCollective/Traits/SignatureTrait.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,24 @@ protected function getMethodSignature(File $phpCsFile, int $stackPtr): array
5757
$typehint = substr($typehint, 1);
5858
}
5959

60+
// Determine if parameter accepts null (nullable or explicit null)
61+
$nullable = $parameter['nullable_type'];
62+
if ($nullable === false && $typehint !== '') {
63+
for ($i = $parameter['type_hint_token']; $i <= $parameter['type_hint_end_token']; $i++) {
64+
if ($tokens[$i]['code'] === T_NULL) {
65+
$nullable = true;
66+
67+
break;
68+
}
69+
}
70+
}
71+
6072
$arguments[] = [
6173
'variableIndex' => $parameter['token'],
6274
'variable' => $parameter['name'],
6375
'typehint' => $typehint,
6476
'typehintFull' => $parameter['type_hint'],
65-
'nullable' => $parameter['nullable_type'],
77+
'nullable' => $nullable,
6678
'defaultIndex' => $defaultIndex,
6779
'default' => $default,
6880
];

tests/_data/DocBlockParamAllowDefaultValue/after.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,14 @@ public function registerTableMapClass(string $tableMapClass): void
6767
public function toPHP(mixed $value): mixed {
6868
return $value;
6969
}
70+
71+
/**
72+
* This typehint is correct and should not change. See issue #368.
73+
*
74+
* @param int|string|null $value
75+
* @return void
76+
*/
77+
public function multipleUnion(int|string|null $value): void
78+
{
79+
}
7080
}

tests/_data/DocBlockParamAllowDefaultValue/before.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,14 @@ public function registerTableMapClass(string $tableMapClass): void
6767
public function toPHP(mixed $value): mixed {
6868
return $value;
6969
}
70+
71+
/**
72+
* This typehint is correct and should not change. See issue #368.
73+
*
74+
* @param int|string|null $value
75+
* @return void
76+
*/
77+
public function multipleUnion(int|string|null $value): void
78+
{
79+
}
7080
}

0 commit comments

Comments
 (0)