From 1a4cee2080869a6f38b583069397ab9e5da5f134 Mon Sep 17 00:00:00 2001 From: Johnson Page Date: Tue, 30 Jun 2026 15:46:33 +1000 Subject: [PATCH] check maximum case length against database mapping length --- src/Rules/Doctrine/ORM/EntityColumnRule.php | 14 ++++++++++++++ tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php | 4 ++++ .../Doctrine/ORM/data-attributes/enum-type.php | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/src/Rules/Doctrine/ORM/EntityColumnRule.php b/src/Rules/Doctrine/ORM/EntityColumnRule.php index 75aa1ae6..d8f2ae83 100644 --- a/src/Rules/Doctrine/ORM/EntityColumnRule.php +++ b/src/Rules/Doctrine/ORM/EntityColumnRule.php @@ -120,6 +120,20 @@ public function processNode(Node $node, Scope $scope): array $enumReflection->getDisplayName(), $writableToDatabaseType->describe(VerbosityLevel::typeOnly()), ))->identifier('doctrine.enumType')->build(); + } elseif ($backedEnumType->isString()->yes()) { + $databaseLength = $fieldMapping['length'] ?? 255; + $cases = $enumTypeString::cases(); + $maxLength = max(array_map(fn ($case): int => strlen($case->value), $cases) ?: [0]); + if ($maxLength > $databaseLength) { + $errors[] = RuleErrorBuilder::message(sprintf( + 'Property %s::$%s length mismatch: maximum value length %s for enum %s exceeds database column length %s.', + $className, + $propertyName, + $maxLength, + $enumReflection->getDisplayName(), + $databaseLength, + ))->identifier('doctrine.enumStringLength')->build(); + } } } } diff --git a/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php b/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php index a29a1d57..dbee3274 100644 --- a/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php +++ b/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php @@ -445,6 +445,10 @@ public function testEnumType(?string $objectManagerLoader): void 'Property PHPStan\Rules\Doctrine\ORMAttributes\Foo::$type7 type mapping mismatch: backing type int of enum PHPStan\Rules\Doctrine\ORMAttributes\BazEnum does not match value type string of the database type array.', 63, ], + [ + 'Property PHPStan\Rules\Doctrine\ORMAttributes\Foo::$type8 length mismatch: maximum value length 3 for enum PHPStan\Rules\Doctrine\ORMAttributes\FooEnum exceeds database column length 1.', + 66, + ] ]); } diff --git a/tests/Rules/Doctrine/ORM/data-attributes/enum-type.php b/tests/Rules/Doctrine/ORM/data-attributes/enum-type.php index 50e0a2ec..0c8de669 100644 --- a/tests/Rules/Doctrine/ORM/data-attributes/enum-type.php +++ b/tests/Rules/Doctrine/ORM/data-attributes/enum-type.php @@ -61,4 +61,10 @@ class Foo */ #[ORM\Column(type: "simple_array", enumType: BazEnum::class)] public array $type7; + + #[ORM\Column(type: "string", length: 1, enumType: FooEnum::class)] + public FooEnum $type8; + + #[ORM\Column(type: "string", length: 3, enumType: FooEnum::class)] + public FooEnum $type9; }