Skip to content

Commit

Permalink
[DeadCode] Only remove private property from current class on RemoveU…
Browse files Browse the repository at this point in the history
…nusedPrivatePropertyRector (#917)

* added fixture for RemoveUnusedPrivatePropertyRector

* added fixture for RemoveUnusedPrivatePropertyRector

* Closes #915

* more fixture

* [ci-review] Rector Rectify

* phpstan

* [ci-review] Rector Rectify

Co-authored-by: Blazik <jakub.blaha@involve.cz>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Sep 24, 2021
1 parent 57a6d7a commit ddd8e85
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,11 @@ parameters:
- '#Register Rector\\Php71\\Rector\\Name\\ReservedObjectRector to php71 config set#'
- '#Register Rector\\Php80\\Rector\\Class_\\AnnotationToAttributeRector to php80 config set#'
- '#Register Rector\\Php80\\Rector\\Class_\\DoctrineAnnotationClassToAttributeRector to php80 config set#'

-
message: '#Class has a static method must so must contains "Static" in its name#'
paths:
- packages/FileFormatter/ValueObject/Indent.php #17
- packages/FileFormatter/ValueObject/NewLine.php #15
- src/Application/VersionResolver.php #16
- utils/compiler/src/Unprefixer.php #9
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;

class RemoveOnlyPrivateProperty
{
public \stdClass $dataProperty;

private string $unusedPropertyName;

public function updateData() {
$this->dataProperty->unusedPropertyName = 'some data';
}
}
?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;

class RemoveOnlyPrivateProperty
{
public \stdClass $dataProperty;

public function updateData() {
$this->dataProperty->unusedPropertyName = 'some data';
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;

class RemoveOnlyPrivateProperty2
{
public AnotherClass $dataProperty;

private static string $unusedPropertyName;

public function updateData() {
$this->dataProperty::$unusedPropertyName = 'some data';
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;

class RemoveOnlyPrivateProperty2
{
public AnotherClass $dataProperty;

public function updateData() {
$this->dataProperty::$unusedPropertyName = 'some data';
}
}

?>
19 changes: 14 additions & 5 deletions src/PhpParser/NodeFinder/PropertyFetchFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

final class PropertyFetchFinder
{
/**
* @var string
*/
private const THIS = 'this';

public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeNameResolver $nodeNameResolver,
Expand Down Expand Up @@ -75,7 +80,7 @@ public function findLocalPropertyFetchesByName(Class_ $class, string $paramName)
foreach ($propertyFetches as $propertyFetch) {
if ($propertyFetch instanceof PropertyFetch && ! $this->nodeNameResolver->isName(
$propertyFetch->var,
'this'
self::THIS
)) {
continue;
}
Expand Down Expand Up @@ -125,15 +130,19 @@ private function findPropertyFetchesInClassLike(array $nodes, string $propertyNa
/** @var PropertyFetch[]|StaticPropertyFetch[] $propertyFetches */
$propertyFetches = $this->betterNodeFinder->find($nodes, function (Node $node) use ($propertyName): bool {
// property + static fetch
if ($node instanceof PropertyFetch) {
if ($node instanceof PropertyFetch && $this->nodeNameResolver->isName($node->var, self::THIS)) {
return $this->nodeNameResolver->isName($node, $propertyName);
}

if ($node instanceof StaticPropertyFetch) {
return $this->nodeNameResolver->isName($node, $propertyName);
if (! $node instanceof StaticPropertyFetch) {
return false;
}

if (! $this->nodeNameResolver->isNames($node->class, ['self', self::THIS, 'static'])) {
return false;
}

return false;
return $this->nodeNameResolver->isName($node, $propertyName);
});

return $propertyFetches;
Expand Down

0 comments on commit ddd8e85

Please sign in to comment.