-
Notifications
You must be signed in to change notification settings - Fork 430
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
fix switch(enum) #2687
Merged
Merged
fix switch(enum) #2687
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,24 @@ | ||
<?php declare(strict_types=1); // lint >= 8.1 | ||
|
||
namespace Bug3488; | ||
|
||
enum EnumWithThreeCases { | ||
case ValueA; | ||
case ValueB; | ||
case ValueC; | ||
} | ||
|
||
function testFunction(EnumWithThreeCases $var) : int | ||
{ | ||
switch ($var) { | ||
case EnumWithThreeCases::ValueA: | ||
// some other code | ||
return 1; | ||
case EnumWithThreeCases::ValueB: | ||
// some other code | ||
return 2; | ||
case EnumWithThreeCases::ValueC: | ||
// some other code | ||
return 3; | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
tests/PHPStan/Rules/Variables/data/defined-variables-enum.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,28 @@ | ||
<?php // lint >= 8.1 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DefinedVariablesEnum; | ||
|
||
enum Foo | ||
{ | ||
case A; | ||
case B; | ||
} | ||
|
||
class HelloWorld | ||
{ | ||
public function sayHello(Foo $f): void | ||
{ | ||
switch ($f) { | ||
case Foo::A: | ||
$i = 5; | ||
break; | ||
case Foo::B: | ||
$i = 6; | ||
break; | ||
} | ||
|
||
var_dump($i); | ||
} | ||
} |
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.
Hi, I'm sorry I haven't reviewed this yet, but I saw this and have been thinking about it. I mostly like it but since the last time this code was touched, I introduced
Type::getFiniteTypes()
which is for these situations - when getFiniteTypes return a single object.It works for enums, scalars, constant arrays. I wonder if this condition could be simplified with this method.
I'm also worried about many of the failures with the memory limit, but maybe it's not fault of this PR.
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.
Hi, thanks for the feedback - I didn't know about
getFiniteTypes
. However, I'm not sure that it is applicable here.What I'm trying to express by this code is that
$a == $b
is equivalent to$a === $b
, when both$a
and$b
are enums (actually it could be more general - https://3v4l.org/Huc7t). It doesn't matter whether it's anEnumCaseObjectType
orObjectType
(maybe I'm just misunderstanding "when getFiniteTypes return a single object"). Furthermore, I want to avoid scalar types (e.g.5 == "5"
).I see that
count($t->getFiniteTypes()) === 1
is already used insideresolveIdentical
. So my change does indirectly benefit from it.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.
As for the memory limit: as far as I can see the memory limit is only exceeded for static analysis on PHP 7.2 (the other failures are due to a patch not applying on windows which I assume cannot be my fault). My guess is that it was borderline before and my adding a bit of code pushed it over the edge? I tried it with 500M and it worked: https://github.com/schlndh/phpstan-src/actions/runs/6603424059/job/17936514043
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.
You're right, it'd be bad for
5 == '5'
.