Skip to content
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
20 changes: 17 additions & 3 deletions src/Type/Doctrine/QueryBuilder/OtherMethodQueryBuilderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
use PHPStan\Analyser\ScopeFactory;
use PHPStan\DependencyInjection\Container;
use PHPStan\Parser\Parser;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function array_key_exists;
use function array_map;
use function implode;
use function is_array;
use function sprintf;

Expand Down Expand Up @@ -57,7 +61,7 @@ public function findQueryBuilderTypesInCalledMethod(Scope $scope, MethodReflecti
return [];
}

$cacheKey = $this->buildCacheKey($fileName, $className, $methodName);
$cacheKey = $this->buildCacheKey($fileName, $methodReflection->getDeclaringClass(), $methodName);

if (array_key_exists($cacheKey, $this->cache)) {
if ($this->cache[$cacheKey] === null) {
Expand Down Expand Up @@ -174,9 +178,19 @@ private function findMethodNode(string $methodName, array $classStatements): ?Cl
return null;
}

private function buildCacheKey(string $fileName, string $declaringClassName, string $methodName): string
private function buildCacheKey(string $fileName, ClassReflection $classReflection, string $methodName): string
{
return sprintf('%s-%s-%s', $fileName, $declaringClassName, $methodName);
$className = $classReflection->getName();
if ($classReflection->isGeneric()) {
// the template types bound by the caller decide the query builder an inherited method returns
$templateTypes = array_map(
static fn (Type $type): string => $type->describe(VerbosityLevel::cache()),
$classReflection->getActiveTemplateTypeMap()->getTypes(),
);
$className .= sprintf('<%s>', implode(',', $templateTypes));
}

return sprintf('%s-%s-%s', $fileName, $className, $methodName);
}

}
5 changes: 5 additions & 0 deletions tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ public function testDynamicWhere(): void
]);
}

public function testQueryBuilderInheritedFromGenericRepository(): void
{
$this->analyse([__DIR__ . '/data/query-builder-dql-generic-repository.php'], []);
}

public static function getAdditionalConfigFiles(): array
{
return [
Expand Down
19 changes: 19 additions & 0 deletions tests/Rules/Doctrine/ORM/data/GenericRepositoryEntityA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class GenericRepositoryEntityA extends GenericRepositorySuperclass
{

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

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

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class GenericRepositoryEntityB extends GenericRepositorySuperclass
{

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

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

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\MappedSuperclass()
*/
abstract class GenericRepositorySuperclass
{

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

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

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

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;

/**
* @template T of GenericRepositorySuperclass
* @extends EntityRepository<T>
*/
abstract class AbstractGenericRepository extends EntityRepository
{

protected function createSharedQueryBuilder(): QueryBuilder
{
return $this->createQueryBuilder('e')
->andWhere('e.shared = :shared');
}

public function findShared(): void
{
$this->createSharedQueryBuilder()
->andWhere('e.id = :id')
->getQuery();
}

}

/**
* @extends AbstractGenericRepository<GenericRepositoryEntityA>
*/
class GenericRepositoryA extends AbstractGenericRepository
{

public function findOnlyA(): void
{
$this->createSharedQueryBuilder()
->andWhere('e.onlyA = :onlyA')
->getQuery();
}

}

/**
* @extends AbstractGenericRepository<GenericRepositoryEntityB>
*/
class GenericRepositoryB extends AbstractGenericRepository
{

public function findOnlyB(): void
{
$this->createSharedQueryBuilder()
->andWhere('e.onlyB = :onlyB')
->getQuery();
}

}
Loading