Skip to content
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);
}

}