Skip to content

Commit 4930ff1

Browse files
spazeondrejmirtes
authored andcommitted
Dynamic return type extension for getUnsafeValues added in nette/forms 3.1.2
getUnsafeValues unlike getValues requires a parameter and doesn't support booleans so I went with a new class instead of multiple ifs.
1 parent 14dad5b commit 4930ff1

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

extension.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ services:
8585
tags:
8686
- phpstan.broker.dynamicMethodReturnTypeExtension
8787

88+
-
89+
class: PHPStan\Type\Nette\FormContainerUnsafeValuesDynamicReturnTypeExtension
90+
tags:
91+
- phpstan.broker.dynamicMethodReturnTypeExtension
92+
8893
-
8994
class: PHPStan\Type\Nette\FormContainerValuesDynamicReturnTypeExtension
9095
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\ArrayType;
10+
use PHPStan\Type\Constant\ConstantStringType;
11+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
12+
use PHPStan\Type\MixedType;
13+
use PHPStan\Type\NullType;
14+
use PHPStan\Type\ObjectType;
15+
use PHPStan\Type\StringType;
16+
use PHPStan\Type\Type;
17+
18+
class FormContainerUnsafeValuesDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
19+
{
20+
21+
public function getClass(): string
22+
{
23+
return 'Nette\Forms\Container';
24+
}
25+
26+
public function isMethodSupported(MethodReflection $methodReflection): bool
27+
{
28+
return $methodReflection->getName() === 'getUnsafeValues';
29+
}
30+
31+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
32+
{
33+
if (count($methodCall->args) === 0) {
34+
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
35+
}
36+
37+
$arg = $methodCall->args[0]->value;
38+
$scopedType = $scope->getType($arg);
39+
if ($scopedType instanceof NullType) {
40+
return new ObjectType('Nette\Utils\ArrayHash');
41+
}
42+
43+
if ($scopedType instanceof ConstantStringType && $scopedType->getValue() === 'array') {
44+
return new ArrayType(new StringType(), new MixedType());
45+
}
46+
47+
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
48+
}
49+
50+
}

0 commit comments

Comments
 (0)