Skip to content

Commit 9d798be

Browse files
ruudkondrejmirtes
authored andcommitted
Report correct type on type mapping mismatch
See #187 (comment)
1 parent d9429f6 commit 9d798be

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

src/Rules/Doctrine/ORM/EntityColumnRule.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ public function processNode(Node $node, Scope $scope): array
149149
'Property %s::$%s type mapping mismatch: database can contain %s but property expects %s.',
150150
$className,
151151
$propertyName,
152-
$writableToPropertyType->describe(VerbosityLevel::typeOnly()),
153-
$property->getWritableType()->describe(VerbosityLevel::typeOnly())
152+
$writableToPropertyType->describe(VerbosityLevel::getRecommendedLevelByType($propertyWritableType, $writableToPropertyType)),
153+
$property->getWritableType()->describe(VerbosityLevel::getRecommendedLevelByType($propertyWritableType, $writableToPropertyType))
154154
);
155155
}
156156
$propertyReadableType = TypeTraverser::map($property->getReadableType(), $transformArrays);
@@ -159,8 +159,8 @@ public function processNode(Node $node, Scope $scope): array
159159
'Property %s::$%s type mapping mismatch: property can contain %s but database expects %s.',
160160
$className,
161161
$propertyName,
162-
$propertyReadableType->describe(VerbosityLevel::typeOnly()),
163-
$writableToDatabaseType->describe(VerbosityLevel::typeOnly())
162+
$propertyReadableType->describe(VerbosityLevel::getRecommendedLevelByType($writableToDatabaseType, $propertyReadableType)),
163+
$writableToDatabaseType->describe(VerbosityLevel::getRecommendedLevelByType($writableToDatabaseType, $propertyReadableType))
164164
);
165165
}
166166
return $errors;

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ protected function getRule(): Rule
3232
if (!Type::hasType(CustomType::NAME)) {
3333
Type::addType(CustomType::NAME, CustomType::class);
3434
}
35+
if (!Type::hasType(CustomNumericType::NAME)) {
36+
Type::addType(CustomNumericType::NAME, CustomNumericType::class);
37+
}
3538
if (!Type::hasType(UuidType::NAME)) {
3639
Type::addType(UuidType::NAME, UuidType::class);
3740
}
@@ -46,6 +49,7 @@ protected function getRule(): Rule
4649
new BinaryType(),
4750
new IntegerType(),
4851
new ReflectionDescriptor(CustomType::class, $this->createBroker()),
52+
new ReflectionDescriptor(CustomNumericType::class, $this->createBroker()),
4953
new DateType(),
5054
new UuidTypeDescriptor(UuidType::class),
5155
new ArrayType(),
@@ -98,6 +102,10 @@ public function testRule(): void
98102
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$arrayOfIntegersOrNull type mapping mismatch: property can contain array|null but database expects array.',
99103
102,
100104
],
105+
[
106+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$numericString type mapping mismatch: database can contain string but property expects string&numeric.',
107+
126,
108+
],
101109
]);
102110
}
103111

@@ -152,6 +160,10 @@ public function testCustomType(): void
152160
'Property PHPStan\Rules\Doctrine\ORM\EntityWithCustomType::$foo type mapping mismatch: property can contain int but database expects array.',
153161
24,
154162
],
163+
[
164+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithCustomType::$numeric type mapping mismatch: property can contain string but database expects string&numeric.',
165+
30,
166+
],
155167
]);
156168
}
157169

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Doctrine\ORM;
4+
5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
6+
use Doctrine\DBAL\Types\Type;
7+
8+
class CustomNumericType extends Type
9+
{
10+
11+
public const NAME = 'custom_numeric';
12+
13+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
14+
{
15+
return '';
16+
}
17+
18+
public function getName(): string
19+
{
20+
return self::NAME;
21+
}
22+
23+
/**
24+
* @return numeric-string|null
25+
*/
26+
public function convertToPHPValue($value, AbstractPlatform $abstractPlatform): ?string
27+
{
28+
return '';
29+
}
30+
31+
/**
32+
* @param numeric-string $value
33+
* @return numeric-string|null
34+
*/
35+
public function convertToDatabaseValue($value, AbstractPlatform $abstractPlatform): ?string
36+
{
37+
return '';
38+
}
39+
40+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,16 @@ class EntityWithCustomType
2222
* @var int
2323
*/
2424
private $foo;
25+
26+
/**
27+
* @ORM\Column(type="custom_numeric")
28+
* @var string
29+
*/
30+
private $numeric;
31+
32+
/**
33+
* @ORM\Column(type="custom_numeric")
34+
* @var numeric-string
35+
*/
36+
private $correctNumeric;
2537
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,10 @@ class MyBrokenEntity extends MyBrokenSuperclass
119119
*/
120120
private $decimalWithString2;
121121

122+
/**
123+
* @ORM\Column(type="string")
124+
* @var numeric-string
125+
*/
126+
private $numericString;
127+
122128
}

0 commit comments

Comments
 (0)