Skip to content
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 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public function specifyTypesInCondition(
($leftType->isString()->yes() && $rightType->isString()->yes())
|| ($integerType->isSuperTypeOf($leftType)->yes() && $integerType->isSuperTypeOf($rightType)->yes())
|| ($floatType->isSuperTypeOf($leftType)->yes() && $floatType->isSuperTypeOf($rightType)->yes())
|| ($leftType->isEnum()->yes() && $rightType->isEnum()->yes())
Copy link
Member

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.

Copy link
Contributor Author

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 an EnumCaseObjectType or ObjectType (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 inside resolveIdentical. So my change does indirectly benefit from it.

Copy link
Contributor Author

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

Copy link
Member

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'.

) {
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr);
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/data/enums.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ public function testEquality(Foo $foo, Bar $bar, Baz $baz, string $s, int $i, bo
assertType('false', false == Foo::ONE);
assertType('false', null == Foo::ONE);
assertType('false', Foo::ONE == null);
assertType('true', $foo == Foo::ONE || Foo::TWO == $foo);

assertType('bool', (rand() ? $bar : null) == $s);
assertType('bool', $s == (rand() ? $bar : null));
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Missing/MissingReturnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ public function testMissingMixedReturnInEmptyBody(): void
]);
}

public function testBug3488(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->checkExplicitMixedMissingReturn = true;
$this->analyse([__DIR__ . '/data/bug-3488.php'], []);
}

public function testBug3669(): void
{
$this->checkExplicitMixedMissingReturn = true;
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Missing/data/bug-3488.php
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;
}
}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,19 @@ public function testBug9474(): void
$this->analyse([__DIR__ . '/data/bug-9474.php'], []);
}

public function testEnum(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/defined-variables-enum.php'], []);
}

public function testBug5326(): void
{
$this->cliArgumentsVariablesRegistered = true;
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Variables/data/defined-variables-enum.php
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);
}
}