Skip to content

Do not specify a variable which is not a query builder #220

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 2 commits into from
Oct 7, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public function specifyTypes(MethodReflection $methodReflection, MethodCall $nod
$queryBuilderNode = $queryBuilderNode->var;
}

// If the variable is not a query builder, there is nothing to specify
if (!(new ObjectType(QueryBuilder::class))->isSuperTypeOf($scope->getType($queryBuilderNode))->yes()) {
return new SpecifiedTypes([]);
}

$resultTypes = [];
foreach ($queryBuilderTypes as $queryBuilderType) {
$resultTypes[] = $queryBuilderType->append($node);
Expand Down
38 changes: 38 additions & 0 deletions tests/Type/Doctrine/QueryBuilderTypeSpecifyingExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Doctrine;

use PHPStan\Testing\TypeInferenceTestCase;

class QueryBuilderTypeSpecifyingExtensionTest extends TypeInferenceTestCase
{

/**
* @return iterable<mixed>
*/
public function dataFileAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-219.php');
}

/**
* @dataProvider dataFileAsserts
* @param string $assertType
* @param string $file
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [__DIR__ . '/../../../extension.neon'];
}

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

namespace Bug219;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;

use function PHPStan\Testing\assertType;

class Test
{

/** @var EntityManagerInterface */
private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

public function getEntityManager(): EntityManagerInterface
{
return $this->entityManager;
}

public function update(): void
{
$this->getEntityManager()->getRepository(\stdClass::class)->createQueryBuilder('t')->update();

assertType('$this(Bug219\Test)', $this);
}

}