Skip to content

Commit 90aea84

Browse files
authored
enforceNativeReturnTypehint fix false positive for unions with template type (#325)
1 parent d36092e commit 90aea84

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

src/Rule/EnforceNativeReturnTypehintRule.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private function getTypehintByType(
117117
bool $topLevel
118118
): ?string
119119
{
120-
if ($type instanceof MixedType) {
120+
if ($type instanceof MixedType || $this->isUnionTypeWithMixed($type)) {
121121
return $this->phpVersion->getVersionId() >= 80_000 ? 'mixed' : null;
122122
}
123123

@@ -351,4 +351,19 @@ private function alwaysThrowsException(ReturnStatementsNode $node): bool
351351
return $exitPoints !== [];
352352
}
353353

354+
private function isUnionTypeWithMixed(Type $type): bool
355+
{
356+
if (!$type instanceof UnionType) {
357+
return false;
358+
}
359+
360+
foreach ($type->getTypes() as $innerType) {
361+
if ($innerType instanceof MixedType) {
362+
return true;
363+
}
364+
}
365+
366+
return false;
367+
}
368+
354369
}

tests/Rule/data/EnforceNativeReturnTypehintRule/code-74.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ public function requireMixed3() {}
8080
/** @return mixed|int|null */
8181
public function requireMixed4() {}
8282

83+
/**
84+
* @template T
85+
* @param callable(): T $function
86+
* @return T|false
87+
*/
88+
public function requireMixed5(callable $function) {}
89+
8390
/** @return void */
8491
public function requireVoid() {} // error: Missing native return typehint void
8592

tests/Rule/data/EnforceNativeReturnTypehintRule/code-80.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ public function requireMixed3() {} // error: Missing native return typehint mixe
8080
/** @return mixed|int|null */
8181
public function requireMixed4() {} // error: Missing native return typehint mixed
8282

83+
/**
84+
* @template T
85+
* @param callable(): T $function
86+
* @return T|false
87+
*/
88+
public function requireMixed5(callable $function) {} // error: Missing native return typehint mixed
89+
8390
/** @return void */
8491
public function requireVoid() {} // error: Missing native return typehint void
8592

tests/Rule/data/EnforceNativeReturnTypehintRule/code-81.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ public function requireMixed3() {} // error: Missing native return typehint mixe
8080
/** @return mixed|int|null */
8181
public function requireMixed4() {} // error: Missing native return typehint mixed
8282

83+
/**
84+
* @template T
85+
* @param callable(): T $function
86+
* @return T|false
87+
*/
88+
public function requireMixed5(callable $function) {} // error: Missing native return typehint mixed
89+
8390
/** @return void */
8491
public function requireVoid() {} // error: Missing native return typehint void
8592

tests/Rule/data/EnforceNativeReturnTypehintRule/code-82.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ public function requireMixed3() {} // error: Missing native return typehint mixe
8080
/** @return mixed|int|null */
8181
public function requireMixed4() {} // error: Missing native return typehint mixed
8282

83+
/**
84+
* @template T
85+
* @param callable(): T $function
86+
* @return T|false
87+
*/
88+
public function requireMixed5(callable $function) {} // error: Missing native return typehint mixed
89+
8390
/** @return void */
8491
public function requireVoid() {} // error: Missing native return typehint void
8592

0 commit comments

Comments
 (0)