Skip to content

Commit a920b42

Browse files
staabmclxmstaab
andauthored
NoDynamicNameRule: Fix union type handling (#212)
Co-authored-by: Markus Staab <[email protected]>
1 parent 1400703 commit a920b42

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/TypeAnalyzer/CallableTypeAnalyzer.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ public function isClosureOrCallableType(Scope $scope, Expr $expr): bool
2020
$nameStaticType = $scope->getType($expr);
2121
$unwrappedNameStaticType = TypeCombinator::removeNull($nameStaticType);
2222

23-
if ($unwrappedNameStaticType instanceof CallableType) {
24-
return true;
25-
}
26-
27-
if ($unwrappedNameStaticType instanceof ClosureType) {
23+
if ($unwrappedNameStaticType->isCallable()->yes()) {
2824
return true;
2925
}
3026

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Tests\Rules\NoDynamicNameRule\Fixture;
6+
7+
use Closure;
8+
9+
final class SkipCallableUnion
10+
{
11+
/** @var array|callable():array */
12+
private $sometimesCallable = [];
13+
14+
public function run()
15+
{
16+
$sometimesCallable = $this->sometimesCallable;
17+
if (is_callable($sometimesCallable)) {
18+
$sometimesCallable();
19+
}
20+
}
21+
}

tests/Rules/NoDynamicNameRule/NoDynamicNameRuleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static function provideData(): Iterator
3535
yield [__DIR__ . '/Fixture/SkipInvokable.php', []];
3636
yield [__DIR__ . '/Fixture/SkipClosure.php', []];
3737
yield [__DIR__ . '/Fixture/SkipCallable.php', []];
38+
yield [__DIR__ . '/Fixture/SkipCallableUnion.php', []];
3839
yield [__DIR__ . '/Fixture/SkipNullableClosure.php', []];
3940
yield [__DIR__ . '/Fixture/SkipImmediatelyInvokedFunctionExpression.php', []];
4041
}

0 commit comments

Comments
 (0)