Skip to content
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

Resolve methodcall args in QueryBuilderType with proper Scope #513

Open
wants to merge 2 commits into
base: 1.4.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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public function getTypeFromMethodCall(
}

return new $class(
$this->queryBuilderClass ?? 'Doctrine\ORM\QueryBuilder'
$this->queryBuilderClass ?? 'Doctrine\ORM\QueryBuilder',
$scope
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To pass Scope in a type is weird. Perhaps we should do argumentsProcessor->processArgs sooner and carry the final args in the Type? Or something different like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my original thought, but this felt easier. Anyway, do you see any real issue with this approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw to fix the failing tests, I probably need to store Scope per the method call inside.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is still going to be wrong in some cases, as the failing tests prove. One Scope per QueryBuilder is not enough as the scope changes when the QB gets built. Better process the args sooner.

);
}

Expand Down
Original file line number Diff line number Diff 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
13 changes: 12 additions & 1 deletion src/Type/Doctrine/QueryBuilder/QueryBuilderType.php
Original file line number Diff line number Diff 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 number Diff line number Diff 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
9 changes: 9 additions & 0 deletions tests/Type/Doctrine/data/QueryResult/Entities/One.php
Original file line number Diff line number Diff 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 Down Expand 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');
}
}
17 changes: 17 additions & 0 deletions tests/Type/Doctrine/data/QueryResult/bug-512.php
Original file line number Diff line number Diff 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());
}
}