Skip to content

Commit cbcea5d

Browse files
committed
Updated for PHPStan 0.9
1 parent f9ceee4 commit cbcea5d

4 files changed

+17
-26
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
"prefer-stable": true,
77
"extra": {
88
"branch-alias": {
9-
"dev-master": "0.8-dev"
9+
"dev-master": "0.9-dev"
1010
}
1111
},
1212
"require": {
1313
"php": "~7.0",
14-
"phpstan/phpstan": "^0.8",
14+
"phpstan/phpstan": "^0.9",
1515
"nette/component-model": "^2.3.0 || ^3.0.0",
1616
"nette/di": "^2.3.0 || ^3.0.0",
1717
"nette/forms": "^2.3.0 || ^3.0.0",

src/Type/Nette/ComponentModelDynamicReturnTypeExtension.php

+3-17
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,14 @@
44

55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
7-
use PHPStan\Reflection\BrokerAwareClassReflectionExtension;
87
use PHPStan\Reflection\MethodReflection;
98
use PHPStan\Type\DynamicMethodReturnTypeExtension;
109
use PHPStan\Type\MixedType;
1110
use PHPStan\Type\Type;
1211

13-
class ComponentModelDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension, BrokerAwareClassReflectionExtension
12+
class ComponentModelDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
1413
{
1514

16-
/** @var \PHPStan\Broker\Broker */
17-
private $broker;
18-
19-
public function setBroker(\PHPStan\Broker\Broker $broker)
20-
{
21-
$this->broker = $broker;
22-
}
23-
2415
public static function getClass(): string
2516
{
2617
return \Nette\ComponentModel\Container::class;
@@ -41,10 +32,6 @@ public function getTypeFromMethodCall(
4132
{
4233
$calledOnType = $scope->getType($methodCall->var);
4334
$mixedType = new MixedType();
44-
if ($calledOnType->getClass() === null || !$this->broker->hasClass($calledOnType->getClass())) {
45-
return $mixedType;
46-
}
47-
4835
$args = $methodCall->args;
4936
if (count($args) !== 1) {
5037
return $mixedType;
@@ -57,13 +44,12 @@ public function getTypeFromMethodCall(
5744

5845
$componentName = $arg->value;
5946

60-
$class = $this->broker->getClass($calledOnType->getClass());
6147
$methodName = sprintf('createComponent%s', ucfirst($componentName));
62-
if (!$class->hasMethod($methodName)) {
48+
if (!$calledOnType->hasMethod($methodName)) {
6349
return $mixedType;
6450
}
6551

66-
return $class->getMethod($methodName)->getReturnType();
52+
return $calledOnType->getMethod($methodName, $scope)->getReturnType();
6753
}
6854

6955
}

src/Type/Nette/FormsBaseControlDynamicReturnTypeExtension.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ public function getTypeFromMethodCall(
2828
Scope $scope
2929
): Type
3030
{
31-
if ($methodReflection->getReturnType()->getClass() !== null && $methodReflection->getReturnType()->getClass() === \Nette\Forms\Controls\BaseControl::class) {
31+
$returnType = $methodReflection->getReturnType();
32+
$referencedClasses = $returnType->getReferencedClasses();
33+
if (
34+
count($referencedClasses) === 1
35+
&& $referencedClasses[0] === \Nette\Forms\Controls\BaseControl::class
36+
) {
3237
return $scope->getType($methodCall->var);
3338
}
3439

tests/Type/Nette/FormContainerValuesDynamicReturnTypeExtensionTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Reflection\MethodReflection;
1010
use PHPStan\Type\ArrayType;
11-
use PHPStan\Type\CommonUnionType;
1211
use PHPStan\Type\FalseBooleanType;
1312
use PHPStan\Type\IterableIterableType;
1413
use PHPStan\Type\MixedType;
1514
use PHPStan\Type\ObjectType;
1615
use PHPStan\Type\TrueBooleanType;
16+
use PHPStan\Type\UnionType;
1717

1818
final class FormContainerValuesDynamicReturnTypeExtensionTest extends \PHPUnit\Framework\TestCase
1919
{
@@ -31,7 +31,7 @@ public function testParameterAsArray()
3131
$methodReflection = $this->createMock(MethodReflection::class);
3232
$methodReflection
3333
->method('getReturnType')
34-
->willReturn(new CommonUnionType([new ArrayType(new MixedType()), new IterableIterableType(new ObjectType(\Nette\Utils\ArrayHash::class))]));
34+
->willReturn(new UnionType([new ArrayType(new MixedType()), new IterableIterableType(new ObjectType(\Nette\Utils\ArrayHash::class))]));
3535

3636
$scope = $this->createMock(Scope::class);
3737
$scope->method('getType')->willReturn(new TrueBooleanType());
@@ -57,7 +57,7 @@ public function testParameterAsArrayHash()
5757
$methodReflection = $this->createMock(MethodReflection::class);
5858
$methodReflection
5959
->method('getReturnType')
60-
->willReturn(new CommonUnionType([new ArrayType(new MixedType()), new IterableIterableType(new ObjectType(\Nette\Utils\ArrayHash::class))]));
60+
->willReturn(new UnionType([new ArrayType(new MixedType()), new IterableIterableType(new ObjectType(\Nette\Utils\ArrayHash::class))]));
6161

6262
$scope = $this->createMock(Scope::class);
6363
$scope->method('getType')->willReturn(new FalseBooleanType());
@@ -76,15 +76,15 @@ public function testParameterAsArrayHash()
7676
$resultType = $this->extension->getTypeFromMethodCall($methodReflection, $methodCall, $scope);
7777

7878
$this->assertInstanceOf(ObjectType::class, $resultType);
79-
$this->assertSame(\Nette\Utils\ArrayHash::class, $resultType->getClass());
79+
$this->assertSame(\Nette\Utils\ArrayHash::class, $resultType->describe());
8080
}
8181

8282
public function testDefaultParameterIsArrayHash()
8383
{
8484
$methodReflection = $this->createMock(MethodReflection::class);
8585
$methodReflection
8686
->method('getReturnType')
87-
->willReturn(new CommonUnionType([new ArrayType(new MixedType()), new IterableIterableType(new ObjectType(\Nette\Utils\ArrayHash::class))]));
87+
->willReturn(new UnionType([new ArrayType(new MixedType()), new IterableIterableType(new ObjectType(\Nette\Utils\ArrayHash::class))]));
8888

8989
$scope = $this->createMock(Scope::class);
9090
$scope->method('getType')->willReturn(new FalseBooleanType());
@@ -96,7 +96,7 @@ public function testDefaultParameterIsArrayHash()
9696
$resultType = $this->extension->getTypeFromMethodCall($methodReflection, $methodCall, $scope);
9797

9898
$this->assertInstanceOf(ObjectType::class, $resultType);
99-
$this->assertSame(\Nette\Utils\ArrayHash::class, $resultType->getClass());
99+
$this->assertSame(\Nette\Utils\ArrayHash::class, $resultType->describe());
100100
}
101101

102102
}

0 commit comments

Comments
 (0)