Skip to content

Do not error if a non-field findByX method exists #56

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 3 commits into from
Mar 11, 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
12 changes: 11 additions & 1 deletion src/Rules/Doctrine/ORM/MagicRepositoryMethodCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Rules\Rule;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use PHPStan\Type\Doctrine\ObjectRepositoryType;
Expand All @@ -15,9 +16,13 @@ class MagicRepositoryMethodCallRule implements Rule
/** @var ObjectMetadataResolver */
private $objectMetadataResolver;

public function __construct(ObjectMetadataResolver $objectMetadataResolver)
/** @var Broker */
private $broker;

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

public function getNodeType(): string
Expand Down Expand Up @@ -78,6 +83,11 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$repositoryReflectionClass = $this->broker->getClass($calledOnType->getClassName());
if ($repositoryReflectionClass->hasNativeMethod($methodName)) {
return [];
}

return [sprintf(
'Call to method %s::%s() - entity %s does not have a field named $%s.',
$calledOnType->describe(VerbosityLevel::typeOnly()),
Expand Down
24 changes: 19 additions & 5 deletions tests/Rules/Doctrine/ORM/MagicRepositoryMethodCallRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class MagicRepositoryMethodCallRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new MagicRepositoryMethodCallRule(new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', null));
$broker = $this->createBroker();

return new MagicRepositoryMethodCallRule(new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', null), $broker);
}

public function testRule(): void
Expand All @@ -26,21 +28,33 @@ public function testRule(): void
'Call to method Doctrine\ORM\EntityRepository<PHPStan\Rules\Doctrine\ORM\MyEntity>::findByNonexistent() - entity PHPStan\Rules\Doctrine\ORM\MyEntity does not have a field named $nonexistent.',
25,
],
[
'Call to method Doctrine\ORM\EntityRepository<PHPStan\Rules\Doctrine\ORM\MyEntity>::findByCustomMethod() - entity PHPStan\Rules\Doctrine\ORM\MyEntity does not have a field named $customMethod.',
26,
],
[
'Call to method Doctrine\ORM\EntityRepository<PHPStan\Rules\Doctrine\ORM\MyEntity>::findOneByTransient() - entity PHPStan\Rules\Doctrine\ORM\MyEntity does not have a field named $transient.',
34,
35,
],
[
'Call to method Doctrine\ORM\EntityRepository<PHPStan\Rules\Doctrine\ORM\MyEntity>::findOneByNonexistent() - entity PHPStan\Rules\Doctrine\ORM\MyEntity does not have a field named $nonexistent.',
35,
36,
],
[
'Call to method Doctrine\ORM\EntityRepository<PHPStan\Rules\Doctrine\ORM\MyEntity>::countByTransient() - entity PHPStan\Rules\Doctrine\ORM\MyEntity does not have a field named $transient.',
44,
45,
],
[
'Call to method Doctrine\ORM\EntityRepository<PHPStan\Rules\Doctrine\ORM\MyEntity>::countByNonexistent() - entity PHPStan\Rules\Doctrine\ORM\MyEntity does not have a field named $nonexistent.',
45,
46,
],
[
'Call to method PHPStan\Rules\Doctrine\ORM\TestRepository<PHPStan\Rules\Doctrine\ORM\MySecondEntity>::findByTransient() - entity PHPStan\Rules\Doctrine\ORM\MySecondEntity does not have a field named $transient.',
55,
],
[
'Call to method PHPStan\Rules\Doctrine\ORM\TestRepository<PHPStan\Rules\Doctrine\ORM\MySecondEntity>::findByNonexistent() - entity PHPStan\Rules\Doctrine\ORM\MySecondEntity does not have a field named $nonexistent.',
56,
],
]);
}
Expand Down
32 changes: 32 additions & 0 deletions tests/Rules/Doctrine/ORM/data/MySecondEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="PHPStan\Rules\Doctrine\ORM\TestRepository")
*/
class MySecondEntity
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*
* @var int
*/
private $id;

/**
* @var string
* @ORM\Column(type="string")
*/
private $title;

/**
* @var string
*/
private $transient;

}
5 changes: 5 additions & 0 deletions tests/Rules/Doctrine/ORM/data/dql.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public function nowdoc(): void
)->getResult();
}

public function findByCustomMethod(): array
{
return [];
}

}
11 changes: 11 additions & 0 deletions tests/Rules/Doctrine/ORM/data/magic-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function doFindBy(): void
$entityRepository->findByTitle('test');
$entityRepository->findByTransient('test');
$entityRepository->findByNonexistent('test');
$entityRepository->findByCustomMethod();
}

public function doFindOneBy(): void
Expand All @@ -45,4 +46,14 @@ public function doCountBy(): void
$entityRepository->countByNonexistent('test');
}

public function doFindByWithRepository(): void
{
$entityRepository = $this->entityManager->getRepository(MySecondEntity::class);
$entityRepository->findBy(['id' => 1]);
$entityRepository->findById(1);
$entityRepository->findByTitle('test');
$entityRepository->findByTransient('test');
$entityRepository->findByNonexistent('test');
$entityRepository->findByCustomMethod();
}
}