Skip to content

Commit 279a808

Browse files
committed
NewExprDynamicReturnTypeExtension - correct fallback in case of arguments exception
1 parent d2da81f commit 279a808

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

src/Type/Doctrine/QueryBuilder/Expr/NewExprDynamicReturnTypeExtension.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
use PHPStan\Broker\Broker;
99
use PHPStan\Reflection\BrokerAwareExtension;
1010
use PHPStan\Reflection\MethodReflection;
11-
use PHPStan\Reflection\ParametersAcceptorSelector;
1211
use PHPStan\Rules\Doctrine\ORM\DynamicQueryBuilderArgumentException;
1312
use PHPStan\Type\Doctrine\ArgumentsProcessor;
1413
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
14+
use PHPStan\Type\ObjectType;
1515
use PHPStan\Type\Type;
1616

1717
class NewExprDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension, BrokerAwareExtension
@@ -58,9 +58,7 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
5858

5959
$className = $scope->resolveName($methodCall->class);
6060
if (!$this->broker->hasClass($className)) {
61-
return ParametersAcceptorSelector::selectSingle(
62-
$methodReflection->getVariants()
63-
)->getReturnType();
61+
return new ObjectType($className);
6462
}
6563

6664
try {
@@ -72,9 +70,7 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
7270
)
7371
);
7472
} catch (DynamicQueryBuilderArgumentException $e) {
75-
return ParametersAcceptorSelector::selectSingle(
76-
$methodReflection->getVariants()
77-
)->getReturnType();
73+
return new ObjectType($this->broker->getClassName($className));
7874
}
7975

8076
return new ExprType($className, $exprObject);

tests/Type/Doctrine/NewExprTest.php

+38
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 NewExprTest extends TypeInferenceTestCase
8+
{
9+
10+
/**
11+
* @return iterable<mixed>
12+
*/
13+
public function dataFileAsserts(): iterable
14+
{
15+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-191.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-191.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Bug191;
4+
5+
use Doctrine\ORM\Query\Expr\Func;
6+
use Doctrine\ORM\Query\Expr\Literal;
7+
use function PHPStan\Testing\assertType;
8+
9+
class Foo
10+
{
11+
12+
public function getTopicConcatExpression(): void
13+
{
14+
$func = new Func('CONCAT_WS', [
15+
new Literal("'.'"),
16+
'event.type.businessDomain',
17+
'event.type.group',
18+
'event.type.resource',
19+
'event.type.subResource',
20+
'event.type.action',
21+
'event.type.version',
22+
]);
23+
assertType(Func::class, $func);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)