diff --git a/packages/file-system-rector/src/Rector/AbstractFileSystemRector.php b/packages/file-system-rector/src/Rector/AbstractFileSystemRector.php index 2a8089b17ea..27449f7991a 100644 --- a/packages/file-system-rector/src/Rector/AbstractFileSystemRector.php +++ b/packages/file-system-rector/src/Rector/AbstractFileSystemRector.php @@ -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('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"); - } } diff --git a/rules/psr4/src/Rector/MultipleClassFileToPsr4ClassesRector.php b/rules/psr4/src/Rector/MultipleClassFileToPsr4ClassesRector.php index aa951b17c61..83232f2d7d6 100644 --- a/rules/psr4/src/Rector/MultipleClassFileToPsr4ClassesRector.php +++ b/rules/psr4/src/Rector/MultipleClassFileToPsr4ClassesRector.php @@ -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; + } } diff --git a/rules/psr4/tests/Rector/MultipleClassFileToPsr4ClassesRector/Expected/ArgumentOutOfRangeException.php b/rules/psr4/tests/Rector/MultipleClassFileToPsr4ClassesRector/Expected/ArgumentOutOfRangeException.php index ae0cd73305d..771e5fd04b0 100644 --- a/rules/psr4/tests/Rector/MultipleClassFileToPsr4ClassesRector/Expected/ArgumentOutOfRangeException.php +++ b/rules/psr4/tests/Rector/MultipleClassFileToPsr4ClassesRector/Expected/ArgumentOutOfRangeException.php @@ -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 { } diff --git a/rules/psr4/tests/Rector/MultipleClassFileToPsr4ClassesRector/Expected/FirstException.php b/rules/psr4/tests/Rector/MultipleClassFileToPsr4ClassesRector/Expected/FirstException.php index a9b9daf02e6..36325bff7a4 100644 --- a/rules/psr4/tests/Rector/MultipleClassFileToPsr4ClassesRector/Expected/FirstException.php +++ b/rules/psr4/tests/Rector/MultipleClassFileToPsr4ClassesRector/Expected/FirstException.php @@ -1,7 +1,6 @@ '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']; } diff --git a/rules/psr4/tests/Rector/MultipleClassFileToPsr4ClassesRector/Expected/SecondException.php b/rules/psr4/tests/Rector/MultipleClassFileToPsr4ClassesRector/Expected/SecondException.php index 4d2d0fb84ed..234bfd742df 100644 --- a/rules/psr4/tests/Rector/MultipleClassFileToPsr4ClassesRector/Expected/SecondException.php +++ b/rules/psr4/tests/Rector/MultipleClassFileToPsr4ClassesRector/Expected/SecondException.php @@ -1,7 +1,6 @@ symfonyStyle->writeln('----------------------------------------'); $this->symfonyStyle->writeln($fileContent); + $this->symfonyStyle->writeln('----------------------------------------'); } }