Skip to content

Commit 1442bf2

Browse files
adaamzondrejmirtes
authored andcommitted
Presenter::getSession Return type extension (#19)
1 parent fb1b40f commit 1442bf2

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

extension.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ services:
3838
tags:
3939
- phpstan.broker.dynamicMethodReturnTypeExtension
4040

41+
-
42+
class: PHPStan\Type\Nette\PresenterGetSessionReturnTypeExtension
43+
tags:
44+
- phpstan.broker.dynamicMethodReturnTypeExtension
45+
4146
-
4247
class: PHPStan\Type\Nette\ServiceLocatorDynamicReturnTypeExtension
4348
tags:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Nette;
4+
5+
use Nette\Http\Session;
6+
use Nette\Http\SessionSection;
7+
use PhpParser\Node\Expr\MethodCall;
8+
use PHPStan\Analyser\Scope;
9+
use PHPStan\Reflection\MethodReflection;
10+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
11+
use PHPStan\Type\NullType;
12+
use PHPStan\Type\ObjectType;
13+
use PHPStan\Type\Type;
14+
15+
final class PresenterGetSessionReturnTypeExtension implements DynamicMethodReturnTypeExtension
16+
{
17+
public function getClass(): string
18+
{
19+
return \Nette\Application\UI\Presenter::class;
20+
}
21+
22+
public function isMethodSupported(MethodReflection $methodReflection): bool
23+
{
24+
return $methodReflection->getName() === 'getSession';
25+
}
26+
27+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
28+
{
29+
if (count($methodCall->args) === 0 || $scope->getType($methodCall->args[0]->value) instanceof NullType) {
30+
return new ObjectType(Session::class);
31+
}
32+
33+
return new ObjectType(SessionSection::class);
34+
}
35+
}

0 commit comments

Comments
 (0)