-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Too wide
@param-out
type - consider all execution ends at once
- Loading branch information
1 parent
3cc1a54
commit d1bcf78
Showing
10 changed files
with
345 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/Rules/TooWideTypehints/TooWideMethodParameterOutTypeRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\TooWideTypehints; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\MethodReturnStatementsNode; | ||
use PHPStan\Reflection\ExtendedMethodReflection; | ||
use PHPStan\Reflection\ParameterReflectionWithPhpDocs; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\UnionType; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<MethodReturnStatementsNode> | ||
*/ | ||
class TooWideMethodParameterOutTypeRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return MethodReturnStatementsNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$inMethod = $node->getMethodReflection(); | ||
$finalScope = null; | ||
foreach ($node->getExecutionEnds() as $executionEnd) { | ||
$endScope = $executionEnd->getStatementResult()->getScope(); | ||
if ($finalScope === null) { | ||
$finalScope = $endScope; | ||
continue; | ||
} | ||
|
||
$finalScope = $finalScope->mergeWith($endScope); | ||
} | ||
|
||
foreach ($node->getReturnStatements() as $statement) { | ||
if ($finalScope === null) { | ||
$finalScope = $statement->getScope(); | ||
continue; | ||
} | ||
|
||
$finalScope = $finalScope->mergeWith($statement->getScope()); | ||
} | ||
|
||
if ($finalScope === null) { | ||
return []; | ||
} | ||
|
||
$variant = ParametersAcceptorSelector::selectSingle($inMethod->getVariants()); | ||
$parameters = $variant->getParameters(); | ||
$errors = []; | ||
foreach ($parameters as $parameter) { | ||
if (!$parameter->passedByReference()->createsNewVariable()) { | ||
continue; | ||
} | ||
|
||
foreach ($this->processSingleParameter($finalScope, $inMethod, $parameter) as $error) { | ||
$errors[] = $error; | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
/** | ||
* @return array<RuleError> | ||
*/ | ||
private function processSingleParameter( | ||
Scope $scope, | ||
ExtendedMethodReflection $inMethod, | ||
ParameterReflectionWithPhpDocs $parameter, | ||
): array | ||
{ | ||
$isParamOutType = true; | ||
$outType = $parameter->getOutType(); | ||
if ($outType === null) { | ||
$isParamOutType = false; | ||
$outType = $parameter->getType(); | ||
} | ||
|
||
if (!$outType instanceof UnionType) { | ||
return []; | ||
} | ||
|
||
$variableExpr = new Node\Expr\Variable($parameter->getName()); | ||
$variableType = $scope->getType($variableExpr); | ||
|
||
$messages = []; | ||
foreach ($outType->getTypes() as $type) { | ||
if (!$type->isSuperTypeOf($variableType)->no()) { | ||
continue; | ||
} | ||
|
||
$errorBuilder = RuleErrorBuilder::message(sprintf( | ||
'Method %s::%s() never assigns %s to &$%s so it can be removed from the %s.', | ||
$inMethod->getDeclaringClass()->getDisplayName(), | ||
$inMethod->getName(), | ||
$type->describe(VerbosityLevel::getRecommendedLevelByType($type)), | ||
$parameter->getName(), | ||
$isParamOutType ? '@param-out type' : 'by-ref type', | ||
)); | ||
if (!$isParamOutType) { | ||
$errorBuilder->tip('You can narrow the parameter out type with @param-out PHPDoc tag.'); | ||
} | ||
|
||
$messages[] = $errorBuilder->build(); | ||
} | ||
|
||
return $messages; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
tests/PHPStan/Rules/TooWideTypehints/TooWideFunctionParameterOutTypeRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\TooWideTypehints; | ||
|
||
use PHPStan\Rules\Rule as TRule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<TooWideFunctionParameterOutTypeRule> | ||
*/ | ||
class TooWideFunctionParameterOutTypeRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): TRule | ||
{ | ||
return new TooWideFunctionParameterOutTypeRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/too-wide-function-parameter-out.php'], [ | ||
[ | ||
'Function TooWideFunctionParameterOut\doBar() never assigns null to &$p so it can be removed from the by-ref type.', | ||
10, | ||
'You can narrow the parameter out type with @param-out PHPDoc tag.', | ||
], | ||
[ | ||
'Function TooWideFunctionParameterOut\doBaz() never assigns null to &$p so it can be removed from the @param-out type.', | ||
18, | ||
], | ||
[ | ||
'Function TooWideFunctionParameterOut\doLorem() never assigns null to &$p so it can be removed from the by-ref type.', | ||
23, | ||
'You can narrow the parameter out type with @param-out PHPDoc tag.', | ||
], | ||
]); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
tests/PHPStan/Rules/TooWideTypehints/TooWideMethodParameterOutTypeRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\TooWideTypehints; | ||
|
||
use PHPStan\Rules\Rule as TRule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<TooWideMethodParameterOutTypeRule> | ||
*/ | ||
class TooWideMethodParameterOutTypeRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): TRule | ||
{ | ||
return new TooWideMethodParameterOutTypeRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/too-wide-method-parameter-out.php'], [ | ||
[ | ||
'Method TooWideMethodParameterOut\Foo::doBar() never assigns null to &$p so it can be removed from the by-ref type.', | ||
13, | ||
'You can narrow the parameter out type with @param-out PHPDoc tag.', | ||
], | ||
[ | ||
'Method TooWideMethodParameterOut\Foo::doBaz() never assigns null to &$p so it can be removed from the @param-out type.', | ||
21, | ||
], | ||
[ | ||
'Method TooWideMethodParameterOut\Foo::doLorem() never assigns null to &$p so it can be removed from the by-ref type.', | ||
26, | ||
'You can narrow the parameter out type with @param-out PHPDoc tag.', | ||
], | ||
]); | ||
} | ||
|
||
public function testBug10684(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/bug-10684.php'], []); | ||
} | ||
|
||
public function testBug10687(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/bug-10687.php'], []); | ||
} | ||
|
||
} |
39 changes: 0 additions & 39 deletions
39
tests/PHPStan/Rules/TooWideTypehints/TooWideParameterOutTypeRuleTest.php
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.