cleanup AbstractFileSystemRector

This commit is contained in:
TomasVotruba 2020-06-21 11:30:23 +02:00
parent ff4c3cc8ce
commit 379aa80679
15 changed files with 20 additions and 107 deletions

View File

@ -7,7 +7,6 @@ namespace Rector\FileSystemRector\Rector;
use Nette\Utils\Strings;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\ParserFactory;
use Rector\Autodiscovery\ValueObject\NodesWithFileDestination;
use Rector\Core\Application\FileProcessor;
use Rector\Core\Application\TokensByFilePathStorage;
@ -20,7 +19,6 @@ use Rector\PostRector\Application\PostFileProcessor;
use Rector\PSR4\Collector\RenamedClassesCollector;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\SmartFileSystem\SmartFileInfo;
use TypeError;
abstract class AbstractFileSystemRector implements FileSystemRectorInterface
{
@ -51,11 +49,6 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
*/
private $formatPerservingPrinter;
/**
* @var ParserFactory
*/
private $parserFactory;
/**
* @var PostFileProcessor
*/
@ -80,7 +73,6 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
* @required
*/
public function autowireAbstractFileSystemRector(
ParserFactory $parserFactory,
Lexer $lexer,
FormatPerservingPrinter $formatPerservingPrinter,
Configuration $configuration,
@ -91,7 +83,6 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
TokensByFilePathStorage $tokensByFilePathStorage,
FileProcessor $fileProcessor
): void {
$this->parserFactory = $parserFactory;
$this->lexer = $lexer;
$this->formatPerservingPrinter = $formatPerservingPrinter;
$this->configuration = $configuration;
@ -128,7 +119,7 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
/**
* @param Node[] $nodes
*/
protected function printNodesToFilePath(array $nodes, string $fileDestination): void
protected function printNodesToFilePath(array $nodes, string $fileDestination, SmartFileInfo $smartFileInfo): void
{
$nodes = $this->postFileProcessor->traverse($nodes);
@ -151,38 +142,8 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
// re-index keys from 0
$nodes = array_values($nodes);
// 1. if nodes are the same, prefer format preserving printer
try {
$dummyLexer = new Lexer();
$dummyParser = $this->parserFactory->create(ParserFactory::PREFER_PHP7, $dummyLexer);
$dummyParser->parse('<?php ' . $this->print($nodes));
$dummyTokenCount = count($dummyLexer->getTokens());
$modelTokenCount = count($this->lexer->getTokens());
if ($dummyTokenCount > $modelTokenCount) {
// nothing we can do - this would end by "Undefined offset in TokenStream.php on line X" error
$formatPreservingContent = '';
} else {
$formatPreservingContent = $this->formatPerservingPrinter->printToString(
$nodes,
$this->oldStmts,
$this->lexer->getTokens()
);
}
} catch (TypeError $typeError) {
// incompatible tokens, nothing we can do to preserve format
$formatPreservingContent = '';
}
$prettyPrintContent = $this->betterStandardPrinter->prettyPrintFile($nodes);
if ($this->areStringsSameWithoutSpaces($formatPreservingContent, $prettyPrintContent)) {
$fileContent = $formatPreservingContent;
} else {
$prettyPrintContent = $this->resolveLastEmptyLine($prettyPrintContent);
$fileContent = $prettyPrintContent;
}
$fileContent = $this->betterStandardPrinter->prettyPrintFile($nodes);
$fileContent = $this->resolveLastEmptyLine($fileContent);
$this->addFile($fileDestination, $fileContent);
}
@ -192,14 +153,6 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
$this->addNodesWithFileDestination($nodesWithFileDestination);
}
/**
* Also without FQN "\" that are added by basic printer
*/
private function areStringsSameWithoutSpaces(string $firstString, string $secondString): bool
{
return $this->clearString($firstString) === $this->clearString($secondString);
}
/**
* Add empty line in the end, if it is in the original tokens
*/
@ -213,28 +166,4 @@ abstract class AbstractFileSystemRector implements FileSystemRectorInterface
return $prettyPrintContent;
}
private function clearString(string $string): string
{
$string = $this->removeComments($string);
// remove all spaces
$string = Strings::replace($string, '#\s+#', '');
// remove FQN "\" that are added by basic printer
$string = Strings::replace($string, '#\\\\#', '');
// remove trailing commas, as one of them doesn't have to contain them
return Strings::replace($string, '#\,#', '');
}
private function removeComments(string $string): string
{
// remove comments like this noe
$string = Strings::replace($string, '#\/\/(.*?)\n#', '');
$string = Strings::replace($string, '#/\*.*?\*/#s', '');
return Strings::replace($string, '#\n\s*\n#', "\n");
}
}

View File

