Skip to content

Commit

Permalink
Merge pull request #3830 from rectorphp/fix-call
Browse files Browse the repository at this point in the history
[PHP 7.0] Fix variable name on static call
  • Loading branch information
TomasVotruba authored Jul 30, 2020
2 parents 1796665 + 2ad0116 commit 8f765da
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
15 changes: 11 additions & 4 deletions packages/rector-generator/src/FileSystem/TemplateFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ public function resolveDestination(
$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;
Expand All @@ -50,4 +48,13 @@ private function applyVariables(string $content, array $variables): string
{
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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand All @@ -23,6 +24,7 @@
* @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
Expand Down Expand Up @@ -97,6 +99,10 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->name instanceof Expr) {
return null;
}

$methodName = $this->getName($node->name);

$className = $this->resolveStaticCallClassName($node);
Expand Down
5 changes: 5 additions & 0 deletions src/Testing/PHPUnit/AbstractGenericRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ protected function setUp(): void
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
Expand Down
13 changes: 3 additions & 10 deletions src/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -136,6 +126,9 @@ private function doTestFileMatchesExpectedContent(
}
}

/**
* @todo decouple to symplify/easy-testing
*/
private function updateFixtureContent(
SmartFileInfo $originalFileInfo,
string $changedContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// 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);
Expand Down

0 comments on commit 8f765da

Please sign in to comment.