Skip to content

Commit

Permalink
Apply RemoveJustPropertyFetchRector and improve skipping for nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 4, 2022
1 parent b0a6173 commit c1f36a4
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public function removeByName(PhpDocInfo $phpDocInfo, string $name): void
}

if ($phpDocChildNode->value instanceof DoctrineAnnotationTagValueNode) {
$doctrineAnnotationTagValueNode = $phpDocChildNode->value;

if ($doctrineAnnotationTagValueNode->hasClassName($name)) {
if ($phpDocChildNode->value->hasClassName($name)) {
unset($phpDocNode->children[$key]);
$phpDocInfo->markAsChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,13 @@ private function resolveAsAliased(array $uses, string $tag): string
$prefix = $use instanceof GroupUse
? $use->prefix . '\\'
: '';
$useUses = $use->uses;

foreach ($useUses as $useUse) {
foreach ($use->uses as $useUse) {
if (! $useUse->alias instanceof Identifier) {
continue;
}

$alias = $useUse->alias;
if ($alias->toString() === $tag) {
if ($useUse->alias->toString() === $tag) {
return $prefix . $useUse->name->toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,10 @@ private function createSpacelessPhpDocTagNode(
GenericTagValueNode $genericTagValueNode,
string $fullyQualifiedAnnotationClass
): SpacelessPhpDocTagNode {
$annotationContent = $genericTagValueNode->value;
$formerStartEnd = $genericTagValueNode->getAttribute(PhpDocAttributeKey::START_AND_END);

return $this->createDoctrineSpacelessPhpDocTagNode(
$annotationContent,
$genericTagValueNode->value,
$tagName,
$fullyQualifiedAnnotationClass,
$formerStartEnd
Expand Down
6 changes: 2 additions & 4 deletions packages/FamilyTree/Reflection/FamilyRelationsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ public function getClassLikeAncestorNames(Class_ | Interface_ | Name $classOrNam

if ($classLike instanceof Class_) {
if ($classLike->extends instanceof Name) {
$extendName = $classLike->extends;

$ancestorNames[] = $this->nodeNameResolver->getName($extendName);
$ancestorNames = array_merge($ancestorNames, $this->getClassLikeAncestorNames($extendName));
$ancestorNames[] = $this->nodeNameResolver->getName($classLike->extends);
$ancestorNames = array_merge($ancestorNames, $this->getClassLikeAncestorNames($classLike->extends));
}

foreach ($classLike->implements as $implement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public function resolve(Node $node): ?string
return null;
}

$functionName = $node->name;

$namespaceName = $functionName->getAttribute(AttributeKey::NAMESPACED_NAME);
$namespaceName = $node->name->getAttribute(AttributeKey::NAMESPACED_NAME);
if ($namespaceName instanceof FullyQualified) {
$functionFqnName = $namespaceName->toString();

Expand All @@ -50,6 +48,6 @@ public function resolve(Node $node): ?string
}
}

return (string) $functionName;
return (string) $node->name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ private function removeUnwrappedItems(string $attributeClass, array $items): arr
continue;
}

$arrayItemKey = $item->key;
if (! $arrayItemKey instanceof String_) {
if (! $item->key instanceof String_) {
continue;
}

if (! in_array($arrayItemKey->value, $unwrappeColumns, true)) {
$stringItemKey = $item->key;
if (! in_array($stringItemKey->value, $unwrappeColumns, true)) {
continue;
}

Expand Down
3 changes: 3 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector;
use Rector\CodingStyle\ValueObject\ReturnArrayClassMethodToYield;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector;
use Rector\Nette\Set\NetteSetList;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector;
Expand Down Expand Up @@ -43,6 +44,8 @@
new ReturnArrayClassMethodToYield('PHPUnit\Framework\TestCase', '*provide*'),
]);

// $rectorConfig->rule(RemoveJustPropertyFetchRector::class);

$rectorConfig->paths([
__DIR__ . '/bin',
__DIR__ . '/src',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture;

use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;

final class SkipDimFetchAssign
{
private $items;

public function run(Array_ $array)
{
/** @var ArrayItem[] $items */
$items = $array->items;

// $this, self, static, FQN
$firstItemValue = $items[0]->value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture;

use PhpParser\Node\Scalar\String_;

final class SkipOnNodesAsMostlyUsed
{
public function run(String_ $string)
{
$name = $string->value;

return $name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\While_;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\Type\ObjectType;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\ValueObject\PropertyFetchToVariableAssign;
Expand Down Expand Up @@ -206,6 +207,10 @@ private function matchVariableToPropertyAssign(Stmt $stmt): ?PropertyFetchToVari
return null;
}

if ($this->isPropertyFetchCallerNode($assign->expr)) {
return null;
}

// keep property fetch nesting
if ($assign->expr->var instanceof PropertyFetch) {
return null;
Expand All @@ -217,4 +222,18 @@ private function matchVariableToPropertyAssign(Stmt $stmt): ?PropertyFetchToVari

return new PropertyFetchToVariableAssign($assign->var, $assign->expr);
}

private function isPropertyFetchCallerNode(PropertyFetch $propertyFetch): bool
{
// skip nodes as mostly used with public property fetches
$propertyFetchCallerType = $this->getType($propertyFetch->var);

if (! $propertyFetchCallerType instanceof ObjectType) {
return false;
}

$nodeObjectType = new ObjectType('PhpParser\Node');
return $nodeObjectType->isSuperTypeOf($propertyFetchCallerType)
->yes();
}
}
4 changes: 2 additions & 2 deletions rules/Naming/Naming/AliasNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function resolveByName(Name $name): ?string
$prefix = $use instanceof GroupUse
? $use->prefix . '\\'
: '';
$useUses = $use->uses;
foreach ($useUses as $useUse) {

foreach ($use->uses as $useUse) {
if (! $useUse->alias instanceof Identifier) {
continue;
}
Expand Down
3 changes: 1 addition & 2 deletions src/NodeManipulator/ArrayManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public function isDynamicArray(Array_ $array): bool
return true;
}

$value = $item->value;
if (! $this->isAllowedArrayValue($value)) {
if (! $this->isAllowedArrayValue($item->value)) {
return true;
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/NodeManipulator/IfManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ public function matchIfNotNullReturnValue(If_ $if): ?Expr
*/
public function matchIfValueReturnValue(If_ $if): ?Expr
{
$stmts = $if->stmts;

if (count($stmts) !== 1) {
if (count($if->stmts) !== 1) {
return null;
}

$insideIfStmt = $stmts[0];
$insideIfStmt = $if->stmts[0];
if (! $insideIfStmt instanceof Return_) {
return null;
}
Expand Down

0 comments on commit c1f36a4

Please sign in to comment.