Skip to content

Commit 0cd4856

Browse files
lookymanondrejmirtes
authored andcommitted
Improve entity rules error messages
1 parent 15bf6ca commit 0cd4856

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

src/Rules/Doctrine/ORM/EntityColumnRule.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,23 @@ public function processNode(Node $node, Scope $scope): array
103103
}
104104

105105
if (!$property->getWritableType()->isSuperTypeOf($writableToPropertyType)->yes()) {
106-
$errors[] = sprintf('Database can contain %s but property expects %s.', $writableToPropertyType->describe(VerbosityLevel::typeOnly()), $property->getWritableType()->describe(VerbosityLevel::typeOnly()));
106+
$errors[] = sprintf(
107+
'Property %s::$%s type mapping mismatch: database can contain %s but property expects %s.',
108+
$className,
109+
$propertyName,
110+
$writableToPropertyType->describe(VerbosityLevel::typeOnly()),
111+
$property->getWritableType()->describe(VerbosityLevel::typeOnly())
112+
);
107113
}
108114
$propertyReadableType = $property->getReadableType();
109115
if (!$writableToDatabaseType->isSuperTypeOf($identifier === $propertyName && !$nullable ? TypeCombinator::removeNull($propertyReadableType) : $propertyReadableType)->yes()) {
110-
$errors[] = sprintf('Property can contain %s but database expects %s.', $propertyReadableType->describe(VerbosityLevel::typeOnly()), $writableToDatabaseType->describe(VerbosityLevel::typeOnly()));
116+
$errors[] = sprintf(
117+
'Property %s::$%s type mapping mismatch: property can contain %s but database expects %s.',
118+
$className,
119+
$propertyName,
120+
$propertyReadableType->describe(VerbosityLevel::typeOnly()),
121+
$writableToDatabaseType->describe(VerbosityLevel::typeOnly())
122+
);
111123
}
112124
return $errors;
113125
}

