-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DeadCode] Add RemoveJustPropertyFetchRector (#2433)
- Loading branch information
1 parent
70261b7
commit b0a6173
Showing
27 changed files
with
525 additions
and
38 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
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
17 changes: 17 additions & 0 deletions
17
...StmtsAwareInterface/RemoveJustPropertyFetchRector/Fixture/skip_conditional_assign.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,17 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture; | ||
|
||
final class SkipConditionalAssign | ||
{ | ||
private $id; | ||
|
||
public function run(?int $id) | ||
{ | ||
if ($id === null) { | ||
$id = $this->id; | ||
} | ||
|
||
return $id; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...tsAwareInterface/RemoveJustPropertyFetchRector/Fixture/skip_double_property_fetch.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,23 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture; | ||
|
||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; | ||
|
||
final class SkipDoublePropertyFetch | ||
{ | ||
public function run(PhpDocTagNode $phpDocTagNode) | ||
{ | ||
if (! $phpDocTagNode->value instanceof GenericTagValueNode) { | ||
return null; | ||
} | ||
|
||
$description = $phpDocTagNode->value->value; | ||
if ($description === '') { | ||
return 'no'; | ||
} | ||
|
||
return $description; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...or/StmtsAwareInterface/RemoveJustPropertyFetchRector/Fixture/skip_in_while_assign.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,24 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\BinaryOp; | ||
|
||
final class SkipInWhileAssign | ||
{ | ||
public function run(Expr $expr, string $binaryOpClass) | ||
{ | ||
$conditions = []; | ||
|
||
while ($expr instanceof BinaryOp) { | ||
$conditions[] = $expr->right; | ||
$expr = $expr->left; | ||
|
||
if ($binaryOpClass !== $expr::class) { | ||
$conditions[] = $expr; | ||
break; | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...tsAwareInterface/RemoveJustPropertyFetchRector/Fixture/skip_nested_property_fetch.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,17 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture; | ||
|
||
use PhpParser\Node\Expr\Assign; | ||
|
||
final class SkipNestedPropertyFetch | ||
{ | ||
public function run(Assign $assign) | ||
{ | ||
if ($assign->var instanceof Assign) { | ||
$nestedAssign = $assign->var; | ||
|
||
echo $nestedAssign->expr; | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...tor/StmtsAwareInterface/RemoveJustPropertyFetchRector/Fixture/skip_property_write.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,14 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture; | ||
|
||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; | ||
|
||
final class SkipPropertyWrite | ||
{ | ||
public function run(PhpDocNode $phpDocNode) | ||
{ | ||
$phpDocChildNodes = $phpDocNode->children; | ||
$phpDocChildNode = array_pop($phpDocChildNodes); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...mtsAwareInterface/RemoveJustPropertyFetchRector/Fixture/skip_var_annotated_assign.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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture; | ||
|
||
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SkipVarAnnotatedAssign extends TestCase | ||
{ | ||
public function go() | ||
{ | ||
/** @var PhpDocTagNode $childNode */ | ||
$propertyTagValueNode = $childNode->value; | ||
$this->assertInstanceOf(ParamTagValueNode::class, $propertyTagValueNode); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...dCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchRector/Fixture/some_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,33 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
private $name; | ||
|
||
public function run() | ||
{ | ||
$name = $this->name; | ||
|
||
return $name; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
private $name; | ||
|
||
public function run() | ||
{ | ||
return $this->name; | ||
} | ||
} | ||
|
||
?> |
47 changes: 47 additions & 0 deletions
47
...StmtsAwareInterface/RemoveJustPropertyFetchRector/Fixture/with_simple_method_call.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,47 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture; | ||
|
||
use Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Source\AnotherCaller; | ||
|
||
final class WithSimleMethodCall | ||
{ | ||
private AnotherCaller $anotherCaller; | ||
|
||
public function __construct(AnotherCaller $anotherCaller) | ||
{ | ||
$this->anotherCaller = $anotherCaller; | ||
} | ||
|
||
public function run() | ||
{ | ||
$anotherCaller = $this->anotherCaller; | ||
|
||
$result = $anotherCaller->callMe(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture; | ||
|
||
use Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Source\AnotherCaller; | ||
|
||
final class WithSimleMethodCall | ||
{ | ||
private AnotherCaller $anotherCaller; | ||
|
||
public function __construct(AnotherCaller $anotherCaller) | ||
{ | ||
$this->anotherCaller = $anotherCaller; | ||
} | ||
|
||
public function run() | ||
{ | ||
$result = $this->anotherCaller->callMe(); | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...r/StmtsAwareInterface/RemoveJustPropertyFetchRector/RemoveJustPropertyFetchRectorTest.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class RemoveJustPropertyFetchRectorTest 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'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...eadCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchRector/Source/AnotherCaller.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Source; | ||
|
||
final class AnotherCaller | ||
{ | ||
public function callMe() | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...dCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchRector/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\StmtsAwareInterface\RemoveJustPropertyFetchRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RemoveJustPropertyFetchRector::class); | ||
}; |
Oops, something went wrong.