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
@@ -0,0 +1,33 @@
<?php

/**
* No Namespace and multiple classes is on purpose to reproduce the issue
*/
class HttpException extends \RuntimeException
{
private int $statusCode;
private array $headers;

public function __construct(int $statusCode, string $message = '', ?\Throwable $previous = null, array $headers = [], int $code = 0)
{
$this->statusCode = $statusCode;
$this->headers = $headers;

parent::__construct($message, $code, $previous);
}
}

class BadGatewayHttpException extends HttpException
{
public function __construct(string $message = '', ?Throwable $previous = null, array $headers = [], int $code = 0)
{
parent::__construct(Response::HTTP_BAD_GATEWAY, $message, $previous, $headers, $code);
}
}


try {
// do sth
} catch (\Throwable $exception) {
throw new BadGatewayHttpException('test exception', $exception);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
Expand Down Expand Up @@ -186,12 +187,6 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable)
private function resolveExceptionArgumentPosition(Name $exceptionName): ?int
{
$className = $this->getName($exceptionName);

// is native exception?
if (! \str_contains($className, '\\')) {
return self::DEFAULT_EXCEPTION_ARGUMENT_POSITION;
}

if (! $this->reflectionProvider->hasClass($className)) {
return self::DEFAULT_EXCEPTION_ARGUMENT_POSITION;
}
Expand All @@ -211,11 +206,16 @@ private function resolveExceptionArgumentPosition(Name $exceptionName): ?int
$parameterType = $extendedParameterReflection->getType();
$className = ClassNameFromObjectTypeResolver::resolve($extendedParameterReflection->getType());

$objectType = new ObjectType('Throwable');
if ($className === null) {
$parameterType = TypeCombinator::removeNull($parameterType);
if ($objectType->isSuperTypeOf($parameterType)->yes()) {
return $position;
}

continue;
}

$objectType = new ObjectType('Throwable');
if ($objectType->isSuperTypeOf($parameterType)->no()) {
continue;
}
Expand Down