src/Rules/Doctrine/ORM/EntityRelationRule.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,22 @@ public function processNode(Node $node, Scope $scope): array
8787
$errors = [];
8888
if ($columnType !== null) {
8989
if (!$property->getWritableType()->isSuperTypeOf($columnType)->yes()) {
90-
$errors[] = sprintf('Database can contain %s but property expects %s.', $columnType->describe(VerbosityLevel::typeOnly()), $property->getWritableType()->describe(VerbosityLevel::typeOnly()));
90+
$errors[] = sprintf(
91+
'Property %s::$%s type mapping mismatch: database can contain %s but property expects %s.',
92+
$className,
93+
$propertyName,
94+
$columnType->describe(VerbosityLevel::typeOnly()),
95+
$property->getWritableType()->describe(VerbosityLevel::typeOnly())
96+
);
9197
}
9298
if (!$columnType->isSuperTypeOf($property->getReadableType())->yes()) {
93-
$errors[] = sprintf('Property can contain %s but database expects %s.', $property->getReadableType()->describe(VerbosityLevel::typeOnly()), $columnType->describe(VerbosityLevel::typeOnly()));
99+
$errors[] = sprintf(
100+
'Property %s::$%s type mapping mismatch: property can contain %s but database expects %s.',
101+
$className,
102+
$propertyName,
103+
$property->getReadableType()->describe(VerbosityLevel::typeOnly()),
104+
$columnType->describe(VerbosityLevel::typeOnly())
105+
);
94106
}
95107
}
96108

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,27 @@ public function testRule(): void
3434
{
3535
$this->analyse([__DIR__ . '/data/MyBrokenEntity.php'], [
3636
[
37-
'Database can contain string but property expects int|null.',
37+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.',
3838
19,
3939
],
4040
[
41-
'Database can contain string|null but property expects string.',
41+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$one type mapping mismatch: database can contain string|null but property expects string.',
4242
25,
4343
],
4444
[
45-
'Property can contain string|null but database expects string.',
45+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$two type mapping mismatch: property can contain string|null but database expects string.',
4646
31,
4747
],
4848
[
49-
'Database can contain DateTime but property expects DateTimeImmutable.',
49+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$three type mapping mismatch: database can contain DateTime but property expects DateTimeImmutable.',
5050
37,
5151
],
5252
[
53-
'Database can contain DateTimeImmutable but property expects DateTime.',
53+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$four type mapping mismatch: database can contain DateTimeImmutable but property expects DateTime.',
5454
43,
5555
],
5656
[
57-
'Property can contain DateTime but database expects DateTimeImmutable.',
57+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$four type mapping mismatch: property can contain DateTime but database expects DateTimeImmutable.',
5858
43,
5959
],
6060
]);
@@ -64,7 +64,7 @@ public function testSuperclass(): void
6464
{
6565
$this->analyse([__DIR__ . '/data/MyBrokenSuperclass.php'], [
6666
[
67-
'Database can contain resource but property expects int.',
67+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenSuperclass::$five type mapping mismatch: database can contain resource but property expects int.',
6868
17,
6969
],
7070
]);
@@ -85,7 +85,7 @@ public function generatedIdsProvider(): Iterator
8585
__DIR__ . '/data/GeneratedIdEntity2.php',
8686
[
8787
[
88-
'Database can contain string|null but property expects string.',
88+
'Property PHPStan\Rules\Doctrine\ORM\GeneratedIdEntity2::$id type mapping mismatch: database can contain string|null but property expects string.',
8989
19,
9090
],
9191
],

tests/Rules/Doctrine/ORM/EntityRelationRuleTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,79 +32,79 @@ public function ruleProvider(): Iterator
3232
yield 'one to one' => [__DIR__ . '/data/EntityWithBrokenOneToOneRelations.php',
3333
[
3434
[
35-
'Property can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
35+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToOneRelations::$oneToOneNullableProperty type mapping mismatch: property can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
3636
31,
3737
],
3838
[
39-
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
39+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToOneRelations::$oneToOneNullableColumn type mapping mismatch: database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
4040
37,
4141
],
4242
[
43-
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\MyEntity|null.',
43+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToOneRelations::$oneToOneWrongClass type mapping mismatch: database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\MyEntity|null.',
4444
50,
4545
],
4646
[
47-
'Property can contain PHPStan\Rules\Doctrine\ORM\MyEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity|null.',
47+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToOneRelations::$oneToOneWrongClass type mapping mismatch: property can contain PHPStan\Rules\Doctrine\ORM\MyEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity|null.',
4848
50,
4949
],
5050
]];
5151

5252
yield 'many to one' => [__DIR__ . '/data/EntityWithBrokenManyToOneRelations.php',
5353
[
5454
[
55-
'Property can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
55+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenManyToOneRelations::$manyToOneNullableProperty type mapping mismatch: property can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
5656
31,
5757
],
5858
[
59-
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
59+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenManyToOneRelations::$manyToOneNullableColumn type mapping mismatch: database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
6060
37,
6161
],
6262
[
63-
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\MyEntity|null.',
63+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenManyToOneRelations::$manyToOneWrongClass type mapping mismatch: database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\MyEntity|null.',
6464
50,
6565
],
6666
[
67-
'Property can contain PHPStan\Rules\Doctrine\ORM\MyEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity|null.',
67+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenManyToOneRelations::$manyToOneWrongClass type mapping mismatch: property can contain PHPStan\Rules\Doctrine\ORM\MyEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity|null.',
6868
50,
6969
],
7070
]];
7171

7272
yield 'one to many' => [__DIR__ . '/data/EntityWithBrokenOneToManyRelations.php',
7373
[
7474
[
75-
'Property can contain iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
75+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations::$oneToManyWithIterableAnnotation type mapping mismatch: property can contain iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
7676
24,
7777
],
7878
[
79-
'Property can contain Doctrine\Common\Collections\Collection but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
79+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations::$oneToManyWithCollectionAnnotation type mapping mismatch: property can contain Doctrine\Common\Collections\Collection but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
8080
30,
8181
],
8282
[
83-
'Database can contain Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but property expects array<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
83+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations::$oneToManyWithArrayAnnotation type mapping mismatch: database can contain Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but property expects array<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
8484
36,
8585
],
8686
[
87-
'Property can contain array<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
87+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations::$oneToManyWithArrayAnnotation type mapping mismatch: property can contain array<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
8888
36,
8989
],
9090
]];
9191

9292
yield 'many to many' => [__DIR__ . '/data/EntityWithBrokenManyToManyRelations.php',
9393
[
9494
[
95-
'Property can contain iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
95+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenManyToManyRelations::$manyToManyWithIterableAnnotation type mapping mismatch: property can contain iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
9696
24,
9797
],
9898
[
99-
'Property can contain Doctrine\Common\Collections\Collection but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
99+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenManyToManyRelations::$manyToManyWithCollectionAnnotation type mapping mismatch: property can contain Doctrine\Common\Collections\Collection but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
100100
30,
101101
],
102102
[
103-
'Database can contain Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but property expects array<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
103+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenManyToManyRelations::$manyToManyWithArrayAnnotation type mapping mismatch: database can contain Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but property expects array<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
104104
36,
105105
],
106106
[
107-
'Property can contain array<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
107+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithBrokenManyToManyRelations::$manyToManyWithArrayAnnotation type mapping mismatch: property can contain array<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
108108
36,
109109
],
110110
]];

0 commit comments

Comments
 (0)