Skip to content

Commit df6b9c0

Browse files
committed
Dynamic return type extension for fluent interfaces of form fields
1 parent e2503c4 commit df6b9c0

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"php": "~7.0",
99
"phpstan/phpstan": "^0.6",
1010
"nette/component-model": "^2.3.0",
11+
"nette/forms": "^2.3.0",
1112
"nette/utils": "^2.3.0"
1213
},
1314
"require-dev": {

extension.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ services:
2828
class: PHPStan\Type\Nette\ComponentModelDynamicReturnTypeExtension
2929
tags:
3030
- phpstan.broker.dynamicMethodReturnTypeExtension
31+
32+
-
33+
class: PHPStan\Type\Nette\FormsBaseControlDynamicReturnTypeExtension
34+
tags:
35+
- phpstan.broker.dynamicMethodReturnTypeExtension
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Type;
9+
10+
class FormsBaseControlDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
11+
{
12+
13+
public static function getClass(): string
14+
{
15+
return \Nette\Forms\Controls\BaseControl::class;
16+
}
17+
18+
public function isMethodSupported(
19+
MethodReflection $methodReflection
20+
): bool
21+
{
22+
return $methodReflection->getDeclaringClass()->getName() === \Nette\Forms\Controls\BaseControl::class;
23+
}
24+
25+
public function getTypeFromMethodCall(
26+
MethodReflection $methodReflection,
27+
MethodCall $methodCall,
28+
Scope $scope
29+
): Type
30+
{
31+
if ($methodReflection->getReturnType()->getClass() !== null && $methodReflection->getReturnType()->getClass() === \Nette\Forms\Controls\BaseControl::class) {
32+
return $scope->getType($methodCall->var);
33+
}
34+
35+
return $methodReflection->getReturnType();
36+
}
37+
38+
}

0 commit comments

Comments
 (0)