Skip to content

Commit 3692144

Browse files
Do not specify a variable which is not a query builder (#220)
1 parent de3232a commit 3692144

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

src/Type/Doctrine/QueryBuilder/QueryBuilderTypeSpecifyingExtension.php

+5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public function specifyTypes(MethodReflection $methodReflection, MethodCall $nod
8585
$queryBuilderNode = $queryBuilderNode->var;
8686
}
8787

88+
// If the variable is not a query builder, there is nothing to specify
89+
if (!(new ObjectType(QueryBuilder::class))->isSuperTypeOf($scope->getType($queryBuilderNode))->yes()) {
90+
return new SpecifiedTypes([]);
91+
}
92+
8893
$resultTypes = [];
8994
foreach ($queryBuilderTypes as $queryBuilderType) {
9095
$resultTypes[] = $queryBuilderType->append($node);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Doctrine;
4+
5+
use PHPStan\Testing\TypeInferenceTestCase;
6+
7+
class QueryBuilderTypeSpecifyingExtensionTest extends TypeInferenceTestCase
8+
{
9+
10+
/**
11+
* @return iterable<mixed>
12+
*/
13+
public function dataFileAsserts(): iterable
14+
{
15+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-219.php');
16+
}
17+
18+
/**
19+
* @dataProvider dataFileAsserts
20+
* @param string $assertType
21+
* @param string $file
22+
* @param mixed ...$args
23+
*/
24+
public function testFileAsserts(
25+
string $assertType,
26+
string $file,
27+
...$args
28+
): void
29+
{
30+
$this->assertFileAsserts($assertType, $file, ...$args);
31+
}
32+
33+
public static function getAdditionalConfigFiles(): array
34+
{
35+
return [__DIR__ . '/../../../extension.neon'];
36+
}
37+
38+
}

tests/Type/Doctrine/data/bug-219.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug219;
4+
5+
use Doctrine\ORM\EntityManagerInterface;
6+
use Doctrine\ORM\EntityRepository;
7+
8+
use function PHPStan\Testing\assertType;
9+
10+
class Test
11+
{
12+
13+
/** @var EntityManagerInterface */
14+
private $entityManager;
15+
16+
public function __construct(EntityManagerInterface $entityManager)
17+
{
18+
$this->entityManager = $entityManager;
19+
}
20+
21+
public function getEntityManager(): EntityManagerInterface
22+
{
23+
return $this->entityManager;
24+
}
25+
26+
public function update(): void
27+
{
28+
$this->getEntityManager()->getRepository(\stdClass::class)->createQueryBuilder('t')->update();
29+
30+
assertType('$this(Bug219\Test)', $this);
31+
}
32+
33+
}

0 commit comments

Comments
 (0)