Skip to content

Commit

Permalink
[DeadCode] Make private property removal dependent on local property …
Browse files Browse the repository at this point in the history
…fetches (#311)
  • Loading branch information
TomasVotruba committed Jun 27, 2021
1 parent 46843ec commit e3727aa
Show file tree
Hide file tree
Showing 27 changed files with 61 additions and 712 deletions.
2 changes: 0 additions & 2 deletions config/set/privatization.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Rector\Privatization\Rector\MethodCall\PrivatizeLocalGetterToPropertyRector;
use Rector\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector;
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
use Rector\Privatization\Rector\Property\PrivatizeLocalPropertyToPrivatePropertyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
Expand All @@ -22,7 +21,6 @@
$services->set(ChangeReadOnlyVariableWithDefaultValueToConstantRector::class);
$services->set(RepeatedLiteralToClassConstantRector::class);
$services->set(PrivatizeLocalGetterToPropertyRector::class);
$services->set(PrivatizeLocalPropertyToPrivatePropertyRector::class);
$services->set(PrivatizeFinalClassPropertyRector::class);
$services->set(PrivatizeFinalClassMethodRector::class);

Expand Down
35 changes: 1 addition & 34 deletions packages/NodeCollector/NodeCollector/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,13 @@
final class NodeRepository
{
public function __construct(
private ParsedPropertyFetchNodeCollector $parsedPropertyFetchNodeCollector,
private NodeNameResolver $nodeNameResolver,
private ParsedNodeCollector $parsedNodeCollector,
private ReflectionProvider $reflectionProvider,
private NodeTypeResolver $nodeTypeResolver
) {
}

/**
* @return PropertyFetch[]
*/
public function findPropertyFetchesByProperty(Property $property): array
{
/** @var string|null $className */
$className = $property->getAttribute(AttributeKey::CLASS_NAME);
if ($className === null) {
return [];
}

$propertyName = $this->nodeNameResolver->getName($property);
return $this->parsedPropertyFetchNodeCollector->findPropertyFetchesByTypeAndName($className, $propertyName);
}

/**
* @return PropertyFetch[]
*/
public function findPropertyFetchesByPropertyFetch(PropertyFetch $propertyFetch): array
{
$propertyFetcheeType = $this->nodeTypeResolver->getStaticType($propertyFetch->var);
if (! $propertyFetcheeType instanceof TypeWithClassName) {
return [];
}

$className = $this->nodeTypeResolver->getFullyQualifiedClassName($propertyFetcheeType);

/** @var string $propertyName */
$propertyName = $this->nodeNameResolver->getName($propertyFetch);

return $this->parsedPropertyFetchNodeCollector->findPropertyFetchesByTypeAndName($className, $propertyName);
}

public function hasClassChildren(Class_ $desiredClass): bool
{
$desiredClassName = $desiredClass->getAttribute(AttributeKey::CLASS_NAME);
Expand All @@ -90,6 +56,7 @@ public function hasClassChildren(Class_ $desiredClass): bool
}

/**
* @deprecated Use ReflectionProvider instead to resolve all the traits
* @return Trait_[]
*/
public function findUsedTraitsInClass(ClassLike $classLike): array
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use Rector\NodeCollector\NodeCollector\ParsedNodeCollector;
use Rector\NodeCollector\NodeCollector\ParsedPropertyFetchNodeCollector;

final class NodeCollectorNodeVisitor extends NodeVisitorAbstract
{
public function __construct(
private ParsedNodeCollector $parsedNodeCollector,
private ParsedPropertyFetchNodeCollector $parsedPropertyFetchNodeCollector
) {
}

Expand All @@ -23,8 +21,6 @@ public function enterNode(Node $node)
$this->parsedNodeCollector->collect($node);
}

$this->parsedPropertyFetchNodeCollector->collect($node);

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ public function __construct(
) {
}

/**
* @param PropertyFetch|StaticPropertyFetch $node
*/
public function isRead(Node $node): bool
public function isRead(PropertyFetch|StaticPropertyFetch $node): bool
{
Assert::isAnyOf($node, [PropertyFetch::class, StaticPropertyFetch::class]);

$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Node) {
throw new ShouldNotHappenException();
Expand Down
22 changes: 0 additions & 22 deletions packages/ReadWrite/NodeFinder/NodeUsageFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Foreach_;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeCollector\NodeCollector\NodeRepository;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeNestingScope\NodeFinder\ScopeAwareNodeFinder;

Expand All @@ -20,7 +18,6 @@ final class NodeUsageFinder
public function __construct(
private NodeNameResolver $nodeNameResolver,
private BetterNodeFinder $betterNodeFinder,
private NodeRepository $nodeRepository,
private ScopeAwareNodeFinder $scopeAwareNodeFinder,
private NodeComparator $nodeComparator
) {
Expand Down Expand Up @@ -50,25 +47,6 @@ public function findVariableUsages(array $nodes, Variable $variable): array
});
}

/**
* @return PropertyFetch[]
*/
public function findPropertyFetchUsages(PropertyFetch $desiredPropertyFetch): array
{
$propertyFetches = $this->nodeRepository->findPropertyFetchesByPropertyFetch($desiredPropertyFetch);

$propertyFetchesWithoutPropertyFetch = [];
foreach ($propertyFetches as $propertyFetch) {
if ($propertyFetch === $desiredPropertyFetch) {
continue;
}

$propertyFetchesWithoutPropertyFetch[] = $propertyFetch;
}

return $propertyFetchesWithoutPropertyFetch;
}

public function findPreviousForeachNodeUsage(Foreach_ $foreach, Expr $expr): ?Node
{
return $this->scopeAwareNodeFinder->findParent($foreach, function (Node $node) use ($expr): bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Rector\ReadWrite\ReadNodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\ReadWrite\Contract\ReadNodeAnalyzerInterface;

final class LocalPropertyFetchReadNodeAnalyzer implements ReadNodeAnalyzerInterface
{
public function __construct(
private JustReadExprAnalyzer $justReadExprAnalyzer,
private PropertyFetchFinder $propertyFetchFinder,
private NodeNameResolver $nodeNameResolver
) {
}

public function supports(Node $node): bool
{
return $node instanceof PropertyFetch;
}

/**
* @param PropertyFetch $node
*/
public function isRead(Node $node): bool
{
$class = $node->getAttribute(AttributeKey::CLASS_NODE);
if (! $class instanceof Class_) {
// assume worse to keep node protected
return true;
}

$propertyName = $this->nodeNameResolver->getName($node->name);
if ($propertyName === null) {
// assume worse to keep node protected
return true;
}

$propertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($class, $propertyName);
foreach ($propertyFetches as $propertyFetch) {
if ($this->justReadExprAnalyzer->isReadContext($propertyFetch)) {
return true;
}
}

return false;
}
}

This file was deleted.

3 changes: 0 additions & 3 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Rector\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector;
use Rector\PHPUnit\Rector\Class_\AddSeeTestAnnotationRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Privatization\Rector\Property\PrivatizeLocalPropertyToPrivatePropertyRector;
use Rector\Restoration\Rector\ClassMethod\InferParamFromClassMethodReturnRector;
use Rector\Restoration\ValueObject\InferParamFromClassMethodReturn;
use Rector\Set\ValueObject\SetList;
Expand Down Expand Up @@ -88,8 +87,6 @@
__DIR__ . '/rules/Php70/Rector/FuncCall/MultiDirnameRector.php',
],

PrivatizeLocalPropertyToPrivatePropertyRector::class => [__DIR__ . '/src/Rector/AbstractRector.php'],

ReturnTypeDeclarationRector::class => [
__DIR__ . '/packages/PHPStanStaticTypeMapper/TypeMapper/ArrayTypeMapper.php',
__DIR__ . '/packages/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php',
Expand Down

This file was deleted.

Loading

0 comments on commit e3727aa

Please sign in to comment.