mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 23:10:51 +00:00
[CodeQuality] Add SimplifyIfExactValueReturnValueRector (#2278)
This commit is contained in:
parent
30f2f91909
commit
fa073d4408
|
@ -1,4 +1,4 @@
|
|||
# 511 Rules Overview
|
||||
# 512 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
|||
|
||||
- [Arguments](#arguments) (5)
|
||||
|
||||
- [CodeQuality](#codequality) (71)
|
||||
- [CodeQuality](#codequality) (72)
|
||||
|
||||
- [CodingStyle](#codingstyle) (35)
|
||||
|
||||
|
@ -490,15 +490,9 @@ Change `array_push()` to direct variable assign
|
|||
- class: [`Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector`](../rules/CodeQuality/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$items = [];
|
||||
- array_push($items, $item);
|
||||
+ $items[] = $item;
|
||||
}
|
||||
}
|
||||
$items = [];
|
||||
-array_push($items, $item);
|
||||
+$items[] = $item;
|
||||
```
|
||||
|
||||
<br>
|
||||
|
@ -1338,6 +1332,23 @@ Changes if/else for same value as assign to ternary
|
|||
|
||||
<br>
|
||||
|
||||
### SimplifyIfExactValueReturnValueRector
|
||||
|
||||
Changes compared to value and return of expr to direct return
|
||||
|
||||
- class: [`Rector\CodeQuality\Rector\If_\SimplifyIfExactValueReturnValueRector`](../rules/CodeQuality/Rector/If_/SimplifyIfExactValueReturnValueRector.php)
|
||||
|
||||
```diff
|
||||
$value = 'something';
|
||||
-if ($value === 52) {
|
||||
- return $value;
|
||||
-}
|
||||
-
|
||||
return $value;
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### SimplifyIfIssetToNullCoalescingRector
|
||||
|
||||
Simplify binary if to null coalesce
|
||||
|
@ -2068,45 +2079,43 @@ Order attributes by desired names
|
|||
|
||||
- class: [`Rector\CodingStyle\Rector\ClassMethod\OrderAttributesRector`](../rules/CodingStyle/Rector/ClassMethod/OrderAttributesRector.php)
|
||||
|
||||
#### 1) Order by specific namespace
|
||||
|
||||
```php
|
||||
use Rector\CodingStyle\Rector\ClassMethod\OrderAttributesRector;
|
||||
use Rector\Config\RectorConfig;
|
||||
|
||||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->ruleWithConfiguration(OrderAttributesRector::class, [Annotation\First::class, Annotation\Second::class]);
|
||||
$rectorConfig->ruleWithConfiguration(OrderAttributesRector::class, ['First', 'Second']);
|
||||
};
|
||||
```
|
||||
|
||||
↓
|
||||
|
||||
```diff
|
||||
+#[Annotation\First]
|
||||
#[Annotation\Second]
|
||||
-#[Annotation\First]
|
||||
+#[First]
|
||||
#[Second]
|
||||
-#[First]
|
||||
class Someclass
|
||||
{
|
||||
}
|
||||
```
|
||||
|
||||
#### 2) Order alphabetically
|
||||
<br>
|
||||
|
||||
```php
|
||||
use Rector\CodingStyle\Rector\ClassMethod\OrderAttributesRector;
|
||||
use Rector\Config\RectorConfig;
|
||||
|
||||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->ruleWithConfiguration(OrderAttributesRector::class, [Rector\CodingStyle\Rector\ClassMethod::ALPHABETICALLY]);
|
||||
$rectorConfig->ruleWithConfiguration(OrderAttributesRector::class, ['alphabetically']);
|
||||
};
|
||||
```
|
||||
|
||||
↓
|
||||
|
||||
```diff
|
||||
+#[Annotation\AAttribute]
|
||||
#[Annotation\BAttribute]
|
||||
-#[Annotation\AAttribute]
|
||||
+#[AAttribute]
|
||||
#[BAttribute]
|
||||
-#[AAttribute]
|
||||
class Someclass
|
||||
{
|
||||
}
|
||||
|
|
|
@ -456,6 +456,7 @@ parameters:
|
|||
- src/*/*Processor.php
|
||||
- rules/Composer/Application/FileProcessor/ComposerFileProcessor.php
|
||||
- src/Contract/Processor/FileProcessorInterface.php
|
||||
- packages/Parallel/Application/ParallelFileProcessor.php
|
||||
|
||||
- '#Call to function property_exists\(\) with PhpParser\\Node\\Stmt\\ClassLike and (.*?) will always evaluate to true#'
|
||||
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\If_\SimplifyIfExactValueReturnValueRector\Fixture;
|
||||
|
||||
final class IdentialValueCompare
|
||||
{
|
||||
public function run($value)
|
||||
{
|
||||
if ($value === 93) {
|
||||
return 93;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\If_\SimplifyIfExactValueReturnValueRector\Fixture;
|
||||
|
||||
final class IdentialValueCompare
|
||||
{
|
||||
public function run($value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\If_\SimplifyIfExactValueReturnValueRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class SimplifyIfExactValueReturnValueRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\CodeQuality\Rector\If_\SimplifyIfExactValueReturnValueRector;
|
||||
use Rector\Config\RectorConfig;
|
||||
|
||||
return static function (RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->rule(SimplifyIfExactValueReturnValueRector::class);
|
||||
};
|
|
@ -8,12 +8,6 @@ function simplifyIfNotNullReturnRector()
|
|||
}
|
||||
|
||||
return null;
|
||||
|
||||
if (null !== $newNode) {
|
||||
return $newNode;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -24,7 +18,6 @@ function simplifyIfNotNullReturnRector()
|
|||
{
|
||||
$newNode = 'something';
|
||||
return $newNode;
|
||||
return $newNode;
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
<?php
|
||||
|
||||
function simplifyIfNotNullReturnRector2()
|
||||
{
|
||||
$newNode = 'something';
|
||||
if ($newNode === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $newNode;
|
||||
|
||||
|
||||
if (null === $newNode) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $newNode;
|
||||
|
||||
if (93 === $newNode) {
|
||||
return 93;
|
||||
}
|
||||
|
||||
return $newNode;
|
||||
|
||||
if (93 === $newNode) {
|
||||
return 94;
|
||||
}
|
||||
|
||||
return $newNode;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
function simplifyIfNotNullReturnRector2()
|
||||
{
|
||||
$newNode = 'something';
|
||||
|
||||
return $newNode;
|
||||
|
||||
return $newNode;
|
||||
|
||||
return $newNode;
|
||||
|
||||
if (93 === $newNode) {
|
||||
return 94;
|
||||
}
|
||||
|
||||
return $newNode;
|
||||
}
|
||||
|
||||
?>
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Rector\Tests\CodeQuality\Rector\If_\SimplifyIfNotNullReturnRector\Fixture;
|
||||
|
||||
class Skip
|
||||
final class Skip
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
|
@ -13,14 +13,4 @@ class Skip
|
|||
|
||||
return 5;
|
||||
}
|
||||
|
||||
public function runAgain()
|
||||
{
|
||||
$newNode = 'something';
|
||||
if ($newNode !== null) {
|
||||
return $newNode;
|
||||
}
|
||||
|
||||
return 'another';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\If_\SimplifyIfNotNullReturnRector\Fixture;
|
||||
|
||||
final class SkipNullableResult
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$newNode = $this->resolveMaybe();
|
||||
if ($newNode !== null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return 5;
|
||||
}
|
||||
|
||||
private function resolveMaybe(): ?int
|
||||
{
|
||||
if (mt_rand(0, 1)) {
|
||||
return 100;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\If_\SimplifyIfNotNullReturnRector\Fixture;
|
||||
|
||||
final class SkipWithEmptyIf
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$newNode = 'something';
|
||||
if ($newNode !== null) {
|
||||
}
|
||||
|
||||
return 5;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Rector\If_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\NodeManipulator\IfManipulator;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\CodeQuality\Rector\If_\SimplifyIfNullableReturnRector\SimplifyIfNullableReturnRectorTest
|
||||
*/
|
||||
final class SimplifyIfExactValueReturnValueRector extends AbstractRector
|
||||
{
|
||||
public function __construct(
|
||||
private readonly IfManipulator $ifManipulator
|
||||
) {
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Changes compared to value and return of expr to direct return',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
$value = 'something';
|
||||
if ($value === 52) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
$value = 'something';
|
||||
return $value;
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [If_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param If_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Return_
|
||||
{
|
||||
$comparedNode = $this->ifManipulator->matchIfValueReturnValue($node);
|
||||
if ($comparedNode !== null) {
|
||||
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
|
||||
if (! $nextNode instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->nodeComparator->areNodesEqual($comparedNode, $nextNode->expr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->removeNode($nextNode);
|
||||
return clone $nextNode;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Rector\CodeQuality\Rector\If_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\NodeManipulator\IfManipulator;
|
||||
|
@ -58,7 +59,7 @@ CODE_SAMPLE
|
|||
/**
|
||||
* @param If_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
public function refactor(Node $node): ?Stmt
|
||||
{
|
||||
$comparedNode = $this->ifManipulator->matchIfNotNullReturnValue($node);
|
||||
if ($comparedNode !== null) {
|
||||
|
@ -81,21 +82,6 @@ CODE_SAMPLE
|
|||
return $insideIfNode;
|
||||
}
|
||||
|
||||
$comparedNode = $this->ifManipulator->matchIfValueReturnValue($node);
|
||||
if ($comparedNode !== null) {
|
||||
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
|
||||
if (! $nextNode instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->nodeComparator->areNodesEqual($comparedNode, $nextNode->expr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->removeNode($nextNode);
|
||||
return clone $nextNode;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user