Skip to content

Commit 036af15

Browse files
authored
Create ComponentLookupMethodReflectionExtension.php
1 parent 194171d commit 036af15

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Nette;
4+
5+
use PhpParser\Node\Expr\MethodCall;
6+
use PHPStan\Analyser\Scope;
7+
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Type\Constant\ConstantBooleanType;
9+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
10+
use PHPStan\Type\NullType;
11+
use PHPStan\Type\Type;
12+
use PHPStan\Type\UnionType;
13+
14+
final class ComponentLookupMethodReflectionExtension implements DynamicMethodReturnTypeExtension
15+
{
16+
public function getClass(): string
17+
{
18+
return \Nette\ComponentModel\Component::class;
19+
}
20+
21+
public function isMethodSupported(MethodReflection $methodReflection): bool
22+
{
23+
return $methodReflection->getName() === 'lookup';
24+
}
25+
26+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
27+
{
28+
if (count($methodCall->args) > 1) {
29+
$paramNeedExpr = $methodCall->args[1]->value;
30+
$paramNeedType = $scope->getType($paramNeedExpr);
31+
32+
if ($paramNeedType instanceof ConstantBooleanType && $paramNeedType->getValue() === false) {
33+
if ($methodReflection->getReturnType()->accepts(new NullType()) === false) {
34+
return new UnionType([$methodReflection->getReturnType(), new NullType()]);
35+
}
36+
}
37+
}
38+
39+
return $methodReflection->getReturnType();
40+
}
41+
42+
}

0 commit comments

Comments
 (0)