-
Notifications
You must be signed in to change notification settings - Fork 105
[WIP] Validate entity field types against column types #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Doctrine\ORM; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MissingPropertyFromReflectionException; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Type\Doctrine\DescriptorNotRegisteredException; | ||
use PHPStan\Type\Doctrine\DescriptorRegistry; | ||
use PHPStan\Type\Doctrine\ObjectMetadataResolver; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function sprintf; | ||
|
||
class EntityColumnRule implements Rule | ||
{ | ||
|
||
/** @var \PHPStan\Type\Doctrine\ObjectMetadataResolver */ | ||
private $objectMetadataResolver; | ||
|
||
/** @var \PHPStan\Type\Doctrine\DescriptorRegistry */ | ||
private $descriptorRegistry; | ||
|
||
public function __construct(ObjectMetadataResolver $objectMetadataResolver, DescriptorRegistry $descriptorRegistry) | ||
{ | ||
$this->objectMetadataResolver = $objectMetadataResolver; | ||
$this->descriptorRegistry = $descriptorRegistry; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\PropertyProperty::class; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Stmt\PropertyProperty $node | ||
* @param \PHPStan\Analyser\Scope $scope | ||
* @return string[] | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$class = $scope->getClassReflection(); | ||
if ($class === null) { | ||
return []; | ||
} | ||
|
||
$objectManager = $this->objectMetadataResolver->getObjectManager(); | ||
if ($objectManager === null) { | ||
return []; | ||
} | ||
|
||
$className = $class->getName(); | ||
if ($objectManager->getMetadataFactory()->isTransient($className)) { | ||
return []; | ||
} | ||
|
||
/** @var \Doctrine\ORM\Mapping\ClassMetadataInfo $metadata */ | ||
$metadata = $objectManager->getClassMetadata($className); | ||
$classMetadataInfo = 'Doctrine\ORM\Mapping\ClassMetadataInfo'; | ||
if (!$metadata instanceof $classMetadataInfo) { | ||
return []; | ||
} | ||
|
||
$propertyName = (string) $node->name; | ||
try { | ||
$property = $class->getNativeProperty($propertyName); | ||
} catch (MissingPropertyFromReflectionException $e) { | ||
return []; | ||
} | ||
|
||
if (!isset($metadata->fieldMappings[$propertyName])) { | ||
return []; | ||
} | ||
$fieldMapping = $metadata->fieldMappings[$propertyName]; | ||
|
||
$errors = []; | ||
try { | ||
$descriptor = $this->descriptorRegistry->get($fieldMapping['type']); | ||
} catch (DescriptorNotRegisteredException $e) { | ||
return []; | ||
} | ||
|
||
$writableToPropertyType = $descriptor->getWritableToPropertyType(); | ||
$writableToDatabaseType = $descriptor->getWritableToDatabaseType(); | ||
if ($fieldMapping['nullable'] === true) { | ||
$writableToPropertyType = TypeCombinator::addNull($writableToPropertyType); | ||
$writableToDatabaseType = TypeCombinator::addNull($writableToDatabaseType); | ||
} | ||
|
||
if (!$property->getWritableType()->isSuperTypeOf($writableToPropertyType)->yes()) { | ||
$errors[] = sprintf('Database can contain %s but property expects %s.', $writableToPropertyType->describe(VerbosityLevel::typeOnly()), $property->getWritableType()->describe(VerbosityLevel::typeOnly())); | ||
} | ||
if (!$writableToDatabaseType->isSuperTypeOf($property->getReadableType())->yes()) { | ||
$errors[] = sprintf('Property can contain %s but database expects %s.', $property->getReadableType()->describe(VerbosityLevel::typeOnly()), $writableToDatabaseType->describe(VerbosityLevel::typeOnly())); | ||
} | ||
return $errors; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine; | ||
|
||
class DescriptorNotRegisteredException extends \RuntimeException | ||
{ | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine; | ||
|
||
use PHPStan\Type\Doctrine\Descriptors\DoctrineTypeDescriptor; | ||
|
||
class DescriptorRegistry | ||
{ | ||
|
||
/** @var array<string, \PHPStan\Type\Doctrine\Descriptors\DoctrineTypeDescriptor> */ | ||
private $descriptors = []; | ||
|
||
/** | ||
* @param \PHPStan\Type\Doctrine\Descriptors\DoctrineTypeDescriptor[] $descriptors | ||
*/ | ||
public function __construct(array $descriptors) | ||
{ | ||
foreach ($descriptors as $descriptor) { | ||
$this->descriptors[$descriptor->getType()] = $descriptor; | ||
} | ||
} | ||
|
||
public function get(string $type): DoctrineTypeDescriptor | ||
{ | ||
if (!isset($this->descriptors[$type])) { | ||
throw new \PHPStan\Type\Doctrine\DescriptorNotRegisteredException(); | ||
} | ||
return $this->descriptors[$type]; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine; | ||
|
||
use PHPStan\DependencyInjection\Container; | ||
|
||
class DescriptorRegistryFactory | ||
{ | ||
|
||
public const TYPE_DESCRIPTOR_TAG = 'phpstan.doctrine.typeDescriptor'; | ||
|
||
/** @var \PHPStan\DependencyInjection\Container */ | ||
private $container; | ||
|
||
public function __construct(Container $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
public function createRegistry(): DescriptorRegistry | ||
{ | ||
return new DescriptorRegistry($this->container->getServicesByTag(self::TYPE_DESCRIPTOR_TAG)); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine\Descriptors; | ||
|
||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\Type; | ||
|
||
class ArrayType implements DoctrineTypeDescriptor | ||
{ | ||
|
||
public function getType(): string | ||
{ | ||
return 'array'; | ||
} | ||
|
||
public function getWritableToPropertyType(): Type | ||
{ | ||
return new \PHPStan\Type\ArrayType(new MixedType(), new MixedType()); | ||
} | ||
|
||
public function getWritableToDatabaseType(): Type | ||
{ | ||
return new \PHPStan\Type\ArrayType(new MixedType(), new MixedType()); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine\Descriptors; | ||
|
||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
class BigIntType implements DoctrineTypeDescriptor | ||
{ | ||
|
||
public function getType(): string | ||
{ | ||
return 'bigint'; | ||
} | ||
|
||
public function getWritableToPropertyType(): Type | ||
{ | ||
return new \PHPStan\Type\StringType(); | ||
} | ||
|
||
public function getWritableToDatabaseType(): Type | ||
{ | ||
return TypeCombinator::union(new \PHPStan\Type\StringType(), new \PHPStan\Type\IntegerType()); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine\Descriptors; | ||
|
||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\ResourceType; | ||
use PHPStan\Type\Type; | ||
|
||
class BinaryType implements DoctrineTypeDescriptor | ||
{ | ||
|
||
public function getType(): string | ||
{ | ||
return 'binary'; | ||
} | ||
|
||
public function getWritableToPropertyType(): Type | ||
{ | ||
return new ResourceType(); | ||
} | ||
|
||
public function getWritableToDatabaseType(): Type | ||
{ | ||
return new MixedType(); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine\Descriptors; | ||
|
||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\ResourceType; | ||
use PHPStan\Type\Type; | ||
|
||
class BlobType implements DoctrineTypeDescriptor | ||
{ | ||
|
||
public function getType(): string | ||
{ | ||
return 'blob'; | ||
} | ||
|
||
public function getWritableToPropertyType(): Type | ||
{ | ||
return new ResourceType(); | ||
} | ||
|
||
public function getWritableToDatabaseType(): Type | ||
{ | ||
return new MixedType(); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.