Skip to content

Commit

Permalink
[DeadCode] Add RemoveJustPropertyFetchRector (#2433)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 4, 2022
1 parent 70261b7 commit b0a6173
Show file tree
Hide file tree
Showing 27 changed files with 525 additions and 38 deletions.
3 changes: 1 addition & 2 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,12 @@ public function getParamTypesByName(): array
$paramTypesByName = [];

foreach ($this->phpDocNode->getParamTagValues() as $paramTagValueNode) {
$parameterName = $paramTagValueNode->parameterName;
$parameterType = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType(
$paramTagValueNode,
$this->node
);

$paramTypesByName[$parameterName] = $parameterType;
$paramTypesByName[$paramTagValueNode->parameterName] = $parameterType;
}

return $paramTypesByName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ private function setPositionOfLastToken(PhpDocNode $phpDocNode): void

$phpDocChildNodes = $phpDocNode->children;
$phpDocChildNode = array_pop($phpDocChildNodes);

$startAndEnd = $phpDocChildNode->getAttribute(PhpDocAttributeKey::START_AND_END);

if ($startAndEnd instanceof StartAndEnd) {
Expand Down
2 changes: 1 addition & 1 deletion packages/NodeCollector/BinaryOpConditionsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function findConditions(Expr $expr, string $binaryOpClass): array
$conditions[] = $expr->right;
$expr = $expr->left;

if ($expr::class !== $binaryOpClass) {
if ($binaryOpClass !== $expr::class) {
$conditions[] = $expr;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public function __construct(
*/
public function match(Array_ $array): null | ArrayCallableDynamicMethod | ArrayCallable
{
$arrayItems = $array->items;
if (count($arrayItems) !== 2) {
if (count($array->items) !== 2) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ function (DocNode $docNode) use ($oldToNewNamespaces): ?DocNode {
return null;
}

$name = $docNode->name;
$trimmedName = ltrim($docNode->name, '\\');

if ($name === $trimmedName) {
if ($docNode->name === $trimmedName) {
return null;
}

Expand Down
7 changes: 4 additions & 3 deletions packages/PhpAttribute/AttributeArrayNameInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ private function inlineArrayNode(Array_ $array): array
continue;
}

$key = $arrayItem->key;
if ($key instanceof String_) {
$args[] = new Arg($arrayItem->value, false, false, [], new Identifier($key->value));
if ($arrayItem->key instanceof String_) {
$string = $arrayItem->key;
$argumentName = new Identifier($string->value);
$args[] = new Arg($arrayItem->value, false, false, [], $argumentName);
} else {
$args[] = new Arg($arrayItem->value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public function mapToPHPStanType(TypeNode $typeNode, Node $node, NameScope $name
}

if (str_starts_with($typeNode->name, '\\')) {
$type = $typeNode->name;
$typeWithoutPreslash = Strings::substring($type, 1);
$typeWithoutPreslash = Strings::substring($typeNode->name, 1);
$objectType = new FullyQualifiedObjectType($typeWithoutPreslash);
} else {
if ($typeNode->name === 'scalar') {
Expand Down
5 changes: 2 additions & 3 deletions packages/StaticTypeMapper/PhpDocParser/NullableTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ public function getNodeType(): string
*/
public function mapToPHPStanType(TypeNode $typeNode, Node $node, NameScope $nameScope): Type
{
$type = $typeNode->type;
if ($type instanceof IdentifierTypeNode) {
$type = $this->identifierTypeMapper->mapToPHPStanType($type, $node, $nameScope);
if ($typeNode->type instanceof IdentifierTypeNode) {
$type = $this->identifierTypeMapper->mapToPHPStanType($typeNode->type, $node, $nameScope);
return new UnionType([new NullType(), $type]);
}

Expand Down
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;
}
}
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;
}
}
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;
}
}
}
}
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;
}
}
}
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);
}
}
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);
}
}
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;
}
}

?>
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();
}
}

?>
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';
}
}
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()
{
}
}
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);
};

0 comments on commit b0a6173

Please sign in to comment.