From 2ad011673f9189576f00dcedb3c155acd90e4fb8 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 30 Jul 2020 15:42:21 +0200 Subject: [PATCH] [PHP 7.0] Fix variable name on static call --- .../src/FileSystem/TemplateFileSystem.php | 15 +++++++++++---- .../StaticCallOnNonStaticToInstanceCallRector.php | 6 ++++++ .../PHPUnit/AbstractGenericRectorTestCase.php | 5 +++++ src/Testing/PHPUnit/AbstractRectorTestCase.php | 13 +++---------- .../src/Command/ValidateFixtureContentCommand.php | 7 ++----- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/packages/rector-generator/src/FileSystem/TemplateFileSystem.php b/packages/rector-generator/src/FileSystem/TemplateFileSystem.php index 29eb56fa7da..8d73100deec 100644 --- a/packages/rector-generator/src/FileSystem/TemplateFileSystem.php +++ b/packages/rector-generator/src/FileSystem/TemplateFileSystem.php @@ -34,10 +34,8 @@ final class TemplateFileSystem $destination = Strings::replace($destination, '#(__Configured|__Extra)#', ''); // remove ".inc" protection from PHPUnit if not a test case - if (! Strings::match($destination, '#/Fixture/#')) { - if (Strings::endsWith($destination, '.inc')) { - $destination = Strings::before($destination, '.inc'); - } + if ($this->isNonFixtureFileWithIncSuffix($destination)) { + $destination = Strings::before($destination, '.inc'); } return $targetDirectory . DIRECTORY_SEPARATOR . $destination; @@ -50,4 +48,13 @@ final class TemplateFileSystem { return str_replace(array_keys($variables), array_values($variables), $content); } + + private function isNonFixtureFileWithIncSuffix(string $filePath): bool + { + if (Strings::match($filePath, '#/Fixture/#')) { + return false; + } + + return Strings::endsWith($filePath, '.inc'); + } } diff --git a/rules/php70/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php b/rules/php70/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php index 2205f81c780..9c00445b7e5 100644 --- a/rules/php70/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php +++ b/rules/php70/src/Rector/StaticCall/StaticCallOnNonStaticToInstanceCallRector.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Rector\Php70\Rector\StaticCall; use PhpParser\Node; +use PhpParser\Node\Expr; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\PropertyFetch; @@ -23,6 +24,7 @@ use ReflectionClass; * @see https://3v4l.org/tQ32f * @see https://3v4l.org/jB9jn * @see https://stackoverflow.com/a/19694064/1348344 + * * @see \Rector\Php70\Tests\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector\StaticCallOnNonStaticToInstanceCallRectorTest */ final class StaticCallOnNonStaticToInstanceCallRector extends AbstractRector @@ -97,6 +99,10 @@ PHP */ public function refactor(Node $node): ?Node { + if ($node->name instanceof Expr) { + return null; + } + $methodName = $this->getName($node->name); $className = $this->resolveStaticCallClassName($node); diff --git a/src/Testing/PHPUnit/AbstractGenericRectorTestCase.php b/src/Testing/PHPUnit/AbstractGenericRectorTestCase.php index 22fe3d24243..61645ec96cf 100644 --- a/src/Testing/PHPUnit/AbstractGenericRectorTestCase.php +++ b/src/Testing/PHPUnit/AbstractGenericRectorTestCase.php @@ -128,6 +128,11 @@ abstract class AbstractGenericRectorTestCase extends AbstractKernelTestCase protected function tearDown(): void { $this->restoreOldParameterValues(); + + // restore PHP version if changed + if ($this->getPhpVersion() !== '') { + $this->setParameter(Option::PHP_VERSION_FEATURES, '10.0'); + } } protected function getRectorClass(): string diff --git a/src/Testing/PHPUnit/AbstractRectorTestCase.php b/src/Testing/PHPUnit/AbstractRectorTestCase.php index 6eb2a3f6c59..f71b8a83612 100644 --- a/src/Testing/PHPUnit/AbstractRectorTestCase.php +++ b/src/Testing/PHPUnit/AbstractRectorTestCase.php @@ -28,16 +28,6 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase */ private $autoloadTestFixture = true; - protected function tearDown(): void - { - parent::tearDown(); - - // restore PHP version if changed - if ($this->getPhpVersion() !== '') { - $this->setParameter(Option::PHP_VERSION_FEATURES, '10.0'); - } - } - protected function doTestFileInfoWithoutAutoload(SmartFileInfo $fileInfo): void { $this->autoloadTestFixture = false; @@ -136,6 +126,9 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase } } + /** + * @todo decouple to symplify/easy-testing + */ private function updateFixtureContent( SmartFileInfo $originalFileInfo, string $changedContent, diff --git a/utils/project-validator/src/Command/ValidateFixtureContentCommand.php b/utils/project-validator/src/Command/ValidateFixtureContentCommand.php index 7284484342e..bcb11b62a52 100644 --- a/utils/project-validator/src/Command/ValidateFixtureContentCommand.php +++ b/utils/project-validator/src/Command/ValidateFixtureContentCommand.php @@ -53,11 +53,8 @@ final class ValidateFixtureContentCommand extends Command // files content is equal, but it should not $message = sprintf( - 'The "%s" file has same content before "%s" and after it.%sRemove the content after "%s"', - $fixtureFileInfo->getRelativeFilePathFromCwd(), - SplitLine::REGEX, - PHP_EOL, - SplitLine::REGEX + 'The "%s" file has same content before and after. Remove the 2nd half of the file.', + $fixtureFileInfo->getRelativeFilePathFromCwd() ); $this->symfonyStyle->error($message);