Skip to content
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

allow EntityRepository usage by interfaces #141

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ public function hasMethod(\PHPStan\Reflection\ClassReflection $classReflection,
}

$fieldName = $this->classify($methodFieldName);
// ensure all entities implementing the interface have the field
if (interface_exists($entityClassType->getClassName())) {
foreach ($objectManager->getMetadataFactory()->getAllMetadata() as $metadata) {
$implements = class_implements($metadata->getName());
if (!isset($implements[$entityClassType->getClassName()])) {
continue;
}
if (!$metadata->hasField($fieldName) && $metadata->hasAssociation($fieldName)) {
return false;
}
}

return true;
}

$classMetadata = $objectManager->getClassMetadata($entityClassType->getClassName());

return $classMetadata->hasField($fieldName) || $classMetadata->hasAssociation($fieldName);
Expand Down
25 changes: 20 additions & 5 deletions tests/DoctrineIntegration/ORM/data/customRepositoryUsage-2.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
[
{
"message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntity::nonexistent().",
"line": 35,
"line": 43,
"ignorable": true
},
{
"message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyRepository::nonexistant().",
"line": 40,
"line": 48,
"ignorable": true
},
{
"message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntity::nonexistent().",
"line": 54,
"line": 62,
"ignorable": true
},
{
"message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntityInterface::nonexistent().",
"line": 69,
"ignorable": true
},
{
"message": "Cannot call method test() on int.",
"line": 59,
"line": 74,
"ignorable": true
},
{
"message": "Cannot call method test() on int.",
"line": 60,
"line": 75,
"ignorable": true
},
{
"message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntityInterface::test().",
"line": 76,
"ignorable": true
},
{
"message": "Method Doctrine\\Persistence\\ObjectRepository<PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntityInterface>::findOneByBlabla() invoked with 0 parameters, 1 required.",
"line": 76,
"ignorable": true
}
]
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[
{
"message": "Property PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\Example::$repository with generic class PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyRepository does not specify its types: T",
"line": 15,
"line": 16,
"ignorable": true
},
{
"message": "Method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\Example::__construct() has parameter $anotherRepository with generic class PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyRepository but does not specify its types: T",
"line": 22,
"line": 28,
"ignorable": true
}
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
[
{
"message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntity::nonexistent().",
"line": 54,
"line": 62,
"ignorable": true
},
{
"message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntityInterface::nonexistent().",
"line": 69,
"ignorable": true
},
{
"message": "Call to an undefined method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntityInterface::test().",
"line": 76,
"ignorable": true
}
]
19 changes: 17 additions & 2 deletions tests/DoctrineIntegration/ORM/data/customRepositoryUsage-8.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
[
{
"message": "Cannot call method doSomethingElse() on PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntity|null.",
"line": 53,
"line": 61,
"ignorable": true
},
{
"message": "Cannot call method nonexistent() on PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntity|null.",
"line": 54,
"line": 62,
"ignorable": true
},
{
"message": "Cannot call method doSomethingElse() on PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntityInterface|null.",
"line": 68,
"ignorable": true
},
{
"message": "Cannot call method nonexistent() on PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntityInterface|null.",
"line": 69,
"ignorable": true
},
{
"message": "Cannot call method test() on PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyEntityInterface|null.",
"line": 76,
"ignorable": true
}
]
35 changes: 33 additions & 2 deletions tests/DoctrineIntegration/ORM/data/customRepositoryUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\DoctrineIntegration\ORM\CustomRepositoryUsage;

use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping as ORM;
Expand All @@ -19,13 +20,20 @@ class Example
*/
private $anotherRepository;

/**
* @var MyRepositoryInterface
*/
private $interfaceRepository;

public function __construct(
EntityManagerInterface $entityManager,
MyRepository $anotherRepository
MyRepository $anotherRepository,
MyRepositoryInterface $interfaceRepository
)
{
$this->repository = $entityManager->getRepository(MyEntity::class);
$this->anotherRepository = $anotherRepository;
$this->interfaceRepository = $interfaceRepository;
}

public function get(): void
Expand Down Expand Up @@ -54,17 +62,31 @@ public function genericRepository(): void
$entity->nonexistent();
}

public function interfaceRepository(): void
{
$entity = $this->interfaceRepository->find(1);
$entity->doSomethingElse();
$entity->nonexistent();
}

public function callExistingMethodOnRepository(): void
{
$this->repository->findOneByBlabla()->test();
$this->anotherRepository->findOneByBlabla()->test();
$this->interfaceRepository->findOneByBlabla()->test();
}
}

interface MyEntityInterface
{
public function doSomethingElse(): void;
}


/**
* @ORM\Entity(repositoryClass=MyRepository::class)
*/
class MyEntity
class MyEntity implements MyEntityInterface
{
/**
* @ORM\Id()
Expand Down Expand Up @@ -102,3 +124,12 @@ public function findOneByBlabla(): int
return 1;
}
}

/**
* @extends ObjectRepository<MyEntityInterface>
*/
interface MyRepositoryInterface extends ObjectRepository
{

}

2 changes: 1 addition & 1 deletion tests/Rules/Doctrine/ORM/data/MyEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @ORM\Entity()
*/
class MyEntity
class MyEntity implements MyEntityInterface
{
/**
* @ORM\Id()
Expand Down
7 changes: 7 additions & 0 deletions tests/Rules/Doctrine/ORM/data/MyEntityInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace PHPStan\Rules\Doctrine\ORM;

interface MyEntityInterface
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace PHPStan\Rules\Doctrine\ORM;

class RepositoryInterfaceFindByCalls
{

}