Open
Show file tree
Hide file tree
Changes from all commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Failed to load files.
Original file line numberDiff line numberDiff line change
Expand Up@@ -147,7 +147,7 @@ public function getTypeFromMethodCall(

if ($lowerMethodName === 'set') {
try {
$args = $this->argumentsProcessor->processArgs($scope, $methodName, array_slice($calledMethodCall->getArgs(), 0, 1));
$args = $this->argumentsProcessor->processArgs($queryBuilderType->getScope(), $methodName, array_slice($calledMethodCall->getArgs(), 0, 1));
} catch (DynamicQueryBuilderArgumentException $e) {
return $defaultReturnType;
}
Expand All@@ -162,7 +162,7 @@ public function getTypeFromMethodCall(
}

try {
$args = $this->argumentsProcessor->processArgs($scope, $methodName, $calledMethodCall->getArgs());
$args = $this->argumentsProcessor->processArgs($queryBuilderType->getScope(), $methodName, $calledMethodCall->getArgs());
} catch (DynamicQueryBuilderArgumentException $e) {
if (in_array($lowerMethodName, self::METHODS_NOT_AFFECTING_RESULT_TYPE, true)) {
continue;
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@
namespace PHPStan\Type\Doctrine\QueryBuilder;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use function md5;
Expand All@@ -16,12 +17,22 @@ abstract class QueryBuilderType extends ObjectType
/** @var array<string, MethodCall> */
private $methodCalls = [];

/** @var Scope */
private $scope;

final public function __construct(
string $className,
Scope $scope,
?Type $subtractedType = null
)
{
parent::__construct($className, $subtractedType);
$this->scope = $scope;
}

final public function getScope(): Scope
{
return $this->scope;
}

/**
Expand All@@ -34,7 +45,7 @@ public function getMethodCalls(): array

public function append(MethodCall $methodCall): self
{
$object = new static($this->getClassName());
$object = new static($this->getClassName(), $this->getScope());
$object->methodCalls = $this->methodCalls;
$object->methodCalls[substr(md5(uniqid()), 0, 10)] = $methodCall;

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,7 @@ public function dataFileAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__ . '/../data/QueryResult/queryBuilderGetQuery.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../data/QueryResult/bug-245.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../data/QueryResult/bug-512.php');
}

/**
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,13 +3,15 @@
namespace QueryResult\Entities;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Embedded as ORMEmbedded;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\QueryBuilder;

/**
* @Entity
Expand DownExpand Up@@ -71,4 +73,11 @@ public function __construct()
{
$this->subOne = new SubOne();
}

public static function createQueryBuilder(EntityManagerInterface $em): QueryBuilder
{
return $em->createQueryBuilder()
->select('o')
->from(self::class, 'o');
}
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace QueryResult\Bug512;

use Doctrine\ORM\EntityManagerInterface;
use QueryResult\Entities\One;
use function PHPStan\Testing\assertType;

class Bug512
{
public function test(EntityManagerInterface $em): void
{
$query = One::createQueryBuilder($em)->getQuery();
assertType('Doctrine\ORM\Query<null, QueryResult\Entities\One>', $query);
assertType('list<QueryResult\Entities\One>', $query->getResult());
}
}