Skip to content

Commit

Permalink
fix switch(enum)
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh authored and ondrejmirtes committed Oct 22, 2023
1 parent da8c7f9 commit cb91818
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
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())
) {
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);
}
}

0 comments on commit cb91818

Please sign in to comment.