Skip to content

Commit a64eb7b

Browse files
committed
Take advantage of ArgumentsProcessor for ExprType
1 parent ec12e6f commit a64eb7b

File tree

6 files changed

+49
-14
lines changed

6 files changed

+49
-14
lines changed

extension.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,39 +104,46 @@ services:
104104
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
105105
arguments:
106106
class: Doctrine\ORM\Query\Expr\Base
107+
argumentsProcessor: @argumentsProcessor
107108
-
108109
class: PHPStan\Type\Doctrine\QueryBuilder\Expr\NewExprDynamicReturnTypeExtension
109110
tags:
110111
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
111112
arguments:
112113
class: Doctrine\ORM\Query\Expr\Comparison
114+
argumentsProcessor: @argumentsProcessor
113115
-
114116
class: PHPStan\Type\Doctrine\QueryBuilder\Expr\NewExprDynamicReturnTypeExtension
115117
tags:
116118
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
117119
arguments:
118120
class: Doctrine\ORM\Query\Expr\From
121+
argumentsProcessor: @argumentsProcessor
119122
-
120123
class: PHPStan\Type\Doctrine\QueryBuilder\Expr\NewExprDynamicReturnTypeExtension
121124
tags:
122125
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
123126
arguments:
124127
class: Doctrine\ORM\Query\Expr\Func
128+
argumentsProcessor: @argumentsProcessor
125129
-
126130
class: PHPStan\Type\Doctrine\QueryBuilder\Expr\NewExprDynamicReturnTypeExtension
127131
tags:
128132
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
129133
arguments:
130134
class: Doctrine\ORM\Query\Expr\Join
135+
argumentsProcessor: @argumentsProcessor
131136
-
132137
class: PHPStan\Type\Doctrine\QueryBuilder\Expr\NewExprDynamicReturnTypeExtension
133138
tags:
134139
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
135140
arguments:
136141
class: Doctrine\ORM\Query\Expr\Math
142+
argumentsProcessor: @argumentsProcessor
137143
-
138144
class: PHPStan\Type\Doctrine\QueryBuilder\Expr\NewExprDynamicReturnTypeExtension
139145
tags:
140146
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
141147
arguments:
142148
class: Doctrine\ORM\Query\Expr\OrderBy
149+
argumentsProcessor: @argumentsProcessor

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
@dataProvider,
2525
@requires
2626
"/>
27+
<property name="enableObjectTypeHint" value="false"/>
2728
</properties>
2829
</rule>
2930
<rule ref="SlevomatCodingStandard.ControlStructures.AssignmentInCondition"/>

src/Type/Doctrine/ArgumentsProcessor.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public function processArgs(
3030
$value instanceof ExprType
3131
&& strpos($value->getClassName(), 'Doctrine\ORM\Query\Expr') === 0
3232
) {
33-
$className = $value->getClassName();
34-
$args[] = new $className(...$this->processArgs($scope, '__construct', $value->getConstructorArgs()));
33+
$args[] = $value->getExprObject();
3534
continue;
3635
}
3736
// todo $qb->expr() support

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@
77
class ExprType extends ObjectType
88
{
99

10-
/** @var \PhpParser\Node\Arg[] */
11-
private $constructorArgs;
10+
/** @var object */
11+
private $exprObject;
1212

1313
/**
1414
* @param string $className
15-
* @param \PhpParser\Node\Arg[] $constructorArgs
15+
* @param object $exprObject
1616
*/
17-
public function __construct(string $className, array $constructorArgs)
17+
public function __construct(string $className, $exprObject)
1818
{
1919
parent::__construct($className);
20-
$this->constructorArgs = $constructorArgs;
20+
$this->exprObject = $exprObject;
2121
}
2222

2323
/**
24-
* @return \PhpParser\Node\Arg[]
24+
* @return object
2525
*/
26-
public function getConstructorArgs(): array
26+
public function getExprObject()
2727
{
28-
return $this->constructorArgs;
28+
return $this->exprObject;
2929
}
3030

3131
}

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,27 @@
66
use PhpParser\Node\Name;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Reflection\MethodReflection;
9+
use PHPStan\Reflection\ParametersAcceptorSelector;
10+
use PHPStan\Rules\Doctrine\ORM\DynamicQueryBuilderArgumentException;
11+
use PHPStan\Type\Doctrine\ArgumentsProcessor;
912
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
1013
use PHPStan\Type\Type;
1114

1215
class NewExprDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
1316
{
1417

18+
/** @var \PHPStan\Type\Doctrine\ArgumentsProcessor */
19+
private $argumentsProcessor;
20+
1521
/** @var string */
1622
private $class;
1723

18-
public function __construct(string $class)
24+
public function __construct(
25+
ArgumentsProcessor $argumentsProcessor,
26+
string $class
27+
)
1928
{
29+
$this->argumentsProcessor = $argumentsProcessor;
2030
$this->class = $class;
2131
}
2232

@@ -35,7 +45,24 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
3545
if (!$methodCall->class instanceof Name) {
3646
throw new \PHPStan\ShouldNotHappenException();
3747
}
38-
return new ExprType($methodCall->class->toString(), $methodCall->args);
48+
49+
$className = $methodCall->class->toString();
50+
51+
try {
52+
$exprObject = new $className(
53+
...$this->argumentsProcessor->processArgs(
54+
$scope,
55+
$methodReflection->getName(),
56+
$methodCall->args
57+
)
58+
);
59+
} catch (DynamicQueryBuilderArgumentException $e) {
60+
return ParametersAcceptorSelector::selectSingle(
61+
$methodReflection->getVariants()
62+
)->getReturnType();
63+
}
64+
65+
return new ExprType($className, $exprObject);
3966
}
4067

4168
}

tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ protected function getMethodTypeSpecifyingExtensions(): array
105105
*/
106106
public function getDynamicStaticMethodReturnTypeExtensions(): array
107107
{
108+
$argumentsProcessor = new ArgumentsProcessor();
108109
return [
109-
new NewExprDynamicReturnTypeExtension(OrderBy::class),
110-
new NewExprDynamicReturnTypeExtension(Base::class),
110+
new NewExprDynamicReturnTypeExtension($argumentsProcessor, OrderBy::class),
111+
new NewExprDynamicReturnTypeExtension($argumentsProcessor, Base::class),
111112
];
112113
}
113114

0 commit comments

Comments
 (0)