diff --git a/src/Rules/Doctrine/ORM/EntityColumnRule.php b/src/Rules/Doctrine/ORM/EntityColumnRule.php index 5a9f5fff..aa8343b7 100644 --- a/src/Rules/Doctrine/ORM/EntityColumnRule.php +++ b/src/Rules/Doctrine/ORM/EntityColumnRule.php @@ -105,26 +105,6 @@ public function processNode(Node $node, Scope $scope): array $writableToPropertyType = $descriptor->getWritableToPropertyType(); $writableToDatabaseType = $descriptor->getWritableToDatabaseType(); - if ($fieldMapping['type'] === 'enum') { - $values = $fieldMapping['options']['values'] ?? null; - if (is_array($values)) { - $enumTypes = []; - foreach ($values as $value) { - if (!is_string($value)) { - $enumTypes = []; - break; - } - - $enumTypes[] = new ConstantStringType($value); - } - - if (count($enumTypes) > 0) { - $writableToPropertyType = new UnionType($enumTypes); - $writableToDatabaseType = new UnionType($enumTypes); - } - } - } - $enumTypeString = $fieldMapping['enumType'] ?? null; if ($enumTypeString !== null) { if ($writableToDatabaseType->isArray()->no() && $writableToPropertyType->isArray()->no()) { @@ -179,6 +159,24 @@ public function processNode(Node $node, Scope $scope): array ), ...TypeUtils::getAccessoryTypes($writableToDatabaseType)); } + } elseif ($fieldMapping['type'] === 'enum') { + $values = $fieldMapping['options']['values'] ?? null; + if (is_array($values)) { + $enumTypes = []; + foreach ($values as $value) { + if (!is_string($value)) { + $enumTypes = []; + break; + } + + $enumTypes[] = new ConstantStringType($value); + } + + if (count($enumTypes) > 0) { + $writableToPropertyType = new UnionType($enumTypes); + $writableToDatabaseType = new UnionType($enumTypes); + } + } } $identifiers = []; diff --git a/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php b/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php index a39f6151..c55b5cd5 100644 --- a/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php +++ b/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php @@ -461,4 +461,21 @@ public function testBug677(?string $objectManagerLoader): void $this->analyse([__DIR__ . '/data/bug-677.php'], []); } + /** + * @dataProvider dataObjectManagerLoader + */ + public function testBug679(?string $objectManagerLoader): void + { + if (PHP_VERSION_ID < 80100) { + self::markTestSkipped('Test requires PHP 8.1'); + } + if (!class_exists(\Doctrine\DBAL\Types\EnumType::class)) { + self::markTestSkipped('Test requires EnumType.'); + } + + $this->allowNullablePropertyForRequiredField = false; + $this->objectManagerLoader = $objectManagerLoader; + $this->analyse([__DIR__ . '/data/bug-679.php'], []); + } + } diff --git a/tests/Rules/Doctrine/ORM/data/bug-679.php b/tests/Rules/Doctrine/ORM/data/bug-679.php new file mode 100644 index 00000000..f3f4b23c --- /dev/null +++ b/tests/Rules/Doctrine/ORM/data/bug-679.php @@ -0,0 +1,27 @@ += 8.1 + +namespace PHPStan\Rules\Doctrine\ORM\Bug679; + +use Doctrine\ORM\Mapping as ORM; + +enum FooEnum: string { + + case ONE = 'one'; + case TWO = 'two'; + +} + +#[ORM\Entity] +class MyBrokenEntity +{ + /** + * @var int|null + */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] + private $id; + + #[ORM\Column(type: "enum", enumType: FooEnum::class)] + public FooEnum $type1; +}