Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DeadCode] Add RemoveUselessReturnExprInConstructRector (#5158)
* [DeadCode] Add RemoveUselessReturnExprInConstruct * [ci-review] Rector Rectify * skip inner class test * [ci-review] Rector Rectify * fix phpstan * add Rector suffix * register * add fixture for return dynamic * update doc sample * fix --------- Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
444bc59
commit b7a5fef
Showing
14 changed files
with
358 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...RemoveUselessReturnExprInConstructRector/Fixture/return_dynamic_expr_in_construct.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector\Fixture; | ||
|
||
final class ReturnDynamicExprInConstruct | ||
{ | ||
public function __construct() | ||
{ | ||
if (rand(0, 1)) { | ||
return parent::__construct(); | ||
echo 'dead code, ensure stmt inserted between'; | ||
} | ||
|
||
$this->execute(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector\Fixture; | ||
|
||
final class ReturnDynamicExprInConstruct | ||
{ | ||
public function __construct() | ||
{ | ||
if (rand(0, 1)) { | ||
parent::__construct(); | ||
return; | ||
echo 'dead code, ensure stmt inserted between'; | ||
} | ||
|
||
$this->execute(); | ||
} | ||
} | ||
|
||
?> |
37 changes: 37 additions & 0 deletions
37
...sMethod/RemoveUselessReturnExprInConstructRector/Fixture/return_expr_in_construct.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector\Fixture; | ||
|
||
final class ReturnExprInConstruct | ||
{ | ||
public function __construct() | ||
{ | ||
if (rand(0, 1)) { | ||
$this->init(); | ||
return true; | ||
} | ||
|
||
$this->execute(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector\Fixture; | ||
|
||
final class ReturnExprInConstruct | ||
{ | ||
public function __construct() | ||
{ | ||
if (rand(0, 1)) { | ||
$this->init(); | ||
return; | ||
} | ||
|
||
$this->execute(); | ||
} | ||
} | ||
|
||
?> |
8 changes: 8 additions & 0 deletions
8
...ClassMethod/RemoveUselessReturnExprInConstructRector/Fixture/skip_abstract_method.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector\Fixture; | ||
|
||
abstract class SkipAbstractMethod | ||
{ | ||
abstract public function run(); | ||
} |
16 changes: 16 additions & 0 deletions
16
...tor/ClassMethod/RemoveUselessReturnExprInConstructRector/Fixture/skip_inner_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector\Fixture; | ||
|
||
final class SkipInnerClass | ||
{ | ||
public function __construct() | ||
{ | ||
$obj = new class { | ||
public function run() | ||
{ | ||
return true; | ||
} | ||
}; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...r/ClassMethod/RemoveUselessReturnExprInConstructRector/Fixture/skip_not_construct.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector\Fixture; | ||
|
||
final class SkipNotConstruct | ||
{ | ||
public function run() | ||
{ | ||
return true; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
.../ClassMethod/RemoveUselessReturnExprInConstructRector/Fixture/skip_return_no_expr.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector\Fixture; | ||
|
||
final class SkipReturnNoExpr | ||
{ | ||
public function __construct() | ||
{ | ||
if (rand(0, 1)) { | ||
$this->init(); | ||
return; | ||
} | ||
|
||
$this->execute(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...RemoveUselessReturnExprInConstructRector/RemoveUselessReturnExprInConstructRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RemoveUselessReturnExprInConstructRectorTest 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'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...de/Rector/ClassMethod/RemoveUselessReturnExprInConstructRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RemoveUselessReturnExprInConstructRector::class); | ||
}; |
Oops, something went wrong.