Skip to content

Commit

Permalink
Do not use instanceof *Type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 18, 2023
1 parent a801f2d commit 9e083bb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand Down Expand Up @@ -44,11 +43,10 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
$paramNeedExpr = $methodCall->getArgs()[0]->value;
$paramNeedType = $scope->getType($paramNeedExpr);

if ($paramNeedType instanceof ConstantBooleanType) {
if ($paramNeedType->getValue()) {
return TypeCombinator::removeNull($defaultReturnType);
}

if ($paramNeedType->isTrue()->yes()) {
return TypeCombinator::removeNull($defaultReturnType);
}
if ($paramNeedType->isFalse()->yes()) {
return TypeCombinator::addNull($defaultReturnType);
}

Expand Down
10 changes: 4 additions & 6 deletions src/Type/Nette/ComponentLookupDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand Down Expand Up @@ -37,11 +36,10 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
$paramNeedExpr = $methodCall->getArgs()[1]->value;
$paramNeedType = $scope->getType($paramNeedExpr);

if ($paramNeedType instanceof ConstantBooleanType) {
if ($paramNeedType->getValue()) {
return TypeCombinator::removeNull($defaultReturnType);
}

if ($paramNeedType->isTrue()->yes()) {
return TypeCombinator::removeNull($defaultReturnType);
}
if ($paramNeedType->isFalse()->yes()) {
return TypeCombinator::addNull($defaultReturnType);
}

Expand Down
13 changes: 5 additions & 8 deletions src/Type/Nette/FormContainerValuesDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
Expand All @@ -28,23 +26,22 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
return $methodReflection->getName() === 'getValues';
}

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

$arg = $methodCall->getArgs()[0]->value;
$scopedType = $scope->getType($arg);
if (!$scopedType instanceof ConstantBooleanType) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if ($scopedType->isTrue()->yes()) {
return new ArrayType(new StringType(), new MixedType());
}

if (!$scopedType->getValue()) {
if ($scopedType->isFalse()->yes()) {
return new ObjectType('Nette\Utils\ArrayHash');
}

return new ArrayType(new StringType(), new MixedType());
return null;
}

}
6 changes: 1 addition & 5 deletions src/Type/Nette/ServiceLocatorDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -57,10 +56,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
&& count($methodCall->getArgs()) >= 2
) {
$throwType = $scope->getType($methodCall->getArgs()[1]->value);
if (
!$throwType instanceof ConstantBooleanType
|| !$throwType->getValue()
) {
if (!$throwType->isTrue()->yes()) {
$type = TypeCombinator::addNull($type);
}
}
Expand Down

0 comments on commit 9e083bb

Please sign in to comment.