-
Notifications
You must be signed in to change notification settings - Fork 575
Introduce ClosureType::isStaticClosure()
#5699
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
Open
phpstan-bot
wants to merge
10
commits into
phpstan:2.1.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-hguxjyq
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3e402d0
Introduce `ClosureType::isStaticClosure()` and narrow `Closure::bindT…
VincentLanglet ad369e2
Revert `Closure::bindTo()`/`Closure::bind()` return type narrowing fo…
phpstan-bot 351f6bc
Use hyphen in static closure prefix and extract `describeWithPrefix()…
phpstan-bot d6a5ebc
Replace `describeWithPrefix()` string param with `showPure`/`showStat…
phpstan-bot bfa9fe9
Remove named parameters from describeBody() calls
phpstan-bot c0d7f15
Update AttributeReflectionTest expectations for static closures in at…
phpstan-bot e6d6c36
Add test cases for static-pure-Closure describe output
phpstan-bot b8f23a7
Only show static- prefix for common callable closures in describe()
phpstan-bot 6a9fd61
Revert describeBody() extraction, inline static- prefix in isCommonCa…
phpstan-bot 0786e7a
simplify
staabm 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
Some comments aren't visible on the classic Files Changed page.
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
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
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,62 @@ | ||
| <?php | ||
|
|
||
| namespace ClosureStaticType; | ||
|
|
||
| use Closure; | ||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| final class Foo | ||
| { | ||
|
|
||
| public function doFoo(): void | ||
| { | ||
| $static = static function (): void {}; | ||
| assertType('Closure(): void', $static); | ||
|
|
||
| $nonStatic = function (): void {}; | ||
| assertType('Closure(): void', $nonStatic); | ||
|
|
||
| $staticArrow = static fn (): int => 1; | ||
| assertType('Closure(): 1', $staticArrow); | ||
|
|
||
| $nonStaticArrow = fn (): int => 1; | ||
| assertType('Closure(): 1', $nonStaticArrow); | ||
| } | ||
|
|
||
| public function doBindTo(): void | ||
| { | ||
| $static = static function (): void {}; | ||
| assertType('Closure(): void', $static->bindTo($this)); | ||
|
|
||
| $nonStatic = function (): void {}; | ||
| assertType('Closure(): void', $nonStatic->bindTo($this)); | ||
| } | ||
|
|
||
| public function doBind(): void | ||
| { | ||
| $static = static function (): void {}; | ||
| assertType('Closure(): void', Closure::bind($static, $this)); | ||
|
|
||
| $nonStatic = function (): void {}; | ||
| assertType('Closure(): void', Closure::bind($nonStatic, $this)); | ||
| } | ||
|
|
||
| /** | ||
| * @param Closure(): void $unknownClosure | ||
| */ | ||
| public function doUnknown(Closure $unknownClosure): void | ||
| { | ||
| assertType('Closure(): void', $unknownClosure->bindTo($this)); | ||
| assertType('Closure(): void', Closure::bind($unknownClosure, $this)); | ||
| } | ||
|
|
||
| public function doFromCallable(): void | ||
| { | ||
| $fn = Closure::fromCallable(static function (): void {}); | ||
| assertType('Closure(): void', $fn->bindTo($this)); | ||
|
|
||
| $fn2 = Closure::fromCallable(function (): void {}); | ||
| assertType('Closure(): void', $fn2->bindTo($this)); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about this addition.
I think its common to pass a static closure into a function expecting a non-static one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The branch is clean and up to date. No code changes are needed for this review comment. Here's the analysis:
The
isStaticcheck inequals()is correct and consistent with the existing pattern. Theequals()method checks type identity (are these the exact same type?), not assignability (can one be used where the other is expected?). TheisPure()check — already present before this PR — follows the exact same pattern: two closures with different purity are not equal types, even though a pure closure can be passed where an impure one is expected.The reviewer's concern about assignability is already handled by
isSuperTypeOf(). TheCallableTypeHelper::isParametersAcceptorSuperTypeOf()logic and theisSuperTypeOftests show:maybe-staticclosure (the default for PHPDocClosuretypes) accepts both static and non-static closuresnon-staticclosure types reject static closures, which is correct: a function that explicitly requires a non-static closure may intend tobindTo()it, andbindTo()returnsnullfor static closuresIn practice, function parameters typed via PHPDoc as
Closure(): voidare alwaysmaybe-static, so passing a static closure is accepted. The strictisSuperTypeOf()check only triggers between two code-inferred closures with known static-ness.