Skip to content

Commit 9d0069a

Browse files
committed
Updated PHPStan
1 parent c3b5cbf commit 9d0069a

9 files changed

+49
-43
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"nette/component-model": "^2.3.0 || ^3.0.0",
1717
"nette/di": "^2.3.0 || ^3.0.0",
1818
"nette/forms": "^2.3.0 || ^3.0.0",
19-
"nette/utils": "^2.3.0 || ^3.0.0"
19+
"nette/utils": "^2.3.0 || ^3.0.0",
20+
"nikic/php-parser": "^4.0"
2021
},
2122
"require-dev": {
2223
"consistence/coding-standard": "^3.0.1",

src/Reflection/Nette/HtmlMethodReflection.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use Nette\Utils\Html;
66
use PHPStan\Reflection\ClassMemberReflection;
77
use PHPStan\Reflection\ClassReflection;
8+
use PHPStan\Reflection\FunctionVariant;
89
use PHPStan\Reflection\MethodReflection;
910
use PHPStan\Type\MixedType;
1011
use PHPStan\Type\ObjectType;
11-
use PHPStan\Type\Type;
1212

1313
class HtmlMethodReflection implements MethodReflection
1414
{
@@ -41,16 +41,17 @@ public function isStatic(): bool
4141
}
4242

4343
/**
44-
* @return \PHPStan\Reflection\ParameterReflection[]
44+
* @return \PHPStan\Reflection\ParametersAcceptor[]
4545
*/
46-
public function getParameters(): array
46+
public function getVariants(): array
4747
{
48-
return [];
49-
}
50-
51-
public function isVariadic(): bool
52-
{
53-
return true;
48+
return [
49+
new FunctionVariant(
50+
[],
51+
true,
52+
substr($this->name, 0, 3) === 'get' ? new MixedType() : new ObjectType(Html::class)
53+
),
54+
];
5455
}
5556

5657
public function isPrivate(): bool
@@ -68,9 +69,4 @@ public function getName(): string
6869
return $this->name;
6970
}
7071

71-
public function getReturnType(): Type
72-
{
73-
return substr($this->name, 0, 3) === 'get' ? new MixedType() : new ObjectType(Html::class);
74-
}
75-
7672
}

src/Reflection/Nette/NetteObjectClassReflectionExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\Reflection\ClassReflection;
66
use PHPStan\Reflection\MethodReflection;
77
use PHPStan\Reflection\MethodsClassReflectionExtension;
8+
use PHPStan\Reflection\ParametersAcceptorSelector;
89
use PHPStan\Reflection\PropertiesClassReflectionExtension;
910
use PHPStan\Reflection\PropertyReflection;
1011

@@ -42,7 +43,7 @@ public function getProperty(ClassReflection $classReflection, string $propertyNa
4243
{
4344
/** @var \PHPStan\Reflection\MethodReflection $getterMethod */
4445
$getterMethod = $this->getMethodByProperty($classReflection, $propertyName);
45-
return new NetteObjectPropertyReflection($classReflection, $getterMethod->getReturnType());
46+
return new NetteObjectPropertyReflection($classReflection, ParametersAcceptorSelector::selectSingle($getterMethod->getVariants())->getReturnType());
4647
}
4748

4849
public function hasMethod(ClassReflection $classReflection, string $methodName): bool

src/Reflection/Nette/NetteObjectEventListenerMethodReflection.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use PHPStan\Reflection\ClassMemberReflection;
66
use PHPStan\Reflection\ClassReflection;
7+
use PHPStan\Reflection\FunctionVariant;
78
use PHPStan\Reflection\MethodReflection;
8-
use PHPStan\Type\Type;
99
use PHPStan\Type\VoidType;
1010

1111
class NetteObjectEventListenerMethodReflection implements MethodReflection
@@ -44,16 +44,17 @@ public function isStatic(): bool
4444
}
4545

4646
/**
47-
* @return \PHPStan\Reflection\ParameterReflection[]
47+
* @return \PHPStan\Reflection\ParametersAcceptor[]
4848
*/
49-
public function getParameters(): array
49+
public function getVariants(): array
5050
{
51-
return [];
52-
}
53-
54-
public function isVariadic(): bool
55-
{
56-
return true;
51+
return [
52+
new FunctionVariant(
53+
[],
54+
true,
55+
new VoidType()
56+
),
57+
];
5758
}
5859

5960
public function isPrivate(): bool
@@ -66,9 +67,4 @@ public function isPublic(): bool
6667
return true;
6768
}
6869

69-
public function getReturnType(): Type
70-
{
71-
return new VoidType();
72-
}
73-
7470
}

src/Type/Nette/ComponentModelDynamicReturnTypeExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Reflection\ParametersAcceptorSelector;
89
use PHPStan\Type\Constant\ConstantStringType;
910
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1011
use PHPStan\Type\MixedType;
@@ -50,7 +51,9 @@ public function getTypeFromMethodCall(
5051
return $mixedType;
5152
}
5253

53-
return $calledOnType->getMethod($methodName, $scope)->getReturnType();
54+
$method = $calledOnType->getMethod($methodName, $scope);
55+
56+
return ParametersAcceptorSelector::selectSingle($method->getVariants())->getReturnType();
5457
}
5558

5659
}

src/Type/Nette/FormContainerValuesDynamicReturnTypeExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Reflection\ParametersAcceptorSelector;
89
use PHPStan\Type\ArrayType;
910
use PHPStan\Type\Constant\ConstantBooleanType;
1011
use PHPStan\Type\DynamicMethodReturnTypeExtension;
@@ -35,7 +36,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
3536
$arg = $methodCall->args[0]->value;
3637
$scopedType = $scope->getType($arg);
3738
if (!$scopedType instanceof ConstantBooleanType) {
38-
return $methodReflection->getReturnType();
39+
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
3940
}
4041

4142
if (!$scopedType->getValue()) {

src/Type/Nette/FormsBaseControlDynamicReturnTypeExtension.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8+
use PHPStan\Reflection\ParametersAcceptorSelector;
89
use PHPStan\Type\Type;
910

1011
class FormsBaseControlDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension
@@ -28,7 +29,7 @@ public function getTypeFromMethodCall(
2829
Scope $scope
2930
): Type
3031
{
31-
$returnType = $methodReflection->getReturnType();
32+
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
3233
$referencedClasses = $returnType->getReferencedClasses();
3334
if (
3435
count($referencedClasses) === 1
@@ -37,7 +38,7 @@ public function getTypeFromMethodCall(
3738
return $scope->getType($methodCall->var);
3839
}
3940

40-
return $methodReflection->getReturnType();
41+
return $returnType;
4142
}
4243

4344
}

tests/Reflection/Nette/HtmlClassReflectionExtensionTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Reflection\Nette;
44

5+
use PHPStan\Reflection\ParametersAcceptorSelector;
56
use PHPStan\Type\VerbosityLevel;
67

78
class HtmlClassReflectionExtensionTest extends \PHPStan\Testing\TestCase
@@ -51,14 +52,15 @@ public function testGetMethod(): void
5152
{
5253
$classReflection = $this->broker->getClass(\Nette\Utils\Html::class);
5354
$methodReflection = $this->extension->getMethod($classReflection, 'href');
55+
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
5456
self::assertSame('href', $methodReflection->getName());
5557
self::assertSame($classReflection, $methodReflection->getDeclaringClass());
5658
self::assertFalse($methodReflection->isStatic());
57-
self::assertEmpty($methodReflection->getParameters());
58-
self::assertTrue($methodReflection->isVariadic());
59+
self::assertEmpty($parametersAcceptor->getParameters());
60+
self::assertTrue($parametersAcceptor->isVariadic());
5961
self::assertFalse($methodReflection->isPrivate());
6062
self::assertTrue($methodReflection->isPublic());
61-
self::assertSame(\Nette\Utils\Html::class, $methodReflection->getReturnType()->describe(VerbosityLevel::value()));
63+
self::assertSame(\Nette\Utils\Html::class, $parametersAcceptor->getReturnType()->describe(VerbosityLevel::value()));
6264
}
6365

6466
/**

tests/Type/Nette/FormContainerValuesDynamicReturnTypeExtensionTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Expr;
77
use PhpParser\Node\Expr\MethodCall;
88
use PHPStan\Analyser\Scope;
9+
use PHPStan\Reflection\FunctionVariant;
910
use PHPStan\Reflection\MethodReflection;
1011
use PHPStan\Type\ArrayType;
1112
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -30,8 +31,12 @@ public function testParameterAsArray(): void
3031
{
3132
$methodReflection = $this->createMock(MethodReflection::class);
3233
$methodReflection
33-
->method('getReturnType')
34-
->willReturn(new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new ObjectType(\Nette\Utils\ArrayHash::class))]));
34+
->method('getVariants')
35+
->willReturn([new FunctionVariant(
36+
[],
37+
true,
38+
new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new ObjectType(\Nette\Utils\ArrayHash::class))])
39+
)]);
3540

3641
$scope = $this->createMock(Scope::class);
3742
$scope->method('getType')->willReturn(new ConstantBooleanType(true));
@@ -56,8 +61,8 @@ public function testParameterAsArrayHash(): void
5661
{
5762
$methodReflection = $this->createMock(MethodReflection::class);
5863
$methodReflection
59-
->method('getReturnType')
60-
->willReturn(new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new ObjectType(\Nette\Utils\ArrayHash::class))]));
64+
->method('getVariants')
65+
->willReturn([new FunctionVariant([], true, new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new ObjectType(\Nette\Utils\ArrayHash::class))]))]);
6166

6267
$scope = $this->createMock(Scope::class);
6368
$scope->method('getType')->willReturn(new ConstantBooleanType(false));
@@ -83,8 +88,8 @@ public function testDefaultParameterIsArrayHash(): void
8388
{
8489
$methodReflection = $this->createMock(MethodReflection::class);
8590
$methodReflection
86-
->method('getReturnType')
87-
->willReturn(new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new ObjectType(\Nette\Utils\ArrayHash::class))]));
91+
->method('getVariants')
92+
->willReturn([new FunctionVariant([], true, new UnionType([new ArrayType(new MixedType(), new MixedType()), new IterableType(new MixedType(), new ObjectType(\Nette\Utils\ArrayHash::class))]))]);
8893

8994
$scope = $this->createMock(Scope::class);
9095
$scope->method('getType')->willReturn(new ConstantBooleanType(false));

0 commit comments

Comments
 (0)