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
Expand Up @@ -8,8 +8,8 @@ class EmptyBrackets
{
try {
$someCode = 1;
} catch (Throwable $throwable) {
throw new AnotherException;
} catch (\Throwable $throwable) {
throw new \RuntimeException;
}
}
}
Expand All @@ -26,8 +26,8 @@ class EmptyBrackets
{
try {
$someCode = 1;
} catch (Throwable $throwable) {
throw new AnotherException($throwable->getMessage(), $throwable->getCode(), $throwable);
} catch (\Throwable $throwable) {
throw new \RuntimeException($throwable->getMessage(), $throwable->getCode(), $throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Fixture
{
try {
$someCode = 1;
} catch (Throwable $throwable) {
throw new AnotherException('ups');
} catch (\Throwable $throwable) {
throw new \RuntimeException('ups');
}
}
}
Expand All @@ -26,8 +26,8 @@ class Fixture
{
try {
$someCode = 1;
} catch (Throwable $throwable) {
throw new AnotherException('ups', $throwable->getCode(), $throwable);
} catch (\Throwable $throwable) {
throw new \RuntimeException('ups', $throwable->getCode(), $throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class NamedArgument
try {
$this->run();
}catch(\Throwable $throwable) {
throw new LogicException('Some exception', previous: $throwable);
throw new \LogicException('Some exception', previous: $throwable);
}
}
}
Expand All @@ -27,7 +27,7 @@ class NamedArgument
try {
$this->run();
}catch(\Throwable $throwable) {
throw new LogicException('Some exception', $throwable->getCode(), previous: $throwable);
throw new \LogicException('Some exception', $throwable->getCode(), previous: $throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class NamedArgumentForMessage
try {
$this->run();
}catch(\Throwable $throwable) {
throw new LogicException(message: 'Some exception');
throw new \LogicException(message: 'Some exception');
}
}
}
Expand All @@ -27,7 +27,7 @@ class NamedArgumentForMessage
try {
$this->run();
}catch(\Throwable $throwable) {
throw new LogicException(message: 'Some exception', code: $throwable->getCode(), previous: $throwable);
throw new \LogicException(message: 'Some exception', code: $throwable->getCode(), previous: $throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Catch_\ThrowWithPreviousExceptionRector\Fixture;

use stdClass;

class CustomError extends \Exception
{
public function __construct(
string $message = '',
$nodes = null,
?stdClass $source = null,
?array $positions = null,
?array $path = null,
?\Throwable $previous = null,
?array $extensions = null,
?array $unaliasedPath = null
) {
parent::__construct($message, 0, $previous);
}
}

try {
throw new \InvalidArgumentException('foo');
} catch (\InvalidArgumentException $e) {
throw new CustomError(message: 'Some error message', previous: $e);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class HttpException extends \RuntimeException

class BadGatewayHttpException extends HttpException
{
public function __construct(string $message = '', ?Throwable $previous = null, array $headers = [], int $code = 0)
public function __construct(string $message = '', ?\Throwable $previous = null, array $headers = [], int $code = 0)
{
parent::__construct(Response::HTTP_BAD_GATEWAY, $message, $previous, $headers, $code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ class SkipFilledExceptionWithDifferentLocation
public function run()
{
try {
} catch (Throwable $throwable) {
} catch (\Throwable $throwable) {
throw new BadRequestHttpException('message some', $throwable);
}
}
}

class BadRequestHttpException extends Exception
{
public function __construct(string $message = null, \Throwable $previous = null, int $code = 0, array $headers = [])
public function __construct(string $message = null, ?\Throwable $previous = null, int $code = 0, array $headers = [])
{
parent::__construct('message', 400, $previous);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SkipMissingLocation
public function run()
{
try {
} catch (Throwable $throwable) {
} catch (\Throwable $throwable) {
throw new MissingPreviousException('message some');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function run()
try {
$someCode = 1;
} catch (Throwable $throwable) {
throw new AnotherException('ups');
throw new \RuntimeException('ups');
}
}
}
Expand All @@ -65,7 +65,7 @@ public function run()
try {
$someCode = 1;
} catch (Throwable $throwable) {
throw new AnotherException('ups', $throwable->getCode(), $throwable);
throw new \RuntimeException('ups', $throwable->getCode(), $throwable);
}
}
}
Expand Down Expand Up @@ -142,34 +142,47 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable)
$messageArgument = $new->args[0] ?? null;
$shouldUseNamedArguments = $messageArgument instanceof Arg && $messageArgument->name instanceof Identifier;

$hasChanged = false;
if (! isset($new->args[0])) {
// get previous message
$getMessageMethodCall = new MethodCall($caughtThrowableVariable, 'getMessage');
$new->args[0] = new Arg($getMessageMethodCall);
$hasChanged = true;
} elseif ($new->args[0] instanceof Arg && $new->args[0]->name instanceof Identifier && $new->args[0]->name->toString() === 'previous' && $this->nodeComparator->areNodesEqual(
$new->args[0]->value,
$caughtThrowableVariable
)) {
$new->args[0]->name->name = 'message';
$new->args[0]->value = new MethodCall($caughtThrowableVariable, 'getMessage');
$hasChanged = true;
}

if (! isset($new->getArgs()[1])) {
// get previous code
$new->args[1] = new Arg(
new MethodCall($caughtThrowableVariable, 'getCode'),
name: $shouldUseNamedArguments ? new Identifier('code') : null
);
if ($this->hasCodeParameter($new->class)) {
// get previous code
$new->args[1] = new Arg(
new MethodCall($caughtThrowableVariable, 'getCode'),
name: $shouldUseNamedArguments ? new Identifier('code') : null
);
$hasChanged = true;
} else {
return null;
}
}

/** @var Arg $arg1 */
$arg1 = $new->args[1];
if ($arg1->name instanceof Identifier && $arg1->name->toString() === 'previous') {
$new->args[1] = new Arg(
new MethodCall($caughtThrowableVariable, 'getCode'),
name: $shouldUseNamedArguments ? new Identifier('code') : null
);
$new->args[$exceptionArgumentPosition] = $arg1;
if ($this->hasCodeParameter($new->class)) {
$new->args[1] = new Arg(
new MethodCall($caughtThrowableVariable, 'getCode'),
name: $shouldUseNamedArguments ? new Identifier('code') : null
);
$new->args[$exceptionArgumentPosition] = $arg1;
$hasChanged = true;
} elseif (! $hasChanged) {
return null;
}
} else {
$new->args[$exceptionArgumentPosition] = new Arg(
$caughtThrowableVariable,
Expand All @@ -184,6 +197,34 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable)
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

private function hasCodeParameter(Name $exceptionName): bool
{
$className = $this->getName($exceptionName);
if (! $this->reflectionProvider->hasClass($className)) {
return false;
}

$classReflection = $this->reflectionProvider->getClass($className);
$construct = $classReflection->hasMethod(MethodName::CONSTRUCT);

if (! $construct) {
return false;
}

$extendedMethodReflection = $classReflection->getConstructor();
$extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors(
$extendedMethodReflection->getVariants()
);

foreach ($extendedParametersAcceptor->getParameters() as $extendedParameterReflection) {
if ($extendedParameterReflection->getName() === 'code') {
return true;
}
}

return false;
}

private function resolveExceptionArgumentPosition(Name $exceptionName): ?int
{
$className = $this->getName($exceptionName);
Expand Down
20 changes: 9 additions & 11 deletions rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ final class JsonThrowOnErrorRector extends AbstractRector implements MinPhpVersi
{
private const array FLAGS = ['JSON_THROW_ON_ERROR'];

private bool $hasChanged = false;

public function __construct(
private readonly ValueResolver $valueResolver,
private readonly BetterNodeFinder $betterNodeFinder
Expand Down Expand Up @@ -77,9 +75,9 @@ public function refactor(Node $node): ?Node
return null;
}

$this->hasChanged = false;
$hasChanged = false;

$this->traverseNodesWithCallable($node, function (Node $currentNode): ?FuncCall {
$this->traverseNodesWithCallable($node, function (Node $currentNode) use (&$hasChanged): ?FuncCall {
if (! $currentNode instanceof FuncCall) {
return null;
}
Expand All @@ -89,17 +87,17 @@ public function refactor(Node $node): ?Node
}

if ($this->isName($currentNode, 'json_encode')) {
return $this->processJsonEncode($currentNode);
return $this->processJsonEncode($currentNode, $hasChanged);
}

if ($this->isName($currentNode, 'json_decode')) {
return $this->processJsonDecode($currentNode);
return $this->processJsonDecode($currentNode, $hasChanged);
}

return null;
});

if ($this->hasChanged) {
if ($hasChanged) {
return $node;
}

Expand Down Expand Up @@ -134,7 +132,7 @@ private function shouldSkipFuncCall(FuncCall $funcCall): bool
return $this->isFirstValueStringOrArray($funcCall);
}

private function processJsonEncode(FuncCall $funcCall): FuncCall
private function processJsonEncode(FuncCall $funcCall, bool &$hasChanged): FuncCall
{
$flags = [];
if (isset($funcCall->args[1])) {
Expand All @@ -145,14 +143,14 @@ private function processJsonEncode(FuncCall $funcCall): FuncCall

$newArg = $this->getArgWithFlags($flags);
if ($newArg instanceof Arg) {
$this->hasChanged = true;
$hasChanged = true;
$funcCall->args[1] = $newArg;
}

return $funcCall;
}

private function processJsonDecode(FuncCall $funcCall): FuncCall
private function processJsonDecode(FuncCall $funcCall, bool &$hasChanged): FuncCall
{
$flags = [];
if (isset($funcCall->args[3])) {
Expand All @@ -172,7 +170,7 @@ private function processJsonDecode(FuncCall $funcCall): FuncCall

$newArg = $this->getArgWithFlags($flags);
if ($newArg instanceof Arg) {
$this->hasChanged = true;
$hasChanged = true;
$funcCall->args[3] = $newArg;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ChangesReporting/Output/GitlabOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
private const string ERROR_SEVERITY_MINOR = 'minor';

public function __construct(
private Filehasher $filehasher,
private FileHasher $filehasher,
) {
}

Expand Down