Skip to content

Commit

Permalink
Forward compatibility: Eliminate instanceof on PHPStan types
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Nov 9, 2024
1 parent 715605d commit 167ef27
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
20 changes: 11 additions & 9 deletions src/MessageBus/CommandBusDynamicMethodThrowTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPStan\Type\DynamicMethodThrowTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\VoidType;

class CommandBusDynamicMethodThrowTypeExtension implements DynamicMethodThrowTypeExtension
{
Expand Down Expand Up @@ -44,16 +43,19 @@ public function getThrowTypeFromMethodCall(MethodReflection $methodReflection, M
$throwTypes = [];

$commandBusThrowType = $methodReflection->getThrowType();
if ($commandBusThrowType !== null && ! $commandBusThrowType instanceof VoidType) {
if ($commandBusThrowType !== null && ! $commandBusThrowType->isVoid()->yes()) {
$throwTypes[] = $commandBusThrowType;
}

$commandType = $this->commandTypeExtractor->extractCommandType($methodCall, $scope);
if ($commandType !== null && $commandType !== Command::class) {
$throwTypes = array_merge(
$throwTypes,
$this->getHandlerThrowTypes($commandType, $scope),
);
$commandTypes = $this->commandTypeExtractor->extractCommandType($methodCall, $scope);
foreach ($commandTypes as $commandType) {
if ($commandType === Command::class) {
continue;
}
$throwTypes = [
...$throwTypes,
...$this->getHandlerThrowTypes($commandType, $scope),
];
}

if (count($throwTypes) === 0) {
Expand All @@ -79,7 +81,7 @@ private function getHandlerThrowTypes(string $commandType, Scope $scope): array
continue;
}
$handlerThrowType = $handlerReflection->getMethod('__invoke', $scope)->getThrowType();
if ($handlerThrowType === null || $handlerThrowType instanceof VoidType) {
if ($handlerThrowType === null || $handlerThrowType->isVoid()->yes()) {
continue;
}
$throwTypes[] = $handlerThrowType;
Expand Down
17 changes: 7 additions & 10 deletions src/MessageBus/CommandTypeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,25 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\VariadicPlaceholder;
use PHPStan\Analyser\Scope;
use PHPStan\Type\TypeWithClassName;

class CommandTypeExtractor
{

public function extractCommandType(MethodCall $methodCall, Scope $scope): ?string
/**
* @return list<string>
*/
public function extractCommandType(MethodCall $methodCall, Scope $scope): array
{
if (count($methodCall->args) === 0) {
return null;
return [];
}

$commandArgument = $methodCall->args[0];
if ($commandArgument instanceof VariadicPlaceholder) {
return null;
return [];
}

$commandType = $scope->getType($commandArgument->value);
if (! $commandType instanceof TypeWithClassName) {
return null;
}

return $commandType->getClassName();
return $scope->getType($commandArgument->value)->getObjectClassNames();
}

}

0 comments on commit 167ef27

Please sign in to comment.