Skip to content

Commit

Permalink
[DowngradePhp80] Rollback to change match to switch on DowngradeMatch…
Browse files Browse the repository at this point in the history
…ToSwitchRector (#139)

* restore fixture

* restore

* regenerate docs

* roll back fixtures

* rename fixture

* update fixture

* rename fixture

* fixture fix

* more fixture rollback

* reduce parent take 1: on ArrowFunction

* [ci-review] Rector Rectify

* implemented 🎉

* rename fixture

* rename fixture

* update fixture

* fix

* fix phpstan

* fix phpstan

---------

Co-authored-by: Tomas Votruba <[email protected]>
Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
3 people authored Jun 30, 2023
1 parent 06c6448 commit 57ad03c
Show file tree
Hide file tree
Showing 26 changed files with 773 additions and 87 deletions.
26 changes: 25 additions & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,18 @@ Downgrade `match()` to `switch()`
- 400 => 'not found',
- default => 'unknown status code',
- };
+ $message = ($statusCode === 200 || $statusCode === 300 ? null : $statusCode === 400 ? 'not found' : 'unknown status code';
+ switch ($statusCode) {
+ case 200:
+ case 300:
+ $message = null;
+ break;
+ case 400:
+ $message = 'not found';
+ break;
+ default:
+ $message = 'unknown status code';
+ break;
+ }
}
}
```
Expand Down Expand Up @@ -954,6 +965,19 @@ Remove Json constant that available only in php 7.3
```diff
-json_encode($content, JSON_THROW_ON_ERROR);
+json_encode($content, 0);
+if (json_last_error() !== JSON_ERROR_NONE) {
+ throw new \Exception(json_last_error_msg());
+}
```

<br>

```diff
-$content = json_decode($json, null, 512, JSON_THROW_ON_ERROR);
+$content = json_decode($json, null, 512, 0);
+if (json_last_error() !== JSON_ERROR_NONE) {
+ throw new \Exception(json_last_error_msg());
+}
```

<br>
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,11 @@ parameters:
path: rules/DowngradePhp80/Rector/Expression/DowngradeThrowExprRector.php

- '#New objects with "\$defaultExpr" name are overridden\. This can lead to unwanted bugs, please pick a different name to avoid it#'

-
message: '#Class cognitive complexity is 60, keep it under 30#'
path: rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php

-
message: '#Cognitive complexity for "Rector\\DowngradePhp80\\Rector\\Expression\\DowngradeMatchToSwitchRector\:\:refactorWithScope\(\)" is 38, keep it under 10#'
path: rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

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

#[RunTestsInSeparateProcesses]
final class DowngradeMatchToSwitchRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ class ArrayItemKeyed
public function run($statusCode)
{
return [
'result' => $statusCode === 200 || $statusCode === 300 ? null : ($statusCode === 400 ? 'not found' : 'unknown status code'),
'result' => (function () use ($statusCode) {
switch ($statusCode) {
case 200:
case 300:
return null;
case 400:
return 'not found';
default:
return 'unknown status code';
}
})(),
];
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

class ArrayItemKeyedAssigned
{
public function run($statusCode)
{
$value = [
'result' => match ($statusCode) {
200, 300 => null,
400 => 'not found',
default => 'unknown status code',
},
];
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

class ArrayItemKeyedAssigned
{
public function run($statusCode)
{
$value = [
'result' => (function () use ($statusCode) {
switch ($statusCode) {
case 200:
case 300:
return null;
case 400:
return 'not found';
default:
return 'unknown status code';
}
})(),
];
}
}

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

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

class ArrayItemSpread
{
public function run($statusCode)
{
return [
...match ($statusCode) {
200, 300 => [null],
400 => ['not found'],
default => ['unknown status code'],
},
];
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

class ArrayItemSpread
{
public function run($statusCode)
{
return [
...(function () use ($statusCode) {
switch ($statusCode) {
case 200:
case 300:
return [null];
case 400:
return ['not found'];
default:
return ['unknown status code'];
}
})(),
];
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ final class BracketMatches
}

?>

-----
<?php

Expand All @@ -27,9 +26,24 @@ final class BracketMatches
{
public function run($delimiter)
{
$delimiter = $delimiter === '(' ? ')' : ($delimiter === '{' ? '}' : ($delimiter === '[' ? ']' : ($delimiter === '<' ? '>' : $delimiter)));
switch ($delimiter) {
case '(':
$delimiter = ')';
break;
case '{':
$delimiter = '}';
break;
case '[':
$delimiter = ']';
break;
case '<':
$delimiter = '>';
break;
default:
$delimiter = $delimiter;
break;
}
}
}

?>

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

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

$output = function($value) {
echo $value;
};

$output(match ($statusCode) {
100, 200 => null,
300 => 'not found',
default => 'unknown status code',
});

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

$output = function($value) {
echo $value;
};

switch ($statusCode) {
case 100:
case 200:
$output(null);
break;
case 300:
$output('not found');
break;
default:
$output('unknown status code');
break;
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ class Fixture
{
public function run()
{
$message = $statusCode === 200 || $statusCode === 300 ? null : ($statusCode === 400 ? 'not found' : 'unknown status code');
switch ($statusCode) {
case 200:
case 300:
$message = null;
break;
case 400:
$message = 'not found';
break;
default:
$message = 'unknown status code';
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

function output($value)
{
echo $value;
}

class FunctionCall
{
public function run($statusCode)
{
output(match ($statusCode) {
200, 300 => null,
400 => 'not found',
default => 'unknown status code',
});
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

function output($value)
{
echo $value;
}

class FunctionCall
{
public function run($statusCode)
{
switch ($statusCode) {
case 200:
case 300:
output(null);
break;
case 400:
output('not found');
break;
default:
output('unknown status code');
break;
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

final class InArrowFunction
{
public function run()
Expand All @@ -27,7 +26,6 @@ final class InArrowFunction
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;

final class InArrowFunction
{
public function run()
Expand All @@ -38,7 +36,14 @@ final class InArrowFunction
];

$mapped = array_map(
static fn (mixed $default): mixed => true === is_string($default) ? sprintf('"%s"', $default) : $default, $defaults
static function (mixed $default) : mixed {
switch (true) {
case is_string($default):
return sprintf('"%s"', $default);
default:
return $default;
}
}, $defaults
);

return $mapped;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;
final class InArrowFunction2
{
public function run($value)
{
$mapped = static fn (mixed $default): mixed => match (true) {
is_string($default) => sprintf('"%s"', $default),
default => $default,
};

return $mapped($value);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector\Fixture;
final class InArrowFunction2
{
public function run($value)
{
$mapped = static function (mixed $default) : mixed {
switch (true) {
case is_string($default):
return sprintf('"%s"', $default);
default:
return $default;
}
};

return $mapped($value);
}
}

?>
Loading

0 comments on commit 57ad03c

Please sign in to comment.