|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Andante\NullableEmbeddableBundle\PHPStan\Rules; |
| 6 | + |
| 7 | +use Andante\NullableEmbeddableBundle\Attribute\NullableEmbeddable; |
| 8 | +use Doctrine\ORM\Mapping\Column; |
| 9 | +use Doctrine\ORM\Mapping\Embeddable; |
| 10 | +use Doctrine\ORM\Mapping\Embedded; |
| 11 | +use PhpParser\Node; |
| 12 | +use PHPStan\Analyser\Scope; |
| 13 | +use PHPStan\Node\InClassNode; |
| 14 | +use PHPStan\Rules\Rule; |
| 15 | +use PHPStan\Rules\RuleErrorBuilder; |
| 16 | + |
| 17 | +/** |
| 18 | + * @implements Rule<InClassNode> |
| 19 | + */ |
| 20 | +class NullableEmbeddablePropertyRule implements Rule |
| 21 | +{ |
| 22 | + public function getNodeType(): string |
| 23 | + { |
| 24 | + return InClassNode::class; |
| 25 | + } |
| 26 | + |
| 27 | + public function processNode(Node $node, Scope $scope): array |
| 28 | + { |
| 29 | + $classReflection = $node->getClassReflection(); |
| 30 | + |
| 31 | + // Only check classes with both Embeddable and NullableEmbeddable attributes |
| 32 | + if (!$this->hasEmbeddableAttribute($classReflection->getName())) { |
| 33 | + return []; |
| 34 | + } |
| 35 | + |
| 36 | + if (!$this->hasNullableEmbeddableAttribute($classReflection->getName())) { |
| 37 | + return []; |
| 38 | + } |
| 39 | + |
| 40 | + $errors = []; |
| 41 | + $reflectionClass = new \ReflectionClass($classReflection->getName()); |
| 42 | + |
| 43 | + foreach ($reflectionClass->getProperties() as $property) { |
| 44 | + // Skip static properties |
| 45 | + if ($property->isStatic()) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + // Rule 1: Properties with non-null default values outside constructor |
| 50 | + if ($this->hasNonNullDefaultValue($property, $reflectionClass)) { |
| 51 | + $errors[] = RuleErrorBuilder::message( |
| 52 | + \sprintf( |
| 53 | + 'Property %s::$%s in a NullableEmbeddable class has a non-null default value outside the constructor. '. |
| 54 | + 'Initialize it in the constructor instead to avoid issues with Doctrine hydration (Doctrine skips constructors during hydration).', |
| 55 | + $classReflection->getName(), |
| 56 | + $property->getName() |
| 57 | + ) |
| 58 | + )->identifier('nullableEmbeddable.propertyInitialization')->build(); |
| 59 | + } |
| 60 | + |
| 61 | + // Rule 2: Properties with Column mapping must be nullable |
| 62 | + // Either explicitly via nullable=true OR implicitly via PHP nullable type |
| 63 | + $columnAttribute = $this->getColumnAttribute($property); |
| 64 | + if (null !== $columnAttribute) { |
| 65 | + $isExplicitlyNullable = $this->isColumnNullable($columnAttribute); |
| 66 | + $propertyType = $property->getType(); |
| 67 | + $isPhpNullable = $propertyType instanceof \ReflectionNamedType && $propertyType->allowsNull(); |
| 68 | + |
| 69 | + // Only flag if both explicit attribute AND PHP type are not nullable |
| 70 | + if (!$isExplicitlyNullable && !$isPhpNullable) { |
| 71 | + $errors[] = RuleErrorBuilder::message( |
| 72 | + \sprintf( |
| 73 | + 'Property %s::$%s in a NullableEmbeddable class must be nullable (either via nullable=true in #[Column] or ?Type). '. |
| 74 | + 'When the embeddable object is null, Doctrine will set all its database columns to NULL.', |
| 75 | + $classReflection->getName(), |
| 76 | + $property->getName() |
| 77 | + ) |
| 78 | + )->identifier('nullableEmbeddable.columnNullable')->build(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Rule 3: Embedded properties with explicit non-null defaults must be nullable |
| 83 | + // Note: Uninitialized embedded properties are fine - they remain uninitialized when parent is null |
| 84 | + $embeddedAttribute = $this->getEmbeddedAttribute($property); |
| 85 | + if (null !== $embeddedAttribute && $property->hasDefaultValue()) { |
| 86 | + $defaultValue = $property->getDefaultValue(); |
| 87 | + $propertyType = $property->getType(); |
| 88 | + |
| 89 | + // Only flag if there's an explicit non-null default value at class level |
| 90 | + if (null !== $defaultValue && $propertyType instanceof \ReflectionNamedType && !$propertyType->allowsNull()) { |
| 91 | + $errors[] = RuleErrorBuilder::message( |
| 92 | + \sprintf( |
| 93 | + 'Property %s::$%s in a NullableEmbeddable class is an embedded object with a non-null default value and must be nullable. '. |
| 94 | + 'When the parent embeddable is null, all nested embeddables with default values must accept null.', |
| 95 | + $classReflection->getName(), |
| 96 | + $property->getName() |
| 97 | + ) |
| 98 | + )->identifier('nullableEmbeddable.embeddedNullable')->build(); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return $errors; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @param class-string $className |
| 108 | + */ |
| 109 | + private function hasEmbeddableAttribute(string $className): bool |
| 110 | + { |
| 111 | + try { |
| 112 | + $reflectionClass = new \ReflectionClass($className); |
| 113 | + |
| 114 | + return !empty($reflectionClass->getAttributes(Embeddable::class)); |
| 115 | + } catch (\ReflectionException) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @param class-string $className |
| 122 | + */ |
| 123 | + private function hasNullableEmbeddableAttribute(string $className): bool |
| 124 | + { |
| 125 | + try { |
| 126 | + $reflectionClass = new \ReflectionClass($className); |
| 127 | + |
| 128 | + return !empty($reflectionClass->getAttributes(NullableEmbeddable::class)); |
| 129 | + } catch (\ReflectionException) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @param \ReflectionClass<object> $reflectionClass |
| 136 | + */ |
| 137 | + private function hasNonNullDefaultValue(\ReflectionProperty $property, \ReflectionClass $reflectionClass): bool |
| 138 | + { |
| 139 | + // Check if property has a default value |
| 140 | + if (!$property->hasDefaultValue()) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + $defaultValue = $property->getDefaultValue(); |
| 145 | + |
| 146 | + // If default value is null, that's fine |
| 147 | + if (null === $defaultValue) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + // Check if this property is initialized in the constructor |
| 152 | + if ($this->isInitializedInConstructor($property, $reflectionClass)) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + // Non-null default value outside constructor |
| 157 | + return true; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @param \ReflectionClass<object> $reflectionClass |
| 162 | + */ |
| 163 | + private function isInitializedInConstructor(\ReflectionProperty $property, \ReflectionClass $reflectionClass): bool |
| 164 | + { |
| 165 | + $constructor = $reflectionClass->getConstructor(); |
| 166 | + |
| 167 | + if (null === $constructor) { |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + // Check if property is a constructor parameter (promoted property) |
| 172 | + foreach ($constructor->getParameters() as $param) { |
| 173 | + if ($param->getName() === $property->getName() && $param->isPromoted()) { |
| 174 | + return true; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return false; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @return \ReflectionAttribute<Column>|null |
| 183 | + */ |
| 184 | + private function getColumnAttribute(\ReflectionProperty $property): ?\ReflectionAttribute |
| 185 | + { |
| 186 | + $attributes = $property->getAttributes(Column::class); |
| 187 | + |
| 188 | + return $attributes[0] ?? null; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @param \ReflectionAttribute<Column> $columnAttribute |
| 193 | + */ |
| 194 | + private function isColumnNullable(\ReflectionAttribute $columnAttribute): bool |
| 195 | + { |
| 196 | + $arguments = $columnAttribute->getArguments(); |
| 197 | + |
| 198 | + // Check named argument |
| 199 | + if (isset($arguments['nullable'])) { |
| 200 | + return true === $arguments['nullable']; |
| 201 | + } |
| 202 | + |
| 203 | + // Column mapping defaults to nullable: false |
| 204 | + return false; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * @return \ReflectionAttribute<Embedded>|null |
| 209 | + */ |
| 210 | + private function getEmbeddedAttribute(\ReflectionProperty $property): ?\ReflectionAttribute |
| 211 | + { |
| 212 | + $attributes = $property->getAttributes(Embedded::class); |
| 213 | + |
| 214 | + return $attributes[0] ?? null; |
| 215 | + } |
| 216 | +} |
0 commit comments