-
Notifications
You must be signed in to change notification settings - Fork 0
feat(phpstan): add MissingClosureReturnTypehintRule #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3fbfffc
feat(phpstan): add MissingClosureReturnTypehintRule
spawnia 4f77b3e
refactor(phpstan): extract ClosureTypehintRule base for closure rules
spawnia efbb895
chore(phpstan): self-apply MissingClosureReturnTypehintRule
spawnia 6cdbf74
perf(phpstan): narrow closure rule node type to FunctionLike
spawnia 0a7371a
test(phpstan): cover MissingClosureParameterTypehintRule
spawnia 38b4f26
fix(phpstan): label arrow functions correctly and pin closure filter
spawnia 2976649
fix(phpstan): report closure parameter errors on the parameter line
spawnia dca5fdb
refactor(phpstan): hoist parameter name extraction above its guard
spawnia 5ead3a0
test(phpstan): drop redundant comment from closure fixtures
spawnia 9cbba88
fix(phpstan): update PHP 7.4/8.0 ignores for renamed closure rule mem…
spawnia cb096dc
Update .php-cs-fixer.php
spawnia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,51 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace MLL\Utils\PHPStan\Rules; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\ArrowFunction; | ||
| use PhpParser\Node\Expr\Closure; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Rules\IdentifierRuleError; | ||
| use PHPStan\Rules\Rule; | ||
|
|
||
| /** | ||
| * @implements Rule<Node\FunctionLike> | ||
| */ | ||
| abstract class ClosureTypehintRule implements Rule | ||
| { | ||
| /** | ||
| * @param Closure|ArrowFunction $closure | ||
| * | ||
| * @return list<IdentifierRuleError> | ||
| */ | ||
| abstract protected function processClosure(Node\FunctionLike $closure): array; | ||
|
|
||
| /** @param Closure|ArrowFunction $closure */ | ||
| protected function closureKind(Node\FunctionLike $closure): string | ||
| { | ||
| return $closure instanceof ArrowFunction | ||
| ? 'Arrow function' | ||
| : 'Closure'; | ||
| } | ||
|
|
||
| /** @return class-string<Node\FunctionLike> */ | ||
| public function getNodeType(): string | ||
| { | ||
| return Node\FunctionLike::class; | ||
| } | ||
|
|
||
| /** | ||
| * @param Node\FunctionLike $node | ||
| * | ||
| * @return list<IdentifierRuleError> | ||
| */ | ||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (! $node instanceof Closure && ! $node instanceof ArrowFunction) { | ||
| return []; | ||
| } | ||
|
|
||
| return $this->processClosure($node); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,29 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace MLL\Utils\PHPStan\Rules; | ||
|
|
||
| use PhpParser\Node; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
|
|
||
| /** | ||
| * Assumes PHP 8.0+, where every return type is natively expressible. | ||
| * On PHP 7.4 a closure returning `mixed` has no native type to declare, | ||
| * which is why `phpstan/include-by-php-version.php` gates `rules.neon`. | ||
| */ | ||
| final class MissingClosureReturnTypehintRule extends ClosureTypehintRule | ||
| { | ||
| protected function processClosure(Node\FunctionLike $closure): array | ||
| { | ||
| if ($closure->getReturnType() instanceof Node) { | ||
| return []; | ||
| } | ||
|
|
||
| $kind = $this->closureKind($closure); | ||
|
|
||
| return [ | ||
| RuleErrorBuilder::message("{$kind} is missing a native return type hint.") | ||
| ->identifier('missingType.closureReturn') | ||
| ->build(), | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or 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,28 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace MLL\Utils\Tests\PHPStan; | ||
|
|
||
| use MLL\Utils\PHPStan\Rules\MissingClosureParameterTypehintRule; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<MissingClosureParameterTypehintRule> | ||
| */ | ||
| final class MissingClosureParameterTypehintRuleTest extends RuleTestCase | ||
| { | ||
| protected function getRule(): Rule | ||
| { | ||
| return new MissingClosureParameterTypehintRule(); | ||
| } | ||
|
|
||
| public function testMissingParameterTypes(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/closure-parameter-types.php'], [ | ||
| ['Closure parameter factor is missing a native type hint.', 3], | ||
| ['Arrow function parameter factor is missing a native type hint.', 7], | ||
| ['Closure parameter first is missing a native type hint.', 16], | ||
| ['Closure parameter second is missing a native type hint.', 17], | ||
| ]); | ||
| } | ||
| } |
This file contains hidden or 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,26 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace MLL\Utils\Tests\PHPStan; | ||
|
|
||
| use MLL\Utils\PHPStan\Rules\MissingClosureReturnTypehintRule; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<MissingClosureReturnTypehintRule> | ||
| */ | ||
| final class MissingClosureReturnTypehintRuleTest extends RuleTestCase | ||
| { | ||
| protected function getRule(): Rule | ||
| { | ||
| return new MissingClosureReturnTypehintRule(); | ||
| } | ||
|
|
||
| public function testMissingReturnTypes(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/closure-return-types.php'], [ | ||
| ['Closure is missing a native return type hint.', 3], | ||
| ['Arrow function is missing a native return type hint.', 7], | ||
| ]); | ||
| } | ||
| } |
This file contains hidden or 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); | ||
|
|
||
| $missingClosureParameter = static function ($factor): int { | ||
| return 2; | ||
| }; | ||
|
|
||
| $missingArrowParameter = static fn ($factor): int => 2; | ||
|
|
||
| $typedClosureParameter = static function (int $factor): int { | ||
| return 2 * $factor; | ||
| }; | ||
|
|
||
| $typedArrowParameter = static fn (int $factor): int => 2 * $factor; | ||
|
|
||
| $missingMultiLineParameters = static function ( | ||
| $first, | ||
| $second | ||
| ): int { | ||
| return 2; | ||
| }; | ||
|
|
||
| function plainFunctionWithoutParameterType($factor): int | ||
| { | ||
| return 2; | ||
| } | ||
|
|
||
| class MethodWithoutParameterType | ||
| { | ||
| public function untyped($factor): int | ||
| { | ||
| return 2; | ||
| } | ||
| } |
This file contains hidden or 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,26 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| $missingClosure = static function (int $value) { | ||
| return $value * 2; | ||
| }; | ||
|
|
||
| $missingArrow = static fn (int $value) => $value * 2; | ||
|
|
||
| $typedClosure = static function (int $value): int { | ||
| return $value * 2; | ||
| }; | ||
|
|
||
| $typedArrow = static fn (int $value): int => $value * 2; | ||
|
|
||
| function plainFunctionWithoutReturnType() | ||
| { | ||
| return 1; | ||
| } | ||
|
|
||
| class MethodWithoutReturnType | ||
| { | ||
| public function untyped() | ||
| { | ||
| return 1; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.