-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EnforceClosureParamNativeTypehintRule (#192)
- Loading branch information
Showing
8 changed files
with
248 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\PHPStan\Rule; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Variable; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InArrowFunctionNode; | ||
use PHPStan\Node\InClosureNode; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\MixedType; | ||
use function is_string; | ||
|
||
/** | ||
* @implements Rule<Node> | ||
*/ | ||
class EnforceClosureParamNativeTypehintRule implements Rule | ||
{ | ||
|
||
private PhpVersion $phpVersion; | ||
|
||
private bool $allowMissingTypeWhenInferred; | ||
|
||
public function __construct( | ||
PhpVersion $phpVersion, | ||
bool $allowMissingTypeWhenInferred | ||
) | ||
{ | ||
$this->phpVersion = $phpVersion; | ||
$this->allowMissingTypeWhenInferred = $allowMissingTypeWhenInferred; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node::class; | ||
} | ||
|
||
/** | ||
* @return list<RuleError> | ||
*/ | ||
public function processNode( | ||
Node $node, | ||
Scope $scope | ||
): array | ||
{ | ||
if (!$node instanceof InClosureNode && !$node instanceof InArrowFunctionNode) { // @phpstan-ignore-line bc promise | ||
return []; | ||
} | ||
|
||
if ($this->phpVersion->getVersionId() < 80_000) { | ||
return []; // unable to add mixed native typehint there | ||
} | ||
|
||
$errors = []; | ||
$type = $node instanceof InClosureNode ? 'closure' : 'arrow function'; | ||
|
||
foreach ($node->getOriginalNode()->getParams() as $param) { | ||
if (!$param->var instanceof Variable || !is_string($param->var->name)) { | ||
continue; | ||
} | ||
|
||
if ($param->type !== null) { | ||
continue; | ||
} | ||
|
||
$paramType = $scope->getType($param->var); | ||
|
||
if ($this->allowMissingTypeWhenInferred && (!$paramType instanceof MixedType || $paramType->isExplicitMixed())) { | ||
continue; | ||
} | ||
|
||
$errors[] = RuleErrorBuilder::message("Missing parameter typehint for {$type} parameter \${$param->var->name}.") | ||
->identifier('shipmonk.unknownClosureParamType') | ||
->line($param->getLine()) | ||
->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
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,64 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\PHPStan\Rule; | ||
|
||
use LogicException; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use ShipMonk\PHPStan\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<EnforceClosureParamNativeTypehintRule> | ||
*/ | ||
class EnforceClosureParamNativeTypehintRuleTest extends RuleTestCase | ||
{ | ||
|
||
private ?bool $allowMissingTypeWhenInferred = null; | ||
|
||
private ?PhpVersion $phpVersion = null; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
if ($this->allowMissingTypeWhenInferred === null || $this->phpVersion === null) { | ||
throw new LogicException('Missing phpVersion or allowMissingTypeWhenInferred'); | ||
} | ||
|
||
return new EnforceClosureParamNativeTypehintRule( | ||
$this->phpVersion, | ||
$this->allowMissingTypeWhenInferred, | ||
); | ||
} | ||
|
||
public function testAllowInferring(): void | ||
{ | ||
$this->allowMissingTypeWhenInferred = true; | ||
$this->phpVersion = $this->createPhpVersion(80_000); | ||
|
||
$this->analyseFile(__DIR__ . '/data/EnforceClosureParamNativeTypehintRule/allow-inferring.php'); | ||
} | ||
|
||
public function testEnforceEverywhere(): void | ||
{ | ||
$this->allowMissingTypeWhenInferred = false; | ||
$this->phpVersion = $this->createPhpVersion(80_000); | ||
|
||
$this->analyseFile(__DIR__ . '/data/EnforceClosureParamNativeTypehintRule/enforce-everywhere.php'); | ||
} | ||
|
||
public function testNoErrorOnPhp74(): void | ||
{ | ||
$this->allowMissingTypeWhenInferred = false; | ||
$this->phpVersion = $this->createPhpVersion(70_400); | ||
|
||
self::assertEmpty($this->processActualErrors($this->gatherAnalyserErrors([ | ||
__DIR__ . '/data/EnforceClosureParamNativeTypehintRule/allow-inferring.php', | ||
__DIR__ . '/data/EnforceClosureParamNativeTypehintRule/enforce-everywhere.php', | ||
]))); | ||
} | ||
|
||
private function createPhpVersion(int $version): PhpVersion | ||
{ | ||
return new PhpVersion($version); // @phpstan-ignore-line ignore bc promise | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
tests/Rule/data/EnforceClosureParamNativeTypehintRule/allow-inferring.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,30 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ForbidImplicitMixedInClosureParamsRule; | ||
|
||
|
||
/** | ||
* @param list<string> $a | ||
* @param list<mixed> $b | ||
*/ | ||
function test($a, $b, $c, array $d): void | ||
{ | ||
array_map(function ($item) {}, [1]); | ||
array_map(function ($item) {}, $a); | ||
array_map(function ($item) {}, $b); | ||
array_map(function ($item) {}, $c); // error: Missing parameter typehint for closure parameter $item. | ||
array_map(function ($item) {}, $d); // error: Missing parameter typehint for closure parameter $item. | ||
array_map(function (int $item) {}, $c); | ||
array_map(function (int $item) {}, $d); | ||
|
||
array_map(static fn($item) => 1, [1]); | ||
array_map(static fn($item) => 1, $a); | ||
array_map(static fn($item) => 1, $b); | ||
array_map(static fn($item) => 1, $c); // error: Missing parameter typehint for arrow function parameter $item. | ||
array_map(static fn($item) => 1, $d); // error: Missing parameter typehint for arrow function parameter $item. | ||
array_map(static fn(int $item) => 1, $c); | ||
array_map(static fn(int $item) => 1, $d); | ||
|
||
function ($item2) {}; // error: Missing parameter typehint for closure parameter $item2. | ||
function (mixed $item2) {}; | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/Rule/data/EnforceClosureParamNativeTypehintRule/enforce-everywhere.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,30 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ForbidImplicitMixedInClosureParamsRule\Everywhere; | ||
|
||
|
||
/** | ||
* @param list<string> $a | ||
* @param list<mixed> $b | ||
*/ | ||
function test($a, $b, $c, array $d): void | ||
{ | ||
array_map(function ($item) {}, [1]); // error: Missing parameter typehint for closure parameter $item. | ||
array_map(function ($item) {}, $a); // error: Missing parameter typehint for closure parameter $item. | ||
array_map(function ($item) {}, $b); // error: Missing parameter typehint for closure parameter $item. | ||
array_map(function ($item) {}, $c); // error: Missing parameter typehint for closure parameter $item. | ||
array_map(function ($item) {}, $d); // error: Missing parameter typehint for closure parameter $item. | ||
array_map(function (int $item) {}, $c); | ||
array_map(function (int $item) {}, $d); | ||
|
||
array_map(static fn($item) => 1, [1]); // error: Missing parameter typehint for arrow function parameter $item. | ||
array_map(static fn($item) => 1, $a); // error: Missing parameter typehint for arrow function parameter $item. | ||
array_map(static fn($item) => 1, $b); // error: Missing parameter typehint for arrow function parameter $item. | ||
array_map(static fn($item) => 1, $c); // error: Missing parameter typehint for arrow function parameter $item. | ||
array_map(static fn($item) => 1, $d); // error: Missing parameter typehint for arrow function parameter $item. | ||
array_map(static fn(int $item) => 1, $c); | ||
array_map(static fn(int $item) => 1, $d); | ||
|
||
function ($item2) {}; // error: Missing parameter typehint for closure parameter $item2. | ||
function (mixed $item2) {}; | ||
} |
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