Skip to content

Commit

Permalink
Utilize null-default return to simplify extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
clxmstaab authored and ondrejmirtes committed Dec 29, 2022
1 parent dac2474 commit fca0834
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 46 deletions.
12 changes: 5 additions & 7 deletions src/Type/Symfony/CommandGetHelperDynamicReturnTypeExtension.php
Expand Up @@ -36,22 +36,20 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
return $methodReflection->getName() === 'getHelper';
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
$defaultReturnType = new ObjectType('Symfony\Component\Console\Helper\HelperInterface');

if (!isset($methodCall->getArgs()[0])) {
return $defaultReturnType;
return null;
}

$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
return $defaultReturnType;
return null;
}

$argStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->getArgs()[0]->value));
if (count($argStrings) !== 1) {
return $defaultReturnType;
return null;
}
$argName = $argStrings[0]->getValue();

Expand All @@ -65,7 +63,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}
}

return count($returnTypes) > 0 ? TypeCombinator::union(...$returnTypes) : $defaultReturnType;
return count($returnTypes) > 0 ? TypeCombinator::union(...$returnTypes) : null;
}

}
Expand Up @@ -6,7 +6,6 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Symfony\ConsoleApplicationResolver;
use PHPStan\Type\ArrayType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
Expand Down Expand Up @@ -38,22 +37,20 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
return $methodReflection->getName() === 'getArgument';
}

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

if (!isset($methodCall->getArgs()[0])) {
return $defaultReturnType;
return null;
}

$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
return $defaultReturnType;
return null;
}

$argStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->getArgs()[0]->value));
if (count($argStrings) !== 1) {
return $defaultReturnType;
return null;
}
$argName = $argStrings[0]->getValue();

Expand All @@ -79,7 +76,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}
}

return count($argTypes) > 0 ? TypeCombinator::union(...$argTypes) : $defaultReturnType;
return count($argTypes) > 0 ? TypeCombinator::union(...$argTypes) : null;
}

}
Expand Up @@ -6,7 +6,6 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Symfony\ConsoleApplicationResolver;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -39,22 +38,20 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
return $methodReflection->getName() === 'getOption';
}

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

if (!isset($methodCall->getArgs()[0])) {
return $defaultReturnType;
return null;
}

$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
return $defaultReturnType;
return null;
}

$optStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->getArgs()[0]->value));
if (count($optStrings) !== 1) {
return $defaultReturnType;
return null;
}
$optName = $optStrings[0]->getValue();

Expand All @@ -69,7 +66,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}
}

return count($optTypes) > 0 ? TypeCombinator::union(...$optTypes) : $defaultReturnType;
return count($optTypes) > 0 ? TypeCombinator::union(...$optTypes) : null;
}

}
Expand Up @@ -6,7 +6,6 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Symfony\ConsoleApplicationResolver;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantStringType;
Expand Down Expand Up @@ -40,12 +39,11 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
return $methodReflection->getName() === 'getOptions';
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType();
$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
return $defaultReturnType;
return null;
}

$optTypes = [];
Expand All @@ -65,7 +63,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}
}

return count($optTypes) > 0 ? TypeCombinator::union(...$optTypes) : $defaultReturnType;
return count($optTypes) > 0 ? TypeCombinator::union(...$optTypes) : null;
}

}
Expand Up @@ -7,7 +7,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Symfony\ConsoleApplicationResolver;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -36,22 +35,20 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
return $methodReflection->getName() === 'hasArgument';
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
$defaultReturnType = new BooleanType();

if (!isset($methodCall->getArgs()[0])) {
return $defaultReturnType;
return null;
}

$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
return $defaultReturnType;
return null;
}

$argStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->getArgs()[0]->value));
if (count($argStrings) !== 1) {
return $defaultReturnType;
return null;
}
$argName = $argStrings[0]->getValue();

Expand All @@ -67,11 +64,11 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

if (count($returnTypes) === 0) {
return $defaultReturnType;
return null;
}

$returnTypes = array_unique($returnTypes);
return count($returnTypes) === 1 ? new ConstantBooleanType($returnTypes[0]) : $defaultReturnType;
return count($returnTypes) === 1 ? new ConstantBooleanType($returnTypes[0]) : null;
}

}
Expand Up @@ -7,7 +7,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Symfony\ConsoleApplicationResolver;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -36,22 +35,20 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
return $methodReflection->getName() === 'hasOption';
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
$defaultReturnType = new BooleanType();

if (!isset($methodCall->getArgs()[0])) {
return $defaultReturnType;
return null;
}

$classReflection = $scope->getClassReflection();
if ($classReflection === null) {
return $defaultReturnType;
return null;
}

$optStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->getArgs()[0]->value));
if (count($optStrings) !== 1) {
return $defaultReturnType;
return null;
}
$optName = $optStrings[0]->getValue();

Expand All @@ -67,11 +64,11 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

if (count($returnTypes) === 0) {
return $defaultReturnType;
return null;
}

$returnTypes = array_unique($returnTypes);
return count($returnTypes) === 1 ? new ConstantBooleanType($returnTypes[0]) : $defaultReturnType;
return count($returnTypes) === 1 ? new ConstantBooleanType($returnTypes[0]) : null;
}

}

0 comments on commit fca0834

Please sign in to comment.