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 @@ -25,11 +25,14 @@

private OtherMethodQueryBuilderParser $otherMethodQueryBuilderParser;

private ObjectType $queryBuilderObjectType;

public function __construct(
OtherMethodQueryBuilderParser $otherMethodQueryBuilderParser
)
{
$this->otherMethodQueryBuilderParser = $otherMethodQueryBuilderParser;
$this->queryBuilderObjectType = new ObjectType(QueryBuilder::class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never worked a lot of perf issue so I rely on you @staabm.

  1. Wouldn't it be better to move this from the constructor and use
    $this->queryBuilder ??= new ObjectType(QueryBuilder::class) at the place we need it ?

  2. If we start caching ObjectType, shouldn't we start by the PHPStan repository ? I'm pretty sure we have lot of reused ObjectType and we might want to expose a dedicated factory/method which cache the instance for every instance.

In the same way we might want to cache also so ConstantType...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. $this->queryBuilder ??= new ObjectType(QueryBuilder::class) at the place we need it ?

I had the same gut feeling, but than found other classes in which we do it in __construct

2. If we start caching ObjectType, shouldn't we start by the PHPStan repository ? I'm pretty sure we have lot of reused ObjectType and we might want to expose a dedicated factory/method which cache the instance for every instance.

I think the caching we do here is only necessary when its invoked very very often. as long as we don't have evidence, I would not add another level of indirection

In the same way we might want to cache also so ConstantType...

not sure what you mean exactly. maybe I missunderstood the above

}

public function getType(Expr $expr, Scope $scope): ?Type
Expand All @@ -50,7 +53,7 @@

$returnType = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants())->getReturnType();

$returnsQueryBuilder = (new ObjectType(QueryBuilder::class))->isSuperTypeOf($returnType)->yes();
$returnsQueryBuilder = $this->queryBuilderObjectType->isSuperTypeOf($returnType)->yes();

Check warning on line 56 in src/Type/Doctrine/QueryBuilder/ReturnQueryBuilderExpressionTypeResolverExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $returnType = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants())->getReturnType(); - $returnsQueryBuilder = $this->queryBuilderObjectType->isSuperTypeOf($returnType)->yes(); + $returnsQueryBuilder = !$this->queryBuilderObjectType->isSuperTypeOf($returnType)->no(); if (!$returnsQueryBuilder) { return null;

Check warning on line 56 in src/Type/Doctrine/QueryBuilder/ReturnQueryBuilderExpressionTypeResolverExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $returnType = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants())->getReturnType(); - $returnsQueryBuilder = $this->queryBuilderObjectType->isSuperTypeOf($returnType)->yes(); + $returnsQueryBuilder = $returnType->isSuperTypeOf($this->queryBuilderObjectType)->yes(); if (!$returnsQueryBuilder) { return null;

Check warning on line 56 in src/Type/Doctrine/QueryBuilder/ReturnQueryBuilderExpressionTypeResolverExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $returnType = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants())->getReturnType(); - $returnsQueryBuilder = $this->queryBuilderObjectType->isSuperTypeOf($returnType)->yes(); + $returnsQueryBuilder = !$this->queryBuilderObjectType->isSuperTypeOf($returnType)->no(); if (!$returnsQueryBuilder) { return null;

Check warning on line 56 in src/Type/Doctrine/QueryBuilder/ReturnQueryBuilderExpressionTypeResolverExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $returnType = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants())->getReturnType(); - $returnsQueryBuilder = $this->queryBuilderObjectType->isSuperTypeOf($returnType)->yes(); + $returnsQueryBuilder = $returnType->isSuperTypeOf($this->queryBuilderObjectType)->yes(); if (!$returnsQueryBuilder) { return null;

if (!$returnsQueryBuilder) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public function testFirstClassCallableDoesNotFail(EntityManagerInterface $em): v
$this->getQueryBuilder(...);
}

public function testNullableQueryBuilderIsNotInferred(EntityManagerInterface $em): void
{
assertType('Doctrine\\ORM\\QueryBuilder|null', $this->getNullableQueryBuilder($em));
}

private function adjustQueryBuilderToIndexByInt(QueryBuilder $qb): void
{
$qb->indexBy('m', 'm.intColumn');
Expand Down Expand Up @@ -100,6 +105,13 @@ private static function getStaticQueryBuilder(EntityManagerInterface $em): Query
->from(Many::class, 'm');
}

private function getNullableQueryBuilder(EntityManagerInterface $em): ?QueryBuilder
{
return $em->createQueryBuilder()
->select('m')
->from(Many::class, 'm');
}

private function getBranchingQueryBuilder(EntityManagerInterface $em): QueryBuilder
{
$queryBuilder = $em->createQueryBuilder()
Expand Down
Loading