-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bleeding edge - IncompatibleDefaultParameterTypeRule for closures
- Loading branch information
1 parent
c4ee0b8
commit 0264f5b
Showing
11 changed files
with
259 additions
and
2 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
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
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
65 changes: 65 additions & 0 deletions
65
src/Rules/Functions/IncompatibleArrowFunctionDefaultParameterTypeRule.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,65 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InArrowFunctionNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\Type\Generic\TemplateTypeHelper; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function is_string; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<InArrowFunctionNode> | ||
*/ | ||
class IncompatibleArrowFunctionDefaultParameterTypeRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InArrowFunctionNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$parameters = $node->getClosureType()->getParameters(); | ||
|
||
$errors = []; | ||
foreach ($node->getOriginalNode()->getParams() as $paramI => $param) { | ||
if ($param->default === null) { | ||
continue; | ||
} | ||
if ( | ||
$param->var instanceof Node\Expr\Error | ||
|| !is_string($param->var->name) | ||
) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$defaultValueType = $scope->getType($param->default); | ||
$parameterType = $parameters[$paramI]->getType(); | ||
$parameterType = TemplateTypeHelper::resolveToBounds($parameterType); | ||
|
||
if ($parameterType->accepts($defaultValueType, true)->yes()) { | ||
continue; | ||
} | ||
|
||
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $defaultValueType); | ||
|
||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'Default value of the parameter #%d $%s (%s) of anonymous function is incompatible with type %s.', | ||
$paramI + 1, | ||
$param->var->name, | ||
$defaultValueType->describe($verbosityLevel), | ||
$parameterType->describe($verbosityLevel), | ||
))->line($param->getLine())->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/Rules/Functions/IncompatibleClosureDefaultParameterTypeRule.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,65 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClosureNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\Type\Generic\TemplateTypeHelper; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function is_string; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<InClosureNode> | ||
*/ | ||
class IncompatibleClosureDefaultParameterTypeRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClosureNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$parameters = $node->getClosureType()->getParameters(); | ||
|
||
$errors = []; | ||
foreach ($node->getOriginalNode()->getParams() as $paramI => $param) { | ||
if ($param->default === null) { | ||
continue; | ||
} | ||
if ( | ||
$param->var instanceof Node\Expr\Error | ||
|| !is_string($param->var->name) | ||
) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$defaultValueType = $scope->getType($param->default); | ||
$parameterType = $parameters[$paramI]->getType(); | ||
$parameterType = TemplateTypeHelper::resolveToBounds($parameterType); | ||
|
||
if ($parameterType->accepts($defaultValueType, true)->yes()) { | ||
continue; | ||
} | ||
|
||
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($parameterType, $defaultValueType); | ||
|
||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'Default value of the parameter #%d $%s (%s) of anonymous function is incompatible with type %s.', | ||
$paramI + 1, | ||
$param->var->name, | ||
$defaultValueType->describe($verbosityLevel), | ||
$parameterType->describe($verbosityLevel), | ||
))->line($param->getLine())->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
tests/PHPStan/Rules/Functions/IncompatibleArrowFunctionDefaultParameterTypeRuleTest.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,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<IncompatibleArrowFunctionDefaultParameterTypeRule> | ||
*/ | ||
class IncompatibleArrowFunctionDefaultParameterTypeRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new IncompatibleArrowFunctionDefaultParameterTypeRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
if (PHP_VERSION_ID < 70400) { | ||
$this->markTestSkipped('Test requires PHP 7.4.'); | ||
} | ||
$this->analyse([__DIR__ . '/data/incompatible-default-parameter-type-arrow-functions.php'], [ | ||
[ | ||
'Default value of the parameter #1 $i (string) of anonymous function is incompatible with type int.', | ||
13, | ||
], | ||
]); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
tests/PHPStan/Rules/Functions/IncompatibleClosureFunctionDefaultParameterTypeRuleTest.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,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<IncompatibleClosureDefaultParameterTypeRule> | ||
*/ | ||
class IncompatibleClosureFunctionDefaultParameterTypeRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new IncompatibleClosureDefaultParameterTypeRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
if (PHP_VERSION_ID < 70400) { | ||
$this->markTestSkipped('Test requires PHP 7.4.'); | ||
} | ||
$this->analyse([__DIR__ . '/data/incompatible-default-parameter-type-closure.php'], [ | ||
[ | ||
'Default value of the parameter #1 $i (string) of anonymous function is incompatible with type int.', | ||
19, | ||
], | ||
]); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
tests/PHPStan/Rules/Functions/data/incompatible-default-parameter-type-arrow-functions.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,16 @@ | ||
<?php // lint >= 7.4 | ||
|
||
namespace IncompatibleArrowFunctionDefaultParameterType; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(): void | ||
{ | ||
$f = fn (int $i = null) => '1'; | ||
$g = fn (?int $i = null) => '1'; | ||
$h = fn (int $i = 5) => '1'; | ||
$i = fn (int $i = 'foo') => '1'; | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
tests/PHPStan/Rules/Functions/data/incompatible-default-parameter-type-closure.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,24 @@ | ||
<?php // lint >= 7.4 | ||
|
||
namespace IncompatibleClosureDefaultParameterType; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(): void | ||
{ | ||
$f = function (int $i = null) { | ||
return '1'; | ||
}; | ||
$g = function (?int $i = null) { | ||
return '1'; | ||
}; | ||
$h = function (int $i = 5) { | ||
return '1'; | ||
}; | ||
$i = function (int $i = 'foo') { | ||
return '1'; | ||
}; | ||
} | ||
|
||
} |