Skip to content

Guard against crashes when only an entity's interface is known #155

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

Open
wants to merge 6 commits into
base: 1.3.x
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/Rules/Doctrine/ORM/RepositoryMethodCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public function processNode(Node $node, Scope $scope): array
}
$entityClass = $entityClassType->getClassName();

if (interface_exists($entityClass)) {
return [];
}

$methodNameIdentifier = $node->name;
if (!$methodNameIdentifier instanceof Node\Identifier) {
return [];
Expand Down
38 changes: 38 additions & 0 deletions tests/Rules/Doctrine/ORM/EntityImplementingInterfaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

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

/**
* @extends RuleTestCase<RepositoryMethodCallRule>
*/
class EntityImplementingInterfaceTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new RepositoryMethodCallRule(new ObjectMetadataResolver($this->createReflectionProvider(), __DIR__ . '/entity-manager.php', null));
}

/**
* @return string[]
*/
public static function getAdditionalConfigFiles(): array
{
return [__DIR__ . '/magic-repository.neon'];
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/repository-findBy-interface.php'], [
[
'Call to method Doctrine\ORM\EntityRepository<PHPStan\Rules\Doctrine\ORM\MyEntityImplementingInterface>::findBy() - entity PHPStan\Rules\Doctrine\ORM\MyEntityImplementingInterface does not have a field named $nonexistent.',
22,
],
]);
}

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

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class MyEntityImplementingInterface implements MyInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*
* @var int
*/
private $id;

/**
* @ORM\Column()
* @var string
*/
private $name;

public function getName(): string
{
return $this->name;
}
}
8 changes: 8 additions & 0 deletions tests/Rules/Doctrine/ORM/data/MyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

interface MyInterface
{
public function getName(): string;
}
36 changes: 36 additions & 0 deletions tests/Rules/Doctrine/ORM/data/repository-findBy-interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\EntityManager;

class RepositoryForInterfaceFindByCalls
{

/** @var EntityManager */
private $entityManager;

public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}

public function doFindByWithConcreteClass(): void
{
$entityRepository = $this->entityManager->getRepository(MyEntityImplementingInterface::class);
$entityRepository->findBy(['id' => 1]);
$entityRepository->findBy(['nonexistent' => 'test']);
}

public function doFindByWithInterface(MyInterface $entity): void
{
$entityRepository = $this->entityManager->getRepository(get_class($entity));
// This is likely to work, but cannot be inferred from the interface
// alone
$repo->findBy(['name' => $entity->getName()]);
// This is NOT likely to work, but can't be proven without examining
// all implementations of the interface
$entityRepository->findBy(['nonexistent' => 'test']);
}

}