Skip to content

Commit 934f573

Browse files
VincentLangletondrejmirtes
authored andcommitted
Fix enum type
1 parent eeff198 commit 934f573

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

src/Rules/Doctrine/ORM/EntityColumnRule.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,6 @@ public function processNode(Node $node, Scope $scope): array
105105
$writableToPropertyType = $descriptor->getWritableToPropertyType();
106106
$writableToDatabaseType = $descriptor->getWritableToDatabaseType();
107107

108-
if ($fieldMapping['type'] === 'enum') {
109-
$values = $fieldMapping['options']['values'] ?? null;
110-
if (is_array($values)) {
111-
$enumTypes = [];
112-
foreach ($values as $value) {
113-
if (!is_string($value)) {
114-
$enumTypes = [];
115-
break;
116-
}
117-
118-
$enumTypes[] = new ConstantStringType($value);
119-
}
120-
121-
if (count($enumTypes) > 0) {
122-
$writableToPropertyType = new UnionType($enumTypes);
123-
$writableToDatabaseType = new UnionType($enumTypes);
124-
}
125-
}
126-
}
127-
128108
$enumTypeString = $fieldMapping['enumType'] ?? null;
129109
if ($enumTypeString !== null) {
130110
if ($writableToDatabaseType->isArray()->no() && $writableToPropertyType->isArray()->no()) {
@@ -179,6 +159,24 @@ public function processNode(Node $node, Scope $scope): array
179159
), ...TypeUtils::getAccessoryTypes($writableToDatabaseType));
180160

181161
}
162+
} elseif ($fieldMapping['type'] === 'enum') {
163+
$values = $fieldMapping['options']['values'] ?? null;
164+
if (is_array($values)) {
165+
$enumTypes = [];
166+
foreach ($values as $value) {
167+
if (!is_string($value)) {
168+
$enumTypes = [];
169+
break;
170+
}
171+
172+
$enumTypes[] = new ConstantStringType($value);
173+
}
174+
175+
if (count($enumTypes) > 0) {
176+
$writableToPropertyType = new UnionType($enumTypes);
177+
$writableToDatabaseType = new UnionType($enumTypes);
178+
}
179+
}
182180
}
183181

184182
$identifiers = [];

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,21 @@ public function testBug677(?string $objectManagerLoader): void
461461
$this->analyse([__DIR__ . '/data/bug-677.php'], []);
462462
}
463463

464+
/**
465+
* @dataProvider dataObjectManagerLoader
466+
*/
467+
public function testBug679(?string $objectManagerLoader): void
468+
{
469+
if (PHP_VERSION_ID < 80100) {
470+
self::markTestSkipped('Test requires PHP 8.1');
471+
}
472+
if (!class_exists(\Doctrine\DBAL\Types\EnumType::class)) {
473+
self::markTestSkipped('Test requires EnumType.');
474+
}
475+
476+
$this->allowNullablePropertyForRequiredField = false;
477+
$this->objectManagerLoader = $objectManagerLoader;
478+
$this->analyse([__DIR__ . '/data/bug-679.php'], []);
479+
}
480+
464481
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php // lint >= 8.1
2+
3+
namespace PHPStan\Rules\Doctrine\ORM\Bug679;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
enum FooEnum: string {
8+
9+
case ONE = 'one';
10+
case TWO = 'two';
11+
12+
}
13+
14+
#[ORM\Entity]
15+
class MyBrokenEntity
16+
{
17+
/**
18+
* @var int|null
19+
*/
20+
#[ORM\Id]
21+
#[ORM\GeneratedValue]
22+
#[ORM\Column(type: 'integer')]
23+
private $id;
24+
25+
#[ORM\Column(type: "enum", enumType: FooEnum::class)]
26+
public FooEnum $type1;
27+
}

0 commit comments

Comments
 (0)