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 @@ -9,6 +9,7 @@
use PHPStan\Type\Doctrine\ArgumentsProcessor;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
use Throwable;
use function get_class;
use function in_array;
use function is_object;
Expand Down Expand Up @@ -55,7 +56,12 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
return null;
}

$exprValue = $expr->{$methodReflection->getName()}(...$args);
try {
$exprValue = $expr->{$methodReflection->getName()}(...$args);
} catch (Throwable $e) {
Comment thread
simPod marked this conversation as resolved.
return null;
}

Comment thread
staabm marked this conversation as resolved.
if (is_object($exprValue)) {
return new ExprType(get_class($exprValue), $exprValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
use Throwable;
use function get_class;
use function is_object;
use function method_exists;
Expand Down Expand Up @@ -74,7 +75,12 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
return null;
}

$exprValue = $expr->{$methodReflection->getName()}(...$args);
try {
Comment thread
simPod marked this conversation as resolved.
$exprValue = $expr->{$methodReflection->getName()}(...$args);
} catch (Throwable $e) {
return null;
}

if (is_object($exprValue)) {
return new ExprType(get_class($exprValue), $exprValue);
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Rules/Doctrine/ORM/QueryBuilderDqlFirstClassCallableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Doctrine\ORM;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;

/**
* @extends RuleTestCase<QueryBuilderDqlRule>
*/
class QueryBuilderDqlFirstClassCallableTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new QueryBuilderDqlRule(
new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', __DIR__ . '/../../../../tmp'),
true,
);
}

public function testFirstClassCallableInMatchArm(): void
{
$this->analyse([__DIR__ . '/data/query-builder-first-class-callable.php'], []);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../../../extension.neon',
__DIR__ . '/entity-manager.neon',
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types = 1); // lint >= 8.1

namespace PHPStan\Rules\Doctrine\ORM;

use Doctrine\ORM\EntityManager;

class TestFirstClassCallableExpr
{

/** @var EntityManager */
private $entityManager;

public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}

public function matchArmWithFirstClassCallable(): void
{
$queryBuilder = $this->entityManager->createQueryBuilder();
$expr = $queryBuilder->expr();

$comparator = match (random_int(0, 1)) {
0 => $expr->in(...),
1 => $expr->notIn(...),
};

$queryBuilder->select('e')
->from(MyEntity::class, 'e')
->andWhere($comparator('e.id', [1, 2, 3]));
$queryBuilder->getQuery();
}

public function variableWithFirstClassCallable(): void
{
$queryBuilder = $this->entityManager->createQueryBuilder();
$expr = $queryBuilder->expr();

$fn = $expr->eq(...);

$queryBuilder->select('e')
->from(MyEntity::class, 'e')
->andWhere($fn('e.id', '1'));
$queryBuilder->getQuery();
}

}
3 changes: 3 additions & 0 deletions tests/Type/Doctrine/data/QueryResult/baseExpressionAndOr.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@

$string = $and->__toString();
assertType("string", $string);

$invalidAdd = $and->add(123);
assertType("Doctrine\ORM\Query\Expr\Andx", $invalidAdd);
Loading