Skip to content

Commit

Permalink
Remove deprecated ParametersAcceptorSelector::selectFromArgs (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored Oct 1, 2024
1 parent d35bf66 commit d144495
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 35 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "^7.4 || ^8.0",
"phpstan/phpstan": "^1.11.0"
"phpstan/phpstan": "^1.12.5"
},
"require-dev": {
"editorconfig-checker/editorconfig-checker": "^10.6.0",
Expand Down Expand Up @@ -74,7 +74,7 @@
"check:ec": "ec src tests",
"check:ignores": "php bin/verify-inline-ignore.php",
"check:tests": "phpunit -vvv tests",
"check:types": "phpstan analyse -vvv --ansi",
"check:types": "phpstan analyse -vv --ansi",
"fix:cs": "phpcbf"
}
}
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 5 additions & 22 deletions src/Rule/EnforceListReturnRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClosureReturnStatementsNode;
use PHPStan\Node\ReturnStatementsNode;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -43,12 +41,15 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if ($this->alwaysReturnList($node) && !$this->isMarkedWithListReturn($methodReflection)) {
$returnType = $methodReflection->getReturnType();

if ($this->alwaysReturnList($node) && !$returnType->isList()->yes()) {
$callLikeType = $methodReflection instanceof MethodReflection
? 'Method'
: 'Function';
$returnTypeString = $returnType->describe(VerbosityLevel::precise());

$error = RuleErrorBuilder::message("{$callLikeType} {$methodReflection->getName()} always return list, but is marked as {$this->getReturnPhpDoc($methodReflection)}")
$error = RuleErrorBuilder::message("{$callLikeType} {$methodReflection->getName()} always return list, but is marked as {$returnTypeString}")
->identifier('shipmonk.returnListNotUsed')
->build();
return [$error];
Expand All @@ -57,15 +58,6 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

/**
* @param FunctionReflection|MethodReflection $methodReflection
*/
private function getReturnPhpDoc(object $methodReflection): string
{
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
return $returnType->describe(VerbosityLevel::precise());
}

private function alwaysReturnList(ReturnStatementsNode $node): bool
{
$returnStatementsCount = count($node->getReturnStatements());
Expand Down Expand Up @@ -95,13 +87,4 @@ private function alwaysReturnList(ReturnStatementsNode $node): bool
return true;
}

/**
* @param FunctionReflection|MethodReflection $methodReflection
*/
private function isMarkedWithListReturn(object $methodReflection): bool
{
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
return $returnType->isList()->yes();
}

}
3 changes: 1 addition & 2 deletions src/Rule/ForbidReturnValueInYieldingMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\Node\ClosureReturnStatementsNode;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Node\ReturnStatementsNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -90,7 +89,7 @@ private function getReturnType(ReturnStatementsNode $node, Scope $scope): Type
}

if ($methodReflection !== null) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
return $methodReflection->getReturnType();
}

return new MixedType();
Expand Down
3 changes: 1 addition & 2 deletions src/Rule/ForbidUselessNullableReturnRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClosureReturnStatementsNode;
use PHPStan\Node\ReturnStatementsNode;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -39,7 +38,7 @@ public function processNode(Node $node, Scope $scope): array
if ($node instanceof ClosureReturnStatementsNode) {
$declaredType = $scope->getFunctionType($node->getClosureExpr()->getReturnType(), false, false);
} elseif ($methodReflection !== null) {
$declaredType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
$declaredType = $methodReflection->getReturnType();
} else {
return [];
}
Expand Down
6 changes: 5 additions & 1 deletion src/Rule/RequirePreviousExceptionPassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ private function getCallLikeParameters(CallLike $node, Scope $scope): array

// FuncCall not yet supported
if ($methodReflection !== null) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getParameters();
return ParametersAcceptorSelector::selectFromArgs(
$scope,
$node->getArgs(),
$methodReflection->getVariants(),
)->getParameters();
}

return [];
Expand Down

0 comments on commit d144495

Please sign in to comment.