Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
patreon: rectorphp
open_collective: rectorphp
github: tomasvotruba
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -64,13 +64,13 @@ public function refactor(Node $node): ?Node
return null;
}

/** @var ClassLike|null $classNode */
/** @var Class_|Interface_|Trait_|null $classNode */
$classNode = $node->getAttribute(AttributeKey::CLASS_NODE);
if ($classNode === null || $classNode instanceof Trait_) {
if ($classNode === null || $classNode instanceof Trait_ || $classNode instanceof Interface_) {
return null;
}

if ($classNode instanceof Class_ && $classNode->isAnonymous()) {
if ($classNode->isAnonymous()) {
return null;
}

Expand All @@ -86,7 +86,6 @@ public function refactor(Node $node): ?Node
}

$uselessAssigns = $this->resolveUselessAssignNode($propertyFetches);

if (count($uselessAssigns) > 0) {
$this->removeNode($node);
foreach ($uselessAssigns as $uselessAssign) {
Expand All @@ -98,6 +97,9 @@ public function refactor(Node $node): ?Node
}

/**
* Matches all-only: "$this->property = x"
* If these is ANY OTHER use of property, e.g. process($this->property), it returns []
*
* @param PropertyFetch[] $propertyFetches
* @return Assign[]
*/
Expand All @@ -107,9 +109,11 @@ private function resolveUselessAssignNode(array $propertyFetches): array

foreach ($propertyFetches as $propertyFetch) {
$propertyFetchParentNode = $propertyFetch->getAttribute(AttributeKey::PARENT_NODE);

if ($propertyFetchParentNode instanceof Assign && $propertyFetchParentNode->var === $propertyFetch) {
$uselessAssigns[] = $propertyFetchParentNode;
} else {
// it is used another way as well → nothing to remove
return [];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

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

use PhpParser\Node;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;

class SkipAnonymousFunction
{
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

namespace Tests\Unit\Exceptions;

use stdClass;

class SkipNestedClosure
{
private $mailer;
private $message;

public function setUp()
{
$this->message = new stdClass();
$this->mailer = new stdClass();
}

public function test()
{
$this->mailer->method()
->with(
$this->anything(),
$this->callback(
function () {
$this->message
->expects($this->once())
->method()
->with($this->a(), $this->b());

$this->call();

return true;
}
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public function test(): void
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/property_assign.php.inc',
__DIR__ . '/Fixture/with_trait.php.inc',
__DIR__ . '/Fixture/skip_anonymous_class.php.inc',
__DIR__ . '/Fixture/skip_anonymous_function.php.inc',
__DIR__ . '/Fixture/with_trait.php.inc',
__DIR__ . '/Fixture/skip_nested_closure.php.inc',
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
final class RemoveDeepChainMethodCallNodeVisitor extends NodeVisitorAbstract
{
/**
* @warning might cause bugs with fluent interfaces like https://github.com/rectorphp/rector/issues/1646
* @var int
*/
private const NESTED_CHAIN_METHOD_CALL_LIMIT = 10;
private const NESTED_CHAIN_METHOD_CALL_LIMIT = 15;

/**
* @var BetterNodeFinder
Expand Down
2 changes: 1 addition & 1 deletion rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ parameters:
php_version_features: '7.1'

services:
Rector\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector: ~
# Rector\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector: ~
8 changes: 4 additions & 4 deletions src/PhpParser/Node/Manipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public function getAllPropertyFetch(Property $property): array
$nodesToSearch[] = $classNode;

return $this->betterNodeFinder->find($nodesToSearch, function (Node $node) use ($property) {
// itself
if ($this->betterStandardPrinter->areNodesEqual($node, $property)) {
// property + static fetch
if (! $node instanceof PropertyFetch && ! $node instanceof StaticPropertyFetch) {
return null;
}

// property + static fetch
if (! $node instanceof PropertyFetch && ! $node instanceof StaticPropertyFetch) {
// itself
if ($this->betterStandardPrinter->areNodesEqual($node, $property)) {
return null;
}

Expand Down