Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"phpstan/phpstan-phpunit": "^0.12",
"phpstan/phpstan-strict-rules": "^0.12",
"phpunit/phpunit": "^7.0",
"ramsey/uuid-doctrine": "^1.5.0",
"slevomat/coding-standard": "^4.5.2",
"doctrine/common": "^2.7",
"doctrine/orm": "^2.5",
Expand Down
17 changes: 17 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,20 @@ services:
-
class: PHPStan\Type\Doctrine\Descriptors\TimeType
tags: [phpstan.doctrine.typeDescriptor]

# 3rd party Type descriptors
-
class: PHPStan\Type\Doctrine\Descriptors\Ramsey\UuidTypeDescriptor
tags: [phpstan.doctrine.typeDescriptor]
arguments:
uuidTypeName: Ramsey\Uuid\Doctrine\UuidType
-
class: PHPStan\Type\Doctrine\Descriptors\Ramsey\UuidTypeDescriptor
tags: [phpstan.doctrine.typeDescriptor]
arguments:
uuidTypeName: Ramsey\Uuid\Doctrine\UuidBinaryType
-
class: PHPStan\Type\Doctrine\Descriptors\Ramsey\UuidTypeDescriptor
tags: [phpstan.doctrine.typeDescriptor]
arguments:
uuidTypeName: Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType
57 changes: 57 additions & 0 deletions src/Type/Doctrine/Descriptors/Ramsey/UuidTypeDescriptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Doctrine\Descriptors\Ramsey;

use PHPStan\Type\Doctrine\Descriptors\DoctrineTypeDescriptor;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use Ramsey\Uuid\UuidInterface;

class UuidTypeDescriptor implements DoctrineTypeDescriptor
{

private const SUPPORTED_UUID_TYPES = [
\Ramsey\Uuid\Doctrine\UuidType::class,
\Ramsey\Uuid\Doctrine\UuidBinaryType::class,
\Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType::class,
];

/**
* @phpstan-var class-string<\Doctrine\DBAL\Types\Type>
* @var string
*/
private $uuidTypeName;

public function __construct(
string $uuidTypeName
)
{
if (!in_array($uuidTypeName, self::SUPPORTED_UUID_TYPES, true)) {
throw new \PHPStan\ShouldNotHappenException(sprintf(
'Unexpected UUID column type "%s" provided',
$uuidTypeName
));
}

$this->uuidTypeName = $uuidTypeName;
}

public function getType(): string
{
return $this->uuidTypeName;
}

public function getWritableToPropertyType(): Type
{
return new \PHPStan\Type\ObjectType(UuidInterface::class);
}

public function getWritableToDatabaseType(): Type
{
return TypeCombinator::union(
new \PHPStan\Type\StringType(),
new \PHPStan\Type\ObjectType(UuidInterface::class)
);
}

}
14 changes: 14 additions & 0 deletions tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
use PHPStan\Type\Doctrine\Descriptors\DateTimeType;
use PHPStan\Type\Doctrine\Descriptors\DateType;
use PHPStan\Type\Doctrine\Descriptors\IntegerType;
use PHPStan\Type\Doctrine\Descriptors\Ramsey\UuidTypeDescriptor;
use PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor;
use PHPStan\Type\Doctrine\Descriptors\StringType;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use Ramsey\Uuid\Doctrine\UuidType;

/**
* @extends RuleTestCase<EntityColumnRule>
Expand All @@ -28,6 +30,9 @@ protected function getRule(): Rule
if (!Type::hasType(CustomType::NAME)) {
Type::addType(CustomType::NAME, CustomType::class);
}
if (!Type::hasType(UuidType::NAME)) {
Type::addType(UuidType::NAME, UuidType::class);
}

return new EntityColumnRule(
new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', null),
Expand All @@ -40,6 +45,7 @@ protected function getRule(): Rule
new IntegerType(),
new ReflectionDescriptor(CustomType::class, $this->createBroker()),
new DateType(),
new UuidTypeDescriptor(UuidType::class),
]),
true
);
Expand Down Expand Up @@ -72,6 +78,14 @@ public function testRule(): void
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$four type mapping mismatch: property can contain DateTime but database expects DateTimeImmutable.',
43,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$uuidInvalidType type mapping mismatch: database can contain Ramsey\Uuid\UuidInterface but property expects int.',
72,
],
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$uuidInvalidType type mapping mismatch: property can contain int but database expects Ramsey\Uuid\UuidInterface|string.',
72,
],
]);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Rules/Doctrine/ORM/data/MyBrokenEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ class MyBrokenEntity extends MyBrokenSuperclass
*/
private $never;

/**
* @ORM\Column(type="uuid")
* @var \Ramsey\Uuid\UuidInterface
*/
private $uuid;

/**
* @ORM\Column(type="uuid")
* @var int
*/
private $uuidInvalidType;

}