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
2 changes: 1 addition & 1 deletion .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:

-
name: 'Finalize classes'
run: vendor/bin/swiss-knife finalize-classes src tests
run: vendor/bin/swiss-knife finalize-classes src tests --dry-run

-
name: 'Detect composer dependency issues'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AddReturnTypeFromTryCatchTypeRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector\Fixture;

final class SimpleTryCatch
{
public function run()
{
try {
return 1;
} catch (\Exception $e) {
return 2;
}
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector\Fixture;

final class SimpleTryCatch
{
public function run(): int
{
try {
return 1;
} catch (\Exception $e) {
return 2;
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector\Fixture;

final class SimpleTryCatch
{
public function run()
{
try {
return 1;
} catch (\Exception $e) {
return $this->getIntDoc();
}
}

/**
* @return int
*/
private function getIntDoc()
{
return mt_rand(0, 1) ? 'string' : 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector\Fixture;

final class SkipIfNoCatch
{
public function run()
{
try {
return 1;
} finally {
return 5;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector\Fixture;

interface SkipInterface
{
public function run();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector\Fixture;

final class SkipKnownType
{
public function run(): int
{
try {

return 1;

} catch (\Exception $e) {

return 2;

}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector\Fixture;

final class SkipSilentVoid
{
public function run()
{
try {
if (rand(0, 1)) {
return;
}

return 1;
} catch (\Exception $e) {
return 2;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeFromTryCatchTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AddReturnTypeFromTryCatchTypeRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* @see \Rector\Tests\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallRector\ArrayDimFetchToMethodCallRectorTest
*/
class ArrayDimFetchToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface
final class ArrayDimFetchToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var ArrayDimFetchToMethodCall[]
Expand Down Expand Up @@ -101,16 +101,16 @@ public function configure(array $configuration): void
$this->arrayDimFetchToMethodCalls = $configuration;
}

private function handleIsset(Isset_ $node): Expr|int|null
private function handleIsset(Isset_ $isset): Expr|int|null
{
$issets = [];
$exprs = [];

foreach ($node->vars as $var) {
foreach ($isset->vars as $var) {
if ($var instanceof ArrayDimFetch) {
$methodCall = $this->getMethodCall($var, 'exists');

if ($methodCall !== null) {
if ($methodCall instanceof MethodCall) {
$exprs[] = $methodCall;
continue;
}
Expand All @@ -124,30 +124,30 @@ private function handleIsset(Isset_ $node): Expr|int|null
}

if ($issets !== []) {
$node->vars = $issets;
array_unshift($exprs, $node);
$isset->vars = $issets;
array_unshift($exprs, $isset);
}

return array_reduce(
$exprs,
fn (?Expr $carry, Expr $expr) => $carry === null ? $expr : new BooleanAnd($carry, $expr),
fn (?Expr $carry, Expr $expr): Isset_|MethodCall|BooleanAnd => $carry instanceof Expr ? new BooleanAnd($carry, $expr) : $expr,
null,
);
}

/**
* @return Stmt[]|int
*/
private function handleUnset(Unset_ $node): array|int
private function handleUnset(Unset_ $unset): array|int
{
$unsets = [];
$stmts = [];

foreach ($node->vars as $var) {
foreach ($unset->vars as $var) {
if ($var instanceof ArrayDimFetch) {
$methodCall = $this->getMethodCall($var, 'unset');

if ($methodCall !== null) {
if ($methodCall instanceof MethodCall) {
$stmts[] = new Expression($methodCall);
continue;
}
Expand All @@ -161,8 +161,8 @@ private function handleUnset(Unset_ $node): array|int
}

if ($unsets !== []) {
$node->vars = $unsets;
array_unshift($stmts, $node);
$unset->vars = $unsets;
array_unshift($stmts, $unset);
}

return $stmts;
Expand All @@ -171,14 +171,14 @@ private function handleUnset(Unset_ $node): array|int
/**
* @param 'get'|'set'|'exists'|'unset' $action
*/
private function getMethodCall(ArrayDimFetch $fetch, string $action, ?Expr $value = null): ?MethodCall
private function getMethodCall(ArrayDimFetch $arrayDimFetch, string $action, ?Expr $expr = null): ?MethodCall
{
if (!$fetch->dim instanceof Node) {
if (!$arrayDimFetch->dim instanceof Node) {
return null;
}

foreach ($this->arrayDimFetchToMethodCalls as $arrayDimFetchToMethodCall) {
if (!$this->isObjectType($fetch->var, $arrayDimFetchToMethodCall->getObjectType())) {
if (!$this->isObjectType($arrayDimFetch->var, $arrayDimFetchToMethodCall->getObjectType())) {
continue;
}

Expand All @@ -193,13 +193,13 @@ private function getMethodCall(ArrayDimFetch $fetch, string $action, ?Expr $valu
continue;
}

$args = [new Arg($fetch->dim)];
$args = [new Arg($arrayDimFetch->dim)];

if ($value instanceof Expr) {
$args[] = new Arg($value);
if ($expr instanceof Expr) {
$args[] = new Arg($expr);
}

return new MethodCall($fetch->var, $method, $args);
return new MethodCall($arrayDimFetch->var, $method, $args);
}

return null;
Expand Down
Loading