Skip to content

Commit 5161802

Browse files
committed
Component::lookup nullable when $need=false
1 parent fb1b40f commit 5161802

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This extension provides following features:
1212
* `Nette\ComponentModel\Container::getComponent()` knows type of the component because it reads the return type on `createComponent*` (this works best in presenters and controls)
1313
* `Nette\DI\Container::getByType` and `createInstance` return type based on first parameter (`Foo::class`).
1414
* `Nette\Forms\Container::getValues` return type based on `$asArray` parameter.
15+
* `Nette\ComponentModel\Component::lookup` return type based on `$need` parameter.
1516
* Dynamic methods of [Nette\Utils\Html](https://doc.nette.org/en/2.4/html-elements)
1617
* Magic [Nette\Object and Nette\SmartObject](https://doc.nette.org/en/2.4/php-language-enhancements) properties
1718
* Event listeners through the `on*` properties

extension.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ services:
3333
tags:
3434
- phpstan.broker.dynamicMethodReturnTypeExtension
3535

36+
-
37+
class: PHPStan\Type\Nette\ComponentLookupMethodReflectionExtension
38+
tags:
39+
- phpstan.broker.dynamicMethodReturnTypeExtension
40+
3641
-
3742
class: PHPStan\Type\Nette\FormsBaseControlDynamicReturnTypeExtension
3843
tags:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
17+
public function getClass(): string
18+
{
19+
return \Nette\ComponentModel\Component::class;
20+
}
21+
22+
public function isMethodSupported(MethodReflection $methodReflection): bool
23+
{
24+
return $methodReflection->getName() === 'lookup';
25+
}
26+
27+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
28+
{
29+
if (count($methodCall->args) > 1) {
30+
$paramNeedExpr = $methodCall->args[1]->value;
31+
$paramNeedType = $scope->getType($paramNeedExpr);
32+
33+
if ($paramNeedType instanceof ConstantBooleanType && $paramNeedType->getValue() === false) {
34+
if ($methodReflection->getReturnType()->accepts(new NullType()) === false) {
35+
return new UnionType([$methodReflection->getReturnType(), new NullType()]);
36+
}
37+
}
38+
}
39+
40+
return $methodReflection->getReturnType();
41+
}
42+
43+
}

0 commit comments

Comments
 (0)