Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions src/Rules/Doctrine/ORM/EntityColumnRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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 = [];
Expand Down
17 changes: 17 additions & 0 deletions tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'], []);
}

}
27 changes: 27 additions & 0 deletions tests/Rules/Doctrine/ORM/data/bug-679.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php // lint >= 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;
}
Loading