From ced5f813aada552da23c7567b1a9cdec31dc2de8 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 30 Oct 2019 23:12:47 +0100 Subject: [PATCH 1/3] add dead-code set to CI, make NameResolver exception on method call more verbose --- .../Rector/Stmt/RemoveUnreachableStatementRector.php | 1 + .../Fixture/skip_already_array.php.inc | 2 +- .../{ => Source}/ClassWithMethodOfTwoArguments.php | 3 ++- rector-ci.yaml | 1 + src/PhpParser/Node/Manipulator/ClassManipulator.php | 5 +++++ src/PhpParser/Node/Resolver/NameResolver.php | 11 ++++++++++- 6 files changed, 20 insertions(+), 3 deletions(-) rename packages/PHPUnit/tests/Rector/MethodCall/WithConsecutiveArgToArrayRector/{ => Source}/ClassWithMethodOfTwoArguments.php (79%) diff --git a/packages/DeadCode/src/Rector/Stmt/RemoveUnreachableStatementRector.php b/packages/DeadCode/src/Rector/Stmt/RemoveUnreachableStatementRector.php index b0fcbae6202e..82fdf2b012b8 100644 --- a/packages/DeadCode/src/Rector/Stmt/RemoveUnreachableStatementRector.php +++ b/packages/DeadCode/src/Rector/Stmt/RemoveUnreachableStatementRector.php @@ -144,6 +144,7 @@ private function isBreakingScopeNode(Node $node): bool if ($node instanceof Namespace_) { return true; } + return $node instanceof Else_; } } diff --git a/packages/PHPUnit/tests/Rector/MethodCall/WithConsecutiveArgToArrayRector/Fixture/skip_already_array.php.inc b/packages/PHPUnit/tests/Rector/MethodCall/WithConsecutiveArgToArrayRector/Fixture/skip_already_array.php.inc index cca5f83f6e43..9fcc73dd5abd 100644 --- a/packages/PHPUnit/tests/Rector/MethodCall/WithConsecutiveArgToArrayRector/Fixture/skip_already_array.php.inc +++ b/packages/PHPUnit/tests/Rector/MethodCall/WithConsecutiveArgToArrayRector/Fixture/skip_already_array.php.inc @@ -3,7 +3,7 @@ namespace Rector\PHPUnit\Tests\Rector\MethodCall\WithConsecutiveArgToArrayRector\Fixture; use PHPUnit\Framework\TestCase; -use Rector\PHPUnit\Tests\Rector\MethodCall\WithConsecutiveArgToArrayRector\ClassWithMethodOfTwoArguments; +use Rector\PHPUnit\Tests\Rector\MethodCall\WithConsecutiveArgToArrayRector\Source\ClassWithMethodOfTwoArguments; class SkipAlreadyArrayTestCase extends TestCase { diff --git a/packages/PHPUnit/tests/Rector/MethodCall/WithConsecutiveArgToArrayRector/ClassWithMethodOfTwoArguments.php b/packages/PHPUnit/tests/Rector/MethodCall/WithConsecutiveArgToArrayRector/Source/ClassWithMethodOfTwoArguments.php similarity index 79% rename from packages/PHPUnit/tests/Rector/MethodCall/WithConsecutiveArgToArrayRector/ClassWithMethodOfTwoArguments.php rename to packages/PHPUnit/tests/Rector/MethodCall/WithConsecutiveArgToArrayRector/Source/ClassWithMethodOfTwoArguments.php index a13786063d58..083d62511856 100644 --- a/packages/PHPUnit/tests/Rector/MethodCall/WithConsecutiveArgToArrayRector/ClassWithMethodOfTwoArguments.php +++ b/packages/PHPUnit/tests/Rector/MethodCall/WithConsecutiveArgToArrayRector/Source/ClassWithMethodOfTwoArguments.php @@ -2,11 +2,12 @@ declare(strict_types=1); -namespace Rector\PHPUnit\Tests\Rector\MethodCall\WithConsecutiveArgToArrayRector; +namespace Rector\PHPUnit\Tests\Rector\MethodCall\WithConsecutiveArgToArrayRector\Source; final class ClassWithMethodOfTwoArguments { public function go(int $one, string $two): void { + $three = $one + $two; } } diff --git a/rector-ci.yaml b/rector-ci.yaml index 98bf716d7459..cdada396d1b4 100644 --- a/rector-ci.yaml +++ b/rector-ci.yaml @@ -1,6 +1,7 @@ parameters: sets: - "code-quality" + - "dead-code" auto_import_names: true diff --git a/src/PhpParser/Node/Manipulator/ClassManipulator.php b/src/PhpParser/Node/Manipulator/ClassManipulator.php index 86752a6ef9b9..6b043b776e80 100644 --- a/src/PhpParser/Node/Manipulator/ClassManipulator.php +++ b/src/PhpParser/Node/Manipulator/ClassManipulator.php @@ -9,6 +9,7 @@ use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\StaticPropertyFetch; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; use PhpParser\Node\Param; use PhpParser\Node\Stmt; @@ -487,6 +488,10 @@ private function getClassMethodNames(Class_ $classNode): array private function isNonAssignPropertyFetch(Node $node): bool { if ($node instanceof PropertyFetch) { + if (! $node->var instanceof Variable) { + return false; + } + if (! $this->nameResolver->isName($node->var, 'this')) { return false; } diff --git a/src/PhpParser/Node/Resolver/NameResolver.php b/src/PhpParser/Node/Resolver/NameResolver.php index 3a5d0f49f4cf..f9aec7f462b9 100644 --- a/src/PhpParser/Node/Resolver/NameResolver.php +++ b/src/PhpParser/Node/Resolver/NameResolver.php @@ -24,6 +24,7 @@ use PhpParser\Node\Stmt\Use_; use Rector\Exception\ShouldNotHappenException; use Rector\NodeTypeResolver\Node\AttributeKey; +use Symplify\PackageBuilder\FileSystem\SmartFileInfo; final class NameResolver { @@ -44,8 +45,16 @@ public function isNames(Node $node, array $names): bool public function isName(Node $node, string $name): bool { if ($node instanceof MethodCall) { + $debugBacktrace = debug_backtrace(); + + $previousCaller = $debugBacktrace[0]; + $fileInfo = new SmartFileInfo($previousCaller['file']); + $location = $fileInfo->getRelativeFilePathFromDirectory(getcwd()) . ':' . $previousCaller['line']; + throw new ShouldNotHappenException(sprintf( - 'Cannot get name on "%s" node. Use $node->name instead', MethodCall::class + 'Cannot get name on "%s" node. Use $node->name instead. Called in: %s', + MethodCall::class, + $location )); } From 099fdcc05ba908dda02e9acd11aafaa5e5900f19 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 30 Oct 2019 23:33:10 +0100 Subject: [PATCH 2/3] fix phpstan false positive --- .../src/Rector/Stmt/RemoveUnreachableStatementRector.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/DeadCode/src/Rector/Stmt/RemoveUnreachableStatementRector.php b/packages/DeadCode/src/Rector/Stmt/RemoveUnreachableStatementRector.php index 82fdf2b012b8..bee84a43abad 100644 --- a/packages/DeadCode/src/Rector/Stmt/RemoveUnreachableStatementRector.php +++ b/packages/DeadCode/src/Rector/Stmt/RemoveUnreachableStatementRector.php @@ -13,6 +13,7 @@ use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\Nop; +use PhpParser\Node\Stmt\While_; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; @@ -82,6 +83,11 @@ public function refactor(Node $node): ?Node return null; } + if ($previousNode instanceof While_) { + $node->setAttribute(AttributeKey::IS_UNREACHABLE, false); + return null; + } + if ($this->isAfterMarkTestSkippedMethodCall($node)) { $node->setAttribute(AttributeKey::IS_UNREACHABLE, false); return null; From 46948a4fe7594fb52e1955ca9c4c6c4ae2e64d84 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 31 Oct 2019 00:16:41 +0100 Subject: [PATCH 3/3] fix xdebug option --- src/Console/Application.php | 9 +++++++++ src/Console/Command/AbstractCommand.php | 8 -------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Console/Application.php b/src/Console/Application.php index cc3666e13770..d4ae3a054d5a 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -4,6 +4,7 @@ namespace Rector\Console; +use Composer\XdebugHandler\XdebugHandler; use Jean85\PrettyVersions; use Rector\Configuration\Configuration; use Rector\Console\Output\JsonOutputFormatter; @@ -55,6 +56,14 @@ public function setDispatcher(EventDispatcherInterface $eventDispatcher): void public function doRun(InputInterface $input, OutputInterface $output): int { + // @fixes https://github.com/rectorphp/rector/issues/2205 + $isXdebugAllowed = $input->hasParameterOption('--xdebug'); + if (!$isXdebugAllowed) { + $xdebug = new XdebugHandler('rector', '--ansi'); + $xdebug->check(); + unset($xdebug); + } + $this->configuration->setConfigFilePathFromInput($input); $shouldFollowByNewline = false; diff --git a/src/Console/Command/AbstractCommand.php b/src/Console/Command/AbstractCommand.php index fd9f439b5f24..bf4387f56dbf 100644 --- a/src/Console/Command/AbstractCommand.php +++ b/src/Console/Command/AbstractCommand.php @@ -4,7 +4,6 @@ namespace Rector\Console\Command; -use Composer\XdebugHandler\XdebugHandler; use Nette\Utils\Strings; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Descriptor\TextDescriptor; @@ -61,12 +60,5 @@ protected function initialize(InputInterface $input, OutputInterface $output): v $this->getApplication()->setCatchExceptions(false); } - - // @fixes https://github.com/rectorphp/rector/issues/2205 - if ($input->getOption('xdebug')) { - $xdebug = new XdebugHandler('rector', '--ansi'); - $xdebug->check(); - unset($xdebug); - } } }