Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve intersection of generic class string #1724

Merged
merged 1 commit into from
Sep 22, 2022
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
9 changes: 9 additions & 0 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateBenevolentUnionType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeFactory;
Expand Down Expand Up @@ -975,6 +976,14 @@ public static function intersect(Type ...$types): Type
continue 2;
}

if ($types[$i] instanceof GenericClassStringType && $types[$j] instanceof GenericClassStringType) {
$genericType = self::intersect($types[$i]->getGenericType(), $types[$j]->getGenericType());
$types[$i] = new GenericClassStringType($genericType);
array_splice($types, $j--, 1);
$typesCount--;
continue;
}

continue;
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/cli-globals.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8033.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/constant-array-union-unshift.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7987.php');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/data/bug-4875.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function doFoo()
$mock = $this->mockIt(Blah::class);

assertType('Bug4875\Blah&Bug4875\Mock', $mock);
assertType('class-string<Bug4875\Blah>&class-string<Bug4875\Mock>&literal-string', $mock::class);
assertType('class-string<Bug4875\Blah>&class-string<Bug4875\Mock>', get_class($mock));
assertType('class-string<Bug4875\Blah&Bug4875\Mock>&literal-string', $mock::class);
assertType('class-string<Bug4875\Blah&Bug4875\Mock>', get_class($mock));
Copy link
Member

Choose a reason for hiding this comment

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

Can you please explain this a bit? Why class-string<Bug4875\Blah>&class-string<Bug4875\Mock> and class-string<Bug4875\Blah&Bug4875\Mock> aren't equivalent and why this change fixes phpstan/phpstan#7987? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The most important fix is collapsing class-string<T of A|B>&class-string<A> into class-string<T of A>. This allows inferring T more reliably.

Semantically they represent the same thing, and since the collapsed form improves template type inference I think it's better to collapse it.

Copy link
Member

Choose a reason for hiding this comment

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

Nice, thank you!

Copy link

Choose a reason for hiding this comment

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

thank you both! I will test soon and report back if there are any issues

}

}
53 changes: 53 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7987.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);

namespace Bug7987;

use function PHPStan\Testing\assertType;

class AbstractA{}
class AbstractB{}

class Factory
{
/**
* @template TypeObject of AbstractA|AbstractB
* @param class-string<TypeObject> $objectClass
* @return TypeObject
*/
public function getObject(string $objectClass): AbstractA|AbstractB
{
if (is_subclass_of($objectClass, AbstractA::class)) {
assertType('class-string<TypeObject of Bug7987\AbstractA (method Bug7987\Factory::getObject(), argument)>', $objectClass);
$object = $this->getObjectA($objectClass);
assertType('TypeObject of Bug7987\AbstractA (method Bug7987\Factory::getObject(), argument)', $object);
} elseif (is_subclass_of($objectClass, AbstractB::class)) {
assertType('class-string<TypeObject of Bug7987\AbstractB (method Bug7987\Factory::getObject(), argument)>', $objectClass);
$object = $this->getObjectB($objectClass);
assertType('TypeObject of Bug7987\AbstractB (method Bug7987\Factory::getObject(), argument)', $object);
} else {
throw new \Exception("unable to instantiate $objectClass");
}
assertType('TypeObject of Bug7987\AbstractA (method Bug7987\Factory::getObject(), argument)|TypeObject of Bug7987\AbstractB (method Bug7987\Factory::getObject(), argument)', $object);
return $object;
}

/**
* @template TypeObject of AbstractA
* @param class-string<TypeObject> $objectClass
* @return TypeObject
*/
private function getObjectA(string $objectClass): AbstractA
{
return new $objectClass();
}

/**
* @template TypeObject of AbstractB
* @param class-string<TypeObject> $objectClass
* @return TypeObject
*/
private function getObjectB(string $objectClass): AbstractB
{
return new $objectClass();
}
}