Skip to content

Commit ed77cc1

Browse files
xificurkondrejmirtes
authored andcommitted
UI\Component::getPresenter return type extension
1 parent c66d80c commit ed77cc1

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This extension provides following features:
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.
1515
* `Nette\ComponentModel\Component::lookup` return type based on `$throw` parameter.
16+
* `Nette\Application\UI\Component::getPresenter` return type based on `$throw` parameter.
1617
* Dynamic methods of [Nette\Utils\Html](https://doc.nette.org/en/2.4/html-elements)
1718
* Magic [Nette\Object and Nette\SmartObject](https://doc.nette.org/en/2.4/php-language-enhancements) properties
1819
* Event listeners through the `on*` properties

extension.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ services:
4343
tags:
4444
- phpstan.broker.dynamicMethodReturnTypeExtension
4545

46+
-
47+
class: PHPStan\Type\Nette\ComponentGetPresenterDynamicReturnTypeExtension
48+
tags:
49+
- phpstan.broker.dynamicMethodReturnTypeExtension
50+
4651
-
4752
class: PHPStan\Type\Nette\FormsBaseControlDynamicReturnTypeExtension
4853
tags:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Reflection\ParametersAcceptorSelector;
9+
use PHPStan\Type\Constant\ConstantBooleanType;
10+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
11+
use PHPStan\Type\Type;
12+
use PHPStan\Type\TypeCombinator;
13+
14+
final class ComponentGetPresenterDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
15+
{
16+
17+
public function getClass(): string
18+
{
19+
return 'Nette\Application\UI\Component';
20+
}
21+
22+
public function isMethodSupported(MethodReflection $methodReflection): bool
23+
{
24+
return $methodReflection->getName() === 'getPresenter';
25+
}
26+
27+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
28+
{
29+
$defaultReturnType = ParametersAcceptorSelector::selectSingle(
30+
$methodReflection->getVariants()
31+
)->getReturnType();
32+
if (count($methodCall->args) < 1) {
33+
return $defaultReturnType;
34+
}
35+
36+
$paramNeedExpr = $methodCall->args[0]->value;
37+
$paramNeedType = $scope->getType($paramNeedExpr);
38+
39+
if ($paramNeedType instanceof ConstantBooleanType) {
40+
if ($paramNeedType->getValue()) {
41+
return TypeCombinator::removeNull($defaultReturnType);
42+
}
43+
44+
return TypeCombinator::addNull($defaultReturnType);
45+
}
46+
47+
return $defaultReturnType;
48+
}
49+
50+
}

0 commit comments

Comments
 (0)