Skip to content

Commit

Permalink
Simplify default-return path in extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jan 14, 2024
1 parent 9463e66 commit a881e98
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 33 deletions.
5 changes: 2 additions & 3 deletions src/Type/Php/ArrayIntersectKeyFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
Expand All @@ -27,11 +26,11 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
return $functionReflection->getName() === 'array_intersect_key';
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
$args = $functionCall->getArgs();
if (count($args) === 0) {
return ParametersAcceptorSelector::selectFromArgs($scope, $args, $functionReflection->getVariants())->getReturnType();
return null;
}

$argTypes = [];
Expand Down
5 changes: 2 additions & 3 deletions src/Type/Php/ArraySpliceFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ArrayType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\Type;
Expand All @@ -22,10 +21,10 @@ public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): Type
): ?Type
{
if (!isset($functionCall->getArgs()[0])) {
return ParametersAcceptorSelector::selectFromArgs($scope, $functionCall->getArgs(), $functionReflection->getVariants())->getReturnType();
return null;
}

$arrayArg = $scope->getType($functionCall->getArgs()[0]->value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\Type\ClosureType;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
Expand All @@ -28,14 +27,10 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo
return $methodReflection->getName() === 'fromCallable';
}

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
{
if (!isset($methodCall->getArgs()[0])) {
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$methodCall->getArgs(),
$methodReflection->getVariants(),
)->getReturnType();
return null;
}

$callableType = $scope->getType($methodCall->getArgs()[0]->value);
Expand Down
21 changes: 7 additions & 14 deletions src/Type/Php/DsMapDynamicReturnTypeExtension.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\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
Expand All @@ -25,41 +24,35 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
return in_array($methodReflection->getName(), ['get', 'remove'], true);
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
$returnType = ParametersAcceptorSelector::selectFromArgs(
$scope,
$methodCall->getArgs(),
$methodReflection->getVariants(),
)->getReturnType();

$argsCount = count($methodCall->getArgs());
if ($argsCount > 1) {
return $returnType;
return null;
}

if ($argsCount === 0) {
return $returnType;
return null;
}

$mapType = $scope->getType($methodCall->var);
if (!$mapType instanceof TypeWithClassName) {
return $returnType;
return null;
}

$mapAncestor = $mapType->getAncestorWithClassName('Ds\Map');
if ($mapAncestor === null) {
return $returnType;
return null;
}

$mapAncestorClass = $mapAncestor->getClassReflection();
if ($mapAncestorClass === null) {
return $returnType;
return null;
}

$valueType = $mapAncestorClass->getActiveTemplateTypeMap()->getType('TValue');
if ($valueType === null) {
return $returnType;
return null;
}

return $valueType;
Expand Down
5 changes: 2 additions & 3 deletions src/Type/Php/RandomIntFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
Expand All @@ -26,14 +25,14 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
return in_array($functionReflection->getName(), ['random_int', 'rand', 'mt_rand'], true);
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (in_array($functionReflection->getName(), ['rand', 'mt_rand'], true) && count($functionCall->getArgs()) === 0) {
return IntegerRangeType::fromInterval(0, null);
}

if (count($functionCall->getArgs()) < 2) {
return ParametersAcceptorSelector::selectFromArgs($scope, $functionCall->getArgs(), $functionReflection->getVariants())->getReturnType();
return null;
}

$minType = $scope->getType($functionCall->getArgs()[0]->value)->toInteger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand All @@ -28,10 +27,10 @@ public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): Type
): ?Type
{
if (count($functionCall->getArgs()) < 2) {
return ParametersAcceptorSelector::selectFromArgs($scope, $functionCall->getArgs(), $functionReflection->getVariants())->getReturnType();
return null;
}

$version1Strings = $scope->getType($functionCall->getArgs()[0]->value)->getConstantStrings();
Expand Down

0 comments on commit a881e98

Please sign in to comment.