@ -141,9 +141,7 @@ PHP
private function processNodesWithoutNamespace(array $nodes, SmartFileInfo $smartFileInfo, bool $shouldDelete): void
{
// process only files with 2 classes and more
$classes = $this->betterNodeFinder->findClassLikes($nodes);
if (count($classes) <= 1) {
if (! $this->hasAtLeast2Classes($nodes)) {
return;
}
@ -165,7 +163,7 @@ PHP
if ($shouldDelete) {
$this->printNewNodesToFilePath($nodes, $fileDestination);
} else {
$this->printNodesToFilePath($nodes, $fileDestination);
$this->printNodesToFilePath($nodes, $fileDestination, $smartFileInfo);
}
}
}
@ -201,7 +199,7 @@ PHP
if ($shouldDeleteFile) {
$this->printNewNodesToFilePath($newStmtsSet, $fileDestination);
} else {
$this->printNodesToFilePath($newStmtsSet, $fileDestination);
$this->printNodesToFilePath($newStmtsSet, $fileDestination, $smartFileInfo);
}
}
@ -220,4 +218,14 @@ PHP
}
}
}
/**
* @param Node[] $nodes
*/
private function hasAtLeast2Classes(array $nodes): bool
{
$classes = $this->betterNodeFinder->findClassLikes($nodes);
return count($classes) > 1;
}
}

View File

@ -4,17 +4,14 @@
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace NettePostfixedToUniqueAutoload;
use InvalidArgumentException;
/**
* The exception that is thrown when the value of an argument is
* outside the allowable range of values as defined by the invoked method.
*/
class ArgumentOutOfRangeException extends InvalidArgumentException
class ArgumentOutOfRangeException extends \InvalidArgumentException
{
}

View File

@ -1,7 +1,6 @@
<?php
declare(strict_types=1);
namespace Rector\PSR4\Tests\Rector\MultipleClassFileToPsr4ClassesRector\Source;
final class FirstException

View File

@ -4,14 +4,10 @@
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace NettePostfixedToUniqueAutoload;
use InvalidArgumentException;
/**
* The exception that is thrown when a method call is invalid for the object's
* current state, method has been invoked at an illegal or inappropriate time.

View File

@ -1,7 +1,6 @@
<?php
declare(strict_types=1);
final class JustOneExceptionWithoutNamespace
{
}

View File

@ -1,7 +1,6 @@
<?php
declare(strict_types=1);
final class JustTwoExceptionWithoutNamespace
{
}

View File

@ -1,7 +1,6 @@
<?php
declare(strict_types=1);
namespace Rector\PSR4\Tests\Rector\MultipleClassFileToPsr4ClassesRector\Expected;
final class Miss

View File

@ -1,7 +1,6 @@
<?php
declare(strict_types=1);
namespace Rector\PSR4\Tests\Rector\MultipleClassFileToPsr4ClassesRector\Expected;
final class MyClass

View File

@ -1,7 +1,6 @@
<?php
declare(strict_types=1);
namespace Rector\PSR4\Tests\Rector\MultipleClassFileToPsr4ClassesRector\Expected;
interface MyInterface

View File

@ -1,7 +1,6 @@
<?php
declare(strict_types=1);
namespace Rector\PSR4\Tests\Rector\MultipleClassFileToPsr4ClassesRector\Expected;
trait MyTrait

View File

@ -1,7 +1,6 @@
<?php
declare(strict_types=1);
namespace Rector\PSR4\Tests\Rector\MultipleClassFileToPsr4ClassesRector\Expected;
final class Named

View File

@ -4,10 +4,7 @@
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace NettePostfixedToUniqueAutoload\Utils;
/**
@ -15,12 +12,5 @@ namespace NettePostfixedToUniqueAutoload\Utils;
*/
class RegexpException extends \Exception
{
public const MESSAGES = [
PREG_INTERNAL_ERROR => 'Internal error',
PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted',
PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted',
PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data',
PREG_BAD_UTF8_OFFSET_ERROR => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point',
6 => 'Failed due to limited JIT stack space', // PREG_JIT_STACKLIMIT_ERROR
];
public const MESSAGES = [PREG_INTERNAL_ERROR => 'Internal error', PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted', PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted', PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data', PREG_BAD_UTF8_OFFSET_ERROR => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point', 6 => 'Failed due to limited JIT stack space'];
}

View File

@ -1,7 +1,6 @@
<?php
declare(strict_types=1);
namespace Rector\PSR4\Tests\Rector\MultipleClassFileToPsr4ClassesRector\Source;
final class SecondException

View File

@ -91,7 +91,9 @@ final class RemovedAndAddedFilesProcessor
);
}
$this->symfonyStyle->writeln('----------------------------------------');
$this->symfonyStyle->writeln($fileContent);
$this->symfonyStyle->writeln('----------------------------------------');
}
}