Skip to content

Commit 9e083bb

Browse files
committed
Do not use instanceof *Type
1 parent a801f2d commit 9e083bb

4 files changed

+14
-25
lines changed

src/Type/Nette/ComponentGetPresenterDynamicReturnTypeExtension.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
88
use PHPStan\Reflection\ParametersAcceptorSelector;
9-
use PHPStan\Type\Constant\ConstantBooleanType;
109
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1110
use PHPStan\Type\Type;
1211
use PHPStan\Type\TypeCombinator;
@@ -44,11 +43,10 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4443
$paramNeedExpr = $methodCall->getArgs()[0]->value;
4544
$paramNeedType = $scope->getType($paramNeedExpr);
4645

47-
if ($paramNeedType instanceof ConstantBooleanType) {
48-
if ($paramNeedType->getValue()) {
49-
return TypeCombinator::removeNull($defaultReturnType);
50-
}
51-
46+
if ($paramNeedType->isTrue()->yes()) {
47+
return TypeCombinator::removeNull($defaultReturnType);
48+
}
49+
if ($paramNeedType->isFalse()->yes()) {
5250
return TypeCombinator::addNull($defaultReturnType);
5351
}
5452

src/Type/Nette/ComponentLookupDynamicReturnTypeExtension.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
88
use PHPStan\Reflection\ParametersAcceptorSelector;
9-
use PHPStan\Type\Constant\ConstantBooleanType;
109
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1110
use PHPStan\Type\Type;
1211
use PHPStan\Type\TypeCombinator;
@@ -37,11 +36,10 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
3736
$paramNeedExpr = $methodCall->getArgs()[1]->value;
3837
$paramNeedType = $scope->getType($paramNeedExpr);
3938

40-
if ($paramNeedType instanceof ConstantBooleanType) {
41-
if ($paramNeedType->getValue()) {
42-
return TypeCombinator::removeNull($defaultReturnType);
43-
}
44-
39+
if ($paramNeedType->isTrue()->yes()) {
40+
return TypeCombinator::removeNull($defaultReturnType);
41+
}
42+
if ($paramNeedType->isFalse()->yes()) {
4543
return TypeCombinator::addNull($defaultReturnType);
4644
}
4745

src/Type/Nette/FormContainerValuesDynamicReturnTypeExtension.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8-
use PHPStan\Reflection\ParametersAcceptorSelector;
98
use PHPStan\Type\ArrayType;
10-
use PHPStan\Type\Constant\ConstantBooleanType;
119
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1210
use PHPStan\Type\MixedType;
1311
use PHPStan\Type\ObjectType;
@@ -28,23 +26,22 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
2826
return $methodReflection->getName() === 'getValues';
2927
}
3028

31-
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
29+
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
3230
{
3331
if (count($methodCall->getArgs()) === 0) {
3432
return new ObjectType('Nette\Utils\ArrayHash');
3533
}
3634

3735
$arg = $methodCall->getArgs()[0]->value;
3836
$scopedType = $scope->getType($arg);
39-
if (!$scopedType instanceof ConstantBooleanType) {
40-
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
37+
if ($scopedType->isTrue()->yes()) {
38+
return new ArrayType(new StringType(), new MixedType());
4139
}
42-
43-
if (!$scopedType->getValue()) {
40+
if ($scopedType->isFalse()->yes()) {
4441
return new ObjectType('Nette\Utils\ArrayHash');
4542
}
4643

47-
return new ArrayType(new StringType(), new MixedType());
44+
return null;
4845
}
4946

5047
}

src/Type/Nette/ServiceLocatorDynamicReturnTypeExtension.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MethodReflection;
8-
use PHPStan\Type\Constant\ConstantBooleanType;
98
use PHPStan\Type\DynamicMethodReturnTypeExtension;
109
use PHPStan\Type\MixedType;
1110
use PHPStan\Type\ObjectType;
@@ -57,10 +56,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
5756
&& count($methodCall->getArgs()) >= 2
5857
) {
5958
$throwType = $scope->getType($methodCall->getArgs()[1]->value);
60-
if (
61-
!$throwType instanceof ConstantBooleanType
62-
|| !$throwType->getValue()
63-
) {
59+
if (!$throwType->isTrue()->yes()) {
6460
$type = TypeCombinator::addNull($type);
6561
}
6662
}

0 commit comments

Comments
 (0)