Skip to content

Commit cd42b17

Browse files
Fix PHPStan crashing with single value enum
1 parent 8f083bf commit cd42b17

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/Rules/Doctrine/ORM/EntityColumnRule.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use PHPStan\Type\TypehintHelper;
2323
use PHPStan\Type\TypeTraverser;
2424
use PHPStan\Type\TypeUtils;
25-
use PHPStan\Type\UnionType;
2625
use PHPStan\Type\VerbosityLevel;
2726
use Throwable;
2827
use function count;
@@ -173,8 +172,9 @@ public function processNode(Node $node, Scope $scope): array
173172
}
174173

175174
if (count($enumTypes) > 0) {
176-
$writableToPropertyType = new UnionType($enumTypes);
177-
$writableToDatabaseType = new UnionType($enumTypes);
175+
$unionType = TypeCombinator::union(...$enumTypes);
176+
$writableToPropertyType = $unionType;
177+
$writableToDatabaseType = $unionType;
178178
}
179179
}
180180
}

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,4 +478,21 @@ public function testBug679(?string $objectManagerLoader): void
478478
$this->analyse([__DIR__ . '/data/bug-679.php'], []);
479479
}
480480

481+
/**
482+
* @dataProvider dataObjectManagerLoader
483+
*/
484+
public function testBugSingleEnum(?string $objectManagerLoader): void
485+
{
486+
if (PHP_VERSION_ID < 80100) {
487+
self::markTestSkipped('Test requires PHP 8.1');
488+
}
489+
if (!class_exists(\Doctrine\DBAL\Types\EnumType::class)) {
490+
self::markTestSkipped('Test requires EnumType.');
491+
}
492+
493+
$this->allowNullablePropertyForRequiredField = false;
494+
$this->objectManagerLoader = $objectManagerLoader;
495+
$this->analyse([__DIR__ . '/data/bug-single-enum.php'], []);
496+
}
497+
481498
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php // lint >= 8.1
2+
3+
namespace PHPStan\Rules\Doctrine\ORM\BugSingleEnum;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
#[ORM\Entity]
8+
class MyBrokenEntity
9+
{
10+
public const LOGIN_METHOD_BASIC_AUTH = 'BasicAuth';
11+
12+
public const LOGIN_METHODS = [
13+
self::LOGIN_METHOD_BASIC_AUTH,
14+
];
15+
16+
/**
17+
* @var int|null
18+
*/
19+
#[ORM\Id]
20+
#[ORM\GeneratedValue]
21+
#[ORM\Column(type: 'integer')]
22+
private $id;
23+
24+
/**
25+
* @var self::LOGIN_METHOD_*
26+
*/
27+
#[ORM\Column(name: 'login_method', type: 'enum', options: ['default' => self::LOGIN_METHOD_BASIC_AUTH, 'values' => self::LOGIN_METHODS])]
28+
private string $loginMethod = self::LOGIN_METHOD_BASIC_AUTH;
29+
}

0 commit comments

Comments
 (0)