Skip to content

Commit

Permalink
Merge pull request #98 from oliverklee/bugfix/transitive-dep
Browse files Browse the repository at this point in the history
[BUGFIX] Throw ShouldNotHappenException if ServerRequestInterface is missing
  • Loading branch information
sascha-egerer authored Oct 30, 2022
2 parents 3d6d7d2 + 0e14a31 commit 778aa50
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/Rule/RequestAttributeValidationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ public function processNode(Node $node, Scope $scope): array

$declaringClass = $methodReflection->getDeclaringClass();

if (!$declaringClass->implementsInterface(ServerRequestInterface::class)
&& $declaringClass->getName() !== ServerRequestInterface::class) {
return [];
if (interface_exists(ServerRequestInterface::class)) {
if (!$declaringClass->implementsInterface(ServerRequestInterface::class)
&& $declaringClass->getName() !== ServerRequestInterface::class) {
return [];
}
}

$argument = $node->getArgs()[0] ?? null;
Expand Down
9 changes: 8 additions & 1 deletion src/Type/RequestDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use Psr\Http\Message\ServerRequestInterface;

class RequestDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
Expand All @@ -31,7 +32,13 @@ public function __construct(array $requestGetAttributeMapping, TypeStringResolve

public function getClass(): string
{
return \Psr\Http\Message\ServerRequestInterface::class;
if (!interface_exists(ServerRequestInterface::class)) {
throw new \PHPStan\ShouldNotHappenException(
'The package "psr/http-message" is not installed, but should be.'
);
}

return ServerRequestInterface::class;
}

public function getTypeFromMethodCall(
Expand Down

0 comments on commit 778aa50

Please sign in to comment.