Skip to content

Implemented entity relation checking rule #76

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 4 commits into from
Oct 6, 2019
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 rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rules:
- PHPStan\Rules\Doctrine\ORM\MagicRepositoryMethodCallRule
- PHPStan\Rules\Doctrine\ORM\RepositoryMethodCallRule
- PHPStan\Rules\Doctrine\ORM\EntityColumnRule
- PHPStan\Rules\Doctrine\ORM\EntityRelationRule

services:
-
Expand Down
100 changes: 100 additions & 0 deletions src/Rules/Doctrine/ORM/EntityRelationRule.php
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\ObjectMetadataResolver;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

class EntityRelationRule implements Rule
{

/** @var \PHPStan\Type\Doctrine\ObjectMetadataResolver */
private $objectMetadataResolver;

public function __construct(ObjectMetadataResolver $objectMetadataResolver)
{
$this->objectMetadataResolver = $objectMetadataResolver;
}

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->associationMappings[$propertyName])) {
return [];
}
$associationMapping = $metadata->associationMappings[$propertyName];

$columnType = null;
if ((bool) ($associationMapping['type'] & 3)) { // ClassMetadataInfo::TO_ONE
$columnType = new ObjectType($associationMapping['targetEntity']);
if ($associationMapping['joinColumns'][0]['nullable'] ?? true) {
$columnType = TypeCombinator::addNull($columnType);
}
} elseif ((bool) ($associationMapping['type'] & 12)) { // ClassMetadataInfo::TO_MANY
$columnType = TypeCombinator::intersect(
new ObjectType('Doctrine\Common\Collections\Collection'),
new IterableType(new MixedType(), new ObjectType($associationMapping['targetEntity']))
);
}

$errors = [];
if ($columnType !== null) {
if (!$property->getWritableType()->isSuperTypeOf($columnType)->yes()) {
$errors[] = sprintf('Database can contain %s but property expects %s.', $columnType->describe(VerbosityLevel::typeOnly()), $property->getWritableType()->describe(VerbosityLevel::typeOnly()));
}
if (!$columnType->isSuperTypeOf($property->getReadableType())->yes()) {
$errors[] = sprintf('Property can contain %s but database expects %s.', $property->getReadableType()->describe(VerbosityLevel::typeOnly()), $columnType->describe(VerbosityLevel::typeOnly()));
}
}

return $errors;
}

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

namespace PHPStan\Rules\Doctrine\ORM;

use Iterator;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;

class EntityRelationRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new EntityRelationRule(
new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', null)
);
}

/**
* @dataProvider ruleProvider
*/
public function testRule(string $file, array $expectedErrors): void
{
$this->analyse([$file], $expectedErrors);
}

public function ruleProvider(): Iterator
{
yield 'nice entity' => [__DIR__ . '/data/EntityWithRelations.php', []];

yield 'one to one' => [__DIR__ . '/data/EntityWithBrokenOneToOneRelations.php',
[
[
'Property can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
31,
],
[
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
37,
],
[
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\MyEntity|null.',
50,
],
[
'Property can contain PHPStan\Rules\Doctrine\ORM\MyEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity|null.',
50,
],
]];

yield 'many to one' => [__DIR__ . '/data/EntityWithBrokenManyToOneRelations.php',
[
[
'Property can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
31,
],
[
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\AnotherEntity.',
37,
],
[
'Database can contain PHPStan\Rules\Doctrine\ORM\AnotherEntity|null but property expects PHPStan\Rules\Doctrine\ORM\MyEntity|null.',
50,
],
[
'Property can contain PHPStan\Rules\Doctrine\ORM\MyEntity|null but database expects PHPStan\Rules\Doctrine\ORM\AnotherEntity|null.',
50,
],
]];

yield 'one to many' => [__DIR__ . '/data/EntityWithBrokenOneToManyRelations.php',
[
[
'Property can contain iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
24,
],
[
'Property can contain Doctrine\Common\Collections\Collection but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
30,
],
[
'Database can contain Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but property expects array<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
36,
],
[
'Property can contain array<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
36,
],
]];

yield 'many to many' => [__DIR__ . '/data/EntityWithBrokenManyToManyRelations.php',
[
[
'Property can contain iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
24,
],
[
'Property can contain Doctrine\Common\Collections\Collection but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
30,
],
[
'Database can contain Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but property expects array<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
36,
],
[
'Property can contain array<PHPStan\Rules\Doctrine\ORM\AnotherEntity> but database expects Doctrine\Common\Collections\Collection&iterable<PHPStan\Rules\Doctrine\ORM\AnotherEntity>.',
36,
],
]];
}

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

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class AnotherEntity
{

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

/**
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\EntityWithRelations")
*/
private $manyToOne;

/**
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations")
*/
private $one;

/**
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations")
*/
private $two;

/**
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations")
*/
private $three;

/**
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations")
*/
private $four;

/**
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\EntityWithBrokenOneToManyRelations")
*/
private $five;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class EntityWithBrokenManyToManyRelations
{

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

/**
* @ORM\ManyToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
* @var iterable<\PHPStan\Rules\Doctrine\ORM\AnotherEntity>
*/
private $manyToManyWithIterableAnnotation;

/**
* @ORM\ManyToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
* @var \Doctrine\Common\Collections\Collection
*/
private $manyToManyWithCollectionAnnotation;

/**
* @ORM\ManyToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity[]
*/
private $manyToManyWithArrayAnnotation;

/**
* @ORM\ManyToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
* @var \Doctrine\Common\Collections\Collection&iterable<\PHPStan\Rules\Doctrine\ORM\AnotherEntity>
*/
private $manyToManyWithCorrectAnnotation;

/**
* @ORM\ManyToMany(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
* @var \Doctrine\Common\Collections\Collection|\PHPStan\Rules\Doctrine\ORM\AnotherEntity[]
*/
private $manyToManyWithCorrectOldStyleAnnotation;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class EntityWithBrokenManyToOneRelations
{

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

/**
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity|null
*/
private $manyToOneNullableBoth;

/**
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
* @ORM\JoinColumn(nullable=false)
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity|null
*/
private $manyToOneNullableProperty;

/**
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity
*/
private $manyToOneNullableColumn;

/**
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
* @ORM\JoinColumn(nullable=false)
* @var \PHPStan\Rules\Doctrine\ORM\AnotherEntity
*/
private $manyToOneNonNullable;

/**
* @ORM\ManyToOne(targetEntity="PHPStan\Rules\Doctrine\ORM\AnotherEntity")
* @var \PHPStan\Rules\Doctrine\ORM\MyEntity|null
*/
private $manyToOneWrongClass;

}
Loading