Skip to content

Commit e745610

Browse files
committed
EntityColumnRule - do not complain about array type columns even with narrower property type
1 parent b868d39 commit e745610

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/Rules/Doctrine/ORM/EntityColumnRule.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\MissingPropertyFromReflectionException;
88
use PHPStan\Rules\Rule;
9+
use PHPStan\Type\ArrayType;
910
use PHPStan\Type\Doctrine\DescriptorNotRegisteredException;
1011
use PHPStan\Type\Doctrine\DescriptorRegistry;
1112
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
1213
use PHPStan\Type\ErrorType;
1314
use PHPStan\Type\MixedType;
1415
use PHPStan\Type\NeverType;
16+
use PHPStan\Type\Type;
1517
use PHPStan\Type\TypeCombinator;
18+
use PHPStan\Type\TypeTraverser;
1619
use PHPStan\Type\VerbosityLevel;
1720
use Throwable;
1821
use function sprintf;
@@ -125,6 +128,16 @@ public function processNode(Node $node, Scope $scope): array
125128
return [];
126129
}
127130

131+
$transformArrays = function (Type $type, callable $traverse): Type {
132+
if ($type instanceof ArrayType) {
133+
return new ArrayType(new MixedType(), new MixedType());
134+
}
135+
136+
return $traverse($type);
137+
};
138+
139+
$propertyWritableType = TypeTraverser::map($propertyWritableType, $transformArrays);
140+
128141
if (!$propertyWritableType->isSuperTypeOf($writableToPropertyType)->yes()) {
129142
$errors[] = sprintf(
130143
'Property %s::$%s type mapping mismatch: database can contain %s but property expects %s.',
@@ -134,7 +147,7 @@ public function processNode(Node $node, Scope $scope): array
134147
$property->getWritableType()->describe(VerbosityLevel::typeOnly())
135148
);
136149
}
137-
$propertyReadableType = $property->getReadableType();
150+
$propertyReadableType = TypeTraverser::map($property->getReadableType(), $transformArrays);
138151
if (!$writableToDatabaseType->isSuperTypeOf($identifier === $propertyName && !$nullable ? TypeCombinator::removeNull($propertyReadableType) : $propertyReadableType)->yes()) {
139152
$errors[] = sprintf(
140153
'Property %s::$%s type mapping mismatch: property can contain %s but database expects %s.',

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Rules\Rule;
88
use PHPStan\Testing\RuleTestCase;
99
use PHPStan\Type\Doctrine\DescriptorRegistry;
10+
use PHPStan\Type\Doctrine\Descriptors\ArrayType;
1011
use PHPStan\Type\Doctrine\Descriptors\BigIntType;
1112
use PHPStan\Type\Doctrine\Descriptors\BinaryType;
1213
use PHPStan\Type\Doctrine\Descriptors\DateTimeImmutableType;
@@ -46,6 +47,7 @@ protected function getRule(): Rule
4647
new ReflectionDescriptor(CustomType::class, $this->createBroker()),
4748
new DateType(),
4849
new UuidTypeDescriptor(UuidType::class),
50+
new ArrayType(),
4951
]),
5052
true
5153
);
@@ -86,6 +88,14 @@ public function testRule(): void
8688
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$uuidInvalidType type mapping mismatch: property can contain int but database expects Ramsey\Uuid\UuidInterface|string.',
8789
72,
8890
],
91+
[
92+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$arrayOrNull type mapping mismatch: property can contain array|null but database expects array.',
93+
96,
94+
],
95+
[
96+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$arrayOfIntegersOrNull type mapping mismatch: property can contain array|null but database expects array.',
97+
102,
98+
],
8999
]);
90100
}
91101

tests/Rules/Doctrine/ORM/data/MyBrokenEntity.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,34 @@ class MyBrokenEntity extends MyBrokenSuperclass
7171
*/
7272
private $uuidInvalidType;
7373

74+
/**
75+
* @ORM\Column(type="array")
76+
* @var int[]
77+
*/
78+
private $arrayOfIntegers;
79+
80+
/**
81+
* @ORM\Column(type="array")
82+
* @var mixed[][]
83+
*/
84+
private $arrayOfArrays;
85+
86+
/**
87+
* @ORM\Column(type="array")
88+
* @var mixed[]
89+
*/
90+
private $mixeds;
91+
92+
/**
93+
* @ORM\Column(type="array")
94+
* @var array|null
95+
*/
96+
private $arrayOrNull;
97+
98+
/**
99+
* @ORM\Column(type="array")
100+
* @var int[]|null
101+
*/
102+
private $arrayOfIntegersOrNull;
103+
74104
}

0 commit comments

Comments
 (0)