Skip to content

Commit

Permalink
Report correct type on type mapping mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
ruudk authored and ondrejmirtes committed May 17, 2021
1 parent d9429f6 commit 9d798be
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Rules/Doctrine/ORM/EntityColumnRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ public function processNode(Node $node, Scope $scope): array
'Property %s::$%s type mapping mismatch: database can contain %s but property expects %s.',
$className,
$propertyName,
$writableToPropertyType->describe(VerbosityLevel::typeOnly()),
$property->getWritableType()->describe(VerbosityLevel::typeOnly())
$writableToPropertyType->describe(VerbosityLevel::getRecommendedLevelByType($propertyWritableType, $writableToPropertyType)),
$property->getWritableType()->describe(VerbosityLevel::getRecommendedLevelByType($propertyWritableType, $writableToPropertyType))
);
}
$propertyReadableType = TypeTraverser::map($property->getReadableType(), $transformArrays);
Expand All @@ -159,8 +159,8 @@ public function processNode(Node $node, Scope $scope): array
'Property %s::$%s type mapping mismatch: property can contain %s but database expects %s.',
$className,
$propertyName,
$propertyReadableType->describe(VerbosityLevel::typeOnly()),
$writableToDatabaseType->describe(VerbosityLevel::typeOnly())
$propertyReadableType->describe(VerbosityLevel::getRecommendedLevelByType($writableToDatabaseType, $propertyReadableType)),
$writableToDatabaseType->describe(VerbosityLevel::getRecommendedLevelByType($writableToDatabaseType, $propertyReadableType))
);
}
return $errors;
Expand Down
12 changes: 12 additions & 0 deletions tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ protected function getRule(): Rule
if (!Type::hasType(CustomType::NAME)) {
Type::addType(CustomType::NAME, CustomType::class);
}
if (!Type::hasType(CustomNumericType::NAME)) {
Type::addType(CustomNumericType::NAME, CustomNumericType::class);
}
if (!Type::hasType(UuidType::NAME)) {
Type::addType(UuidType::NAME, UuidType::class);
}
Expand All @@ -46,6 +49,7 @@ protected function getRule(): Rule
new BinaryType(),
new IntegerType(),
new ReflectionDescriptor(CustomType::class, $this->createBroker()),
new ReflectionDescriptor(CustomNumericType::class, $this->createBroker()),
new DateType(),
new UuidTypeDescriptor(UuidType::class),
new ArrayType(),
Expand Down Expand Up @@ -98,6 +102,10 @@ public function testRule(): void
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$arrayOfIntegersOrNull type mapping mismatch: property can contain array|null but database expects array.',
102,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$numericString type mapping mismatch: database can contain string but property expects string&numeric.',
126,
],
]);
}

Expand Down Expand Up @@ -152,6 +160,10 @@ public function testCustomType(): void
'Property PHPStan\Rules\Doctrine\ORM\EntityWithCustomType::$foo type mapping mismatch: property can contain int but database expects array.',
24,
],
[
'Property PHPStan\Rules\Doctrine\ORM\EntityWithCustomType::$numeric type mapping mismatch: property can contain string but database expects string&numeric.',
30,
],
]);
}

Expand Down
40 changes: 40 additions & 0 deletions tests/Rules/Doctrine/ORM/data/CustomNumericType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

class CustomNumericType extends Type
{

public const NAME = 'custom_numeric';

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
{
return '';
}

public function getName(): string
{
return self::NAME;
}

/**
* @return numeric-string|null
*/
public function convertToPHPValue($value, AbstractPlatform $abstractPlatform): ?string
{
return '';
}

/**
* @param numeric-string $value
* @return numeric-string|null
*/
public function convertToDatabaseValue($value, AbstractPlatform $abstractPlatform): ?string
{
return '';
}

}
12 changes: 12 additions & 0 deletions tests/Rules/Doctrine/ORM/data/EntityWithCustomType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ class EntityWithCustomType
* @var int
*/
private $foo;

/**
* @ORM\Column(type="custom_numeric")
* @var string
*/
private $numeric;

/**
* @ORM\Column(type="custom_numeric")
* @var numeric-string
*/
private $correctNumeric;
}
6 changes: 6 additions & 0 deletions tests/Rules/Doctrine/ORM/data/MyBrokenEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,10 @@ class MyBrokenEntity extends MyBrokenSuperclass
*/
private $decimalWithString2;

/**
* @ORM\Column(type="string")
* @var numeric-string
*/
private $numericString;

}

0 comments on commit 9d798be

Please # to comment.