Skip to content

Commit

Permalink
OtherMethodQueryBuilderParser: recursion prevention
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored and ondrejmirtes committed Jan 18, 2024
1 parent 03b7d3f commit 9534fcd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Type/Doctrine/QueryBuilder/OtherMethodQueryBuilderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
use function array_key_exists;
use function is_array;
use function sprintf;

Expand All @@ -36,7 +37,11 @@ class OtherMethodQueryBuilderParser
/** @var Container */
private $container;

/** @var array<string, list<QueryBuilderType>> */
/**
* Null if the method is currently being processed
*
* @var array<string, list<QueryBuilderType>|null>
*/
private $cache = [];

public function __construct(bool $descendIntoOtherMethods, Parser $parser, Container $container)
Expand Down Expand Up @@ -64,10 +69,16 @@ public function findQueryBuilderTypesInCalledMethod(Scope $scope, MethodReflecti

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

if (isset($this->cache[$cacheKey])) {
if (array_key_exists($cacheKey, $this->cache)) {
if ($this->cache[$cacheKey] === null) {
return []; // recursion
}

return $this->cache[$cacheKey];
}

$this->cache[$cacheKey] = null;

$nodes = $this->parser->parseFile($fileName);
$classNode = $this->findClassNode($className, $nodes);
if ($classNode === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ private function adjustQueryBuilderToIndexByInt(QueryBuilder $qb): void
$qb->indexBy('m', 'm.intColumn');
}

private function getQueryBuilderRecursively(EntityManagerInterface $em): QueryBuilder
{
return $this->getQueryBuilderRecursively($em);
}

private function getQueryBuilderRecursivelyInLoop(EntityManagerInterface $em): QueryBuilder
{
return $this->getQueryBuilderRecursivelyInLoop2($em);
}

private function getQueryBuilderRecursivelyInLoop2(EntityManagerInterface $em): QueryBuilder
{
return $this->getQueryBuilderRecursivelyInLoop($em);
}

private function getQueryBuilder(EntityManagerInterface $em): QueryBuilder
{
return $em->createQueryBuilder()
Expand Down

0 comments on commit 9534fcd

Please sign in to comment.