Skip to content

Commit

Permalink
Fixes after PHPStan update
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 26, 2024
1 parent bd9efb7 commit 3eb61a0
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public function getTypeFromMethodCall(
{
$calledOnType = $scope->getType($methodCall->var);

$defaultType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
$defaultType = ParametersAcceptorSelector::selectFromArgs(
$scope,
$methodCall->getArgs(),
$methodReflection->getVariants(),
)->getReturnType();

if ($methodReflection->getName() === 'prototype') {
if (!isset($methodCall->getArgs()[0])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public function getTypeFromMethodCall(
{
$calledOnType = $scope->getType($methodCall->var);

$defaultType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
$defaultType = ParametersAcceptorSelector::selectFromArgs(
$scope,
$methodCall->getArgs(),
$methodReflection->getVariants(),
)->getReturnType();

return new ParentObjectType($defaultType->describe(VerbosityLevel::typeOnly()), $calledOnType);
}
Expand Down
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\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Symfony\Config\ValueObject\ParentObjectType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -42,14 +41,14 @@ public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
): ?Type
{
$calledOnType = $scope->getType($methodCall->var);
if ($calledOnType instanceof ParentObjectType) {
return $calledOnType->getParent();
}

return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
return null;
}

}
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\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Symfony\Config\ValueObject\ParentObjectType;
use PHPStan\Type\Symfony\Config\ValueObject\TreeBuilderType;
Expand All @@ -28,20 +27,17 @@ public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
): ?Type
{
$calledOnType = $scope->getType($methodCall->var);

$defaultType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();

if ($calledOnType instanceof TreeBuilderType) {
return new ParentObjectType(
$calledOnType->getRootNodeClassName(),
$calledOnType,
);
}

return $defaultType;
return null;
}

}
5 changes: 2 additions & 3 deletions src/Type/Symfony/HeaderBagDynamicReturnTypeExtension.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\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
Expand All @@ -32,7 +31,7 @@ public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
): ?Type
{
$firstArgType = isset($methodCall->getArgs()[2]) ? $scope->getType($methodCall->getArgs()[2]->value) : new ConstantBooleanType(true);
$isTrueType = (new ConstantBooleanType(true))->isSuperTypeOf($firstArgType);
Expand All @@ -48,7 +47,7 @@ public function getTypeFromMethodCall(
return new ArrayType(new IntegerType(), new StringType());
}

return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
return null;
}

}
12 changes: 8 additions & 4 deletions src/Type/Symfony/InputBagDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
): ?Type
{
if ($methodReflection->getName() === 'get') {
return $this->getGetTypeFromMethodCall($methodReflection, $methodCall, $scope);
Expand All @@ -54,17 +54,21 @@ private function getGetTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
): ?Type
{
if (isset($methodCall->getArgs()[1])) {
$argType = $scope->getType($methodCall->getArgs()[1]->value);
$isNull = (new NullType())->isSuperTypeOf($argType);
if ($isNull->no()) {
return TypeCombinator::removeNull(ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType());
return TypeCombinator::removeNull(ParametersAcceptorSelector::selectFromArgs(
$scope,
$methodCall->getArgs(),
$methodReflection->getVariants(),
)->getReturnType());
}
}

return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
return null;
}

private function getAllTypeFromMethodCall(
Expand Down
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\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
Expand All @@ -30,7 +29,7 @@ public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
): ?Type
{
$firstArgType = isset($methodCall->getArgs()[2]) ? $scope->getType($methodCall->getArgs()[2]->value) : new ConstantBooleanType(true);
$isTrueType = (new ConstantBooleanType(true))->isSuperTypeOf($firstArgType);
Expand All @@ -44,7 +43,7 @@ public function getTypeFromMethodCall(
return new ArrayType(new IntegerType(), new StringType());
}

return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
return null;
}

}
12 changes: 5 additions & 7 deletions src/Type/Symfony/ParameterDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\PhpDoc\TypeStringResolver;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\Symfony\Configuration;
use PHPStan\Symfony\ParameterMap;
Expand Down Expand Up @@ -85,7 +84,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
return in_array($methodReflection->getName(), $methods, true);
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
switch ($methodReflection->getName()) {
case $this->methodGet:
Expand Down Expand Up @@ -206,16 +205,15 @@ private function getHasTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
): ?Type
{
$defaultReturnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if (!isset($methodCall->getArgs()[0]) || !$this->constantHassers) {
return $defaultReturnType;
return null;
}

$parameterKeys = $this->parameterMap::getParameterKeysFromNode($methodCall->getArgs()[0]->value, $scope);
if ($parameterKeys === []) {
return $defaultReturnType;
return null;
}

$has = null;
Expand All @@ -228,7 +226,7 @@ private function getHasTypeFromMethodCall(
($has === true && $parameter === null)
|| ($has === false && $parameter !== null)
) {
return $defaultReturnType;
return null;
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/Type/Symfony/RequestDynamicReturnTypeExtension.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\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ResourceType;
Expand All @@ -29,7 +28,7 @@ public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
): ?Type
{
if (!isset($methodCall->getArgs()[0])) {
return new StringType();
Expand All @@ -46,7 +45,7 @@ public function getTypeFromMethodCall(
return new StringType();
}

return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
return null;
}

}
19 changes: 8 additions & 11 deletions src/Type/Symfony/ServiceDynamicReturnTypeExtension.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\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\Symfony\Configuration;
use PHPStan\Symfony\ParameterMap;
Expand Down Expand Up @@ -56,7 +55,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
return in_array($methodReflection->getName(), ['get', 'has'], true);
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
switch ($methodReflection->getName()) {
case 'get':
Expand All @@ -71,16 +70,15 @@ private function getGetTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
): ?Type
{
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if (!isset($methodCall->getArgs()[0])) {
return $returnType;
return null;
}

$parameterBag = $this->tryGetParameterBag();
if ($parameterBag === null) {
return $returnType;
return null;
}

$serviceId = $this->serviceMap::getServiceIdFromNode($methodCall->getArgs()[0]->value, $scope);
Expand All @@ -91,7 +89,7 @@ private function getGetTypeFromMethodCall(
}
}

return $returnType;
return null;
}

private function tryGetParameterBag(): ?ParameterBag
Expand Down Expand Up @@ -122,11 +120,10 @@ private function getHasTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
): ?Type
{
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
if (!isset($methodCall->getArgs()[0]) || !$this->constantHassers) {
return $returnType;
return null;
}

$serviceId = $this->serviceMap::getServiceIdFromNode($methodCall->getArgs()[0]->value, $scope);
Expand All @@ -135,7 +132,7 @@ private function getHasTypeFromMethodCall(
return new ConstantBooleanType($service !== null && $service->isPublic());
}

return $returnType;
return null;
}

private function determineServiceClass(ParameterBag $parameterBag, ServiceDefinition $service): ?string
Expand Down

0 comments on commit 3eb61a0

Please sign in to comment.