Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 310ab0f

Browse files
Merge remote-tracking branch 'origin/1.3.x' into 1.4.x
2 parents eb6e8eb + 85def57 commit 310ab0f

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

‎src/Type/Doctrine/ArgumentsProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ArgumentsProcessor
1515

1616
/**
1717
* @param Arg[] $methodCallArgs
18-
* @return mixed[]
18+
* @return list<mixed>
1919
* @throws DynamicQueryBuilderArgumentException
2020
*/
2121
public function processArgs(

‎src/Type/Doctrine/QueryBuilder/QueryBuilderGetQueryDynamicReturnTypeExtension.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use PHPStan\Type\Type;
2525
use PHPStan\Type\TypeCombinator;
2626
use Throwable;
27+
use function array_slice;
2728
use function count;
2829
use function in_array;
2930
use function method_exists;
@@ -152,6 +153,18 @@ public function getTypeFromMethodCall(
152153
continue;
153154
}
154155

156+
if ($lowerMethodName === 'set') {
157+
try {
158+
$args = $this->argumentsProcessor->processArgs($scope, $methodName, array_slice($calledMethodCall->getArgs(), 0, 1));
159+
} catch (DynamicQueryBuilderArgumentException $e) {
160+
return $defaultReturnType;
161+
}
162+
if (count($args) === 1) {
163+
$queryBuilder->set($args[0], $args[0]);
164+
continue;
165+
}
166+
}
167+
155168
if (!method_exists($queryBuilder, $methodName)) {
156169
continue;
157170
}

‎tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ public function testAndOrVariants(): void
129129
$this->analyse([__DIR__ . '/data/query-builder-dql-and-or-variants.php'], []);
130130
}
131131

132+
public function testUpdateIssue(): void
133+
{
134+
$this->analyse([__DIR__ . '/data/query-builder-dql-update.php'], [
135+
[
136+
'Could not analyse QueryBuilder with dynamic arguments.',
137+
31,
138+
],
139+
]);
140+
}
141+
132142
public function testBranchingPerformance(): void
133143
{
134144
$this->analyse([__DIR__ . '/data/query-builder-branches-performance.php'], [
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Doctrine\ORM;
4+
5+
use Doctrine\ORM\EntityManager;
6+
7+
class TestUpdateQueryBuilder
8+
{
9+
10+
/** @var EntityManager */
11+
private $entityManager;
12+
13+
public function __construct(EntityManager $entityManager)
14+
{
15+
$this->entityManager = $entityManager;
16+
}
17+
18+
public function nonDynamicSet(int $int, string $title): void
19+
{
20+
$this->entityManager->createQueryBuilder()
21+
->update(MyEntity::class, 'e')
22+
->set('e.title', $title)
23+
->andWhere('e.id = :int')
24+
->setParameter('int', $int)
25+
->getQuery()
26+
->execute();
27+
}
28+
29+
public function dynamicSet(int $int, string $field, string $title): void
30+
{
31+
$this->entityManager->createQueryBuilder()
32+
->update(MyEntity::class, 'e')
33+
->set('e.' . $field, $title)
34+
->andWhere('e.id = :int')
35+
->setParameter('int', $int)
36+
->getQuery()
37+
->execute();
38+
}
39+
40+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /