-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve intersection of generic class string
- Loading branch information
Richard van Velzen
committed
Sep 22, 2022
1 parent
22eeeae
commit 64b4c5f
Showing
4 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |