<commit_before><?php

declare(strict_types=1);

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;
use Rector\Core\Configuration\Configuration;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\PhpParser\Printer\FormatPerservingPrinter;
use Rector\Core\Rector\AbstractRector\AbstractRectorTrait;
use Rector\FileSystemRector\Contract\FileSystemRectorInterface;
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
{
    use AbstractRectorTrait;

    /**
     * @var Configuration
     */
    protected $configuration;

    /**
     * @var ParameterProvider
     */
    protected $parameterProvider;

    /**
     * @var Node[]
     */
    private $oldStmts = [];

    /**
     * @var Lexer
     */
    private $lexer;

    /**
     * @var FormatPerservingPrinter
     */
    private $formatPerservingPrinter;

    /**
     * @var ParserFactory
     */
    private $parserFactory;

    /**
     * @var PostFileProcessor
     */
    private $postFileProcessor;

    /**
     * @var RenamedClassesCollector
     */
    private $renamedClassesCollector;

    /**
     * @var TokensByFilePathStorage
     */
    private $tokensByFilePathStorage;

    /**
     * @var FileProcessor
     */
    private $fileProcessor;

    /**
     * @required
     */
    public function autowireAbstractFileSystemRector(
        ParserFactory $parserFactory,
        Lexer $lexer,
        FormatPerservingPrinter $formatPerservingPrinter,
        Configuration $configuration,
        BetterStandardPrinter $betterStandardPrinter,
        ParameterProvider $parameterProvider,
        PostFileProcessor $postFileProcessor,
        RenamedClassesCollector $renamedClassesCollector,
        TokensByFilePathStorage $tokensByFilePathStorage,
        FileProcessor $fileProcessor
    ): void {
        $this->parserFactory = $parserFactory;
        $this->lexer = $lexer;
        $this->formatPerservingPrinter = $formatPerservingPrinter;
        $this->configuration = $configuration;
        $this->betterStandardPrinter = $betterStandardPrinter;
        $this->parameterProvider = $parameterProvider;
        $this->postFileProcessor = $postFileProcessor;
        $this->renamedClassesCollector = $renamedClassesCollector;
        $this->tokensByFilePathStorage = $tokensByFilePathStorage;
        $this->fileProcessor = $fileProcessor;
    }

    protected function addClassRename(string $oldClass, string $newClass): void
    {
        $this->renamedClassesCollector->addClassRename($oldClass, $newClass);
    }

    /**
     * @return Node[]
     */
    protected function parseFileInfoToNodes(SmartFileInfo $smartFileInfo): array
    {
        if (! $this->tokensByFilePathStorage->hasForFileInfo($smartFileInfo)) {
            $this->fileProcessor->parseFileInfoToLocalCache($smartFileInfo);
        }

        $parsedStmtsAndTokens = $this->tokensByFilePathStorage->getForFileInfo($smartFileInfo);

        // needed for format preserving
        $this->oldStmts = $parsedStmtsAndTokens->getOldStmts();

        return $parsedStmtsAndTokens->getNewStmts();
    }

    /**
     * @param Node[] $nodes
     */
    protected function printNodesToFilePath(array $nodes, string $fileDestination): void
    {
        $nodes = $this->postFileProcessor->traverse($nodes);

        $fileContent = $this->formatPerservingPrinter->printToString(
            $nodes,
            $this->oldStmts,
            $this->lexer->getTokens()
        );

        $this->addFile($fileDestination, $fileContent);
    }

    /**
     * @param Node[] $nodes
     */
    protected function printNewNodesToFilePath(array $nodes, string $fileDestination): void
    {
        $nodes = $this->postFileProcessor->traverse($nodes);

        // 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;
        }

        $this->addFile($fileDestination, $fileContent);
    }

    protected function printNodesWithFileDestination(
        NodesWithFileDestination $nodesWithFileDestination
    ): void {
        $this->printNewNodesToFilePath(
            $nodesWithFileDestination->getNodes(),
            $nodesWithFileDestination->getFileDestination()<commit_msg>fixup! fixup! decopule AbstractFileMovingFileSystemRector<commit_after><?php

declare(strict_types=1);

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;
use Rector\Core\Configuration\Configuration;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\PhpParser\Printer\FormatPerservingPrinter;
use Rector\Core\Rector\AbstractRector\AbstractRectorTrait;
use Rector\FileSystemRector\Contract\FileSystemRectorInterface;
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
{
    use AbstractRectorTrait;

    /**
     * @var Configuration
     */
    protected $configuration;

    /**
     * @var ParameterProvider
     */
    protected $parameterProvider;

    /**
     * @var Node[]
     */
    private $oldStmts = [];

    /**
     * @var Lexer
     */
    private $lexer;

    /**
     * @var FormatPerservingPrinter
     */
    private $formatPerservingPrinter;

    /**
     * @var ParserFactory
     */
    private $parserFactory;

    /**
     * @var PostFileProcessor
     */
    private $postFileProcessor;

    /**
     * @var RenamedClassesCollector
     */
    private $renamedClassesCollector;

    /**
     * @var TokensByFilePathStorage
     */
    private $tokensByFilePathStorage;

    /**
     * @var FileProcessor
     */
    private $fileProcessor;

    /**
     * @required
     */
    public function autowireAbstractFileSystemRector(
        ParserFactory $parserFactory,
        Lexer $lexer,
        FormatPerservingPrinter $formatPerservingPrinter,
        Configuration $configuration,
        BetterStandardPrinter $betterStandardPrinter,
        ParameterProvider $parameterProvider,
        PostFileProcessor $postFileProcessor,
        RenamedClassesCollector $renamedClassesCollector,
        TokensByFilePathStorage $tokensByFilePathStorage,
        FileProcessor $fileProcessor
    ): void {
        $this->parserFactory = $parserFactory;
        $this->lexer = $lexer;
        $this->formatPerservingPrinter = $formatPerservingPrinter;
        $this->configuration = $configuration;
        $this->betterStandardPrinter = $betterStandardPrinter;
        $this->parameterProvider = $parameterProvider;
        $this->postFileProcessor = $postFileProcessor;
        $this->renamedClassesCollector = $renamedClassesCollector;
        $this->tokensByFilePathStorage = $tokensByFilePathStorage;
        $this->fileProcessor = $fileProcessor;
    }

    protected function addClassRename(string $oldClass, string $newClass): void
    {
        $this->renamedClassesCollector->addClassRename($oldClass, $newClass);
    }

    /**
     * @return Node[]
     */
    protected function parseFileInfoToNodes(SmartFileInfo $smartFileInfo): array
    {
        if (! $this->tokensByFilePathStorage->hasForFileInfo($smartFileInfo)) {
            $this->fileProcessor->parseFileInfoToLocalCache($smartFileInfo);
        }

        $parsedStmtsAndTokens = $this->tokensByFilePathStorage->getForFileInfo($smartFileInfo);

        // needed for format preserving
        $this->oldStmts = $parsedStmtsAndTokens->getOldStmts();

        return $parsedStmtsAndTokens->getNewStmts();
    }

    /**
     * @param Node[] $nodes
     */
    protected function printNodesToFilePath(array $nodes, string $fileDestination): void
    {
        $nodes = $this->postFileProcessor->traverse($nodes);

        $fileContent = $this->formatPerservingPrinter->printToString(
            $nodes,
            $this->oldStmts,
            $this->lexer->getTokens()
        );

        $this->addFile($fileDestination, $fileContent);
    }

    /**
     * @param Node[] $nodes
     */
    protected function printNewNodesToFilePath(array $nodes, string $fileDestination): void
    {
        $nodes = $this->postFileProcessor->traverse($nodes);

        // 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;
        }

        $this->addFile($fileDestination, $fileContent);
    }

    protected function printNodesWithFileDestination(NodesWithFileDestination $nodesWithFileDestination): void
    {
        $this->printNewNodesToFilePath(
            $nodesWithFileDestination->getNodes(),
            $nodesWithFileDestination->getFileDestination()
{'fraction_non_alphanumeric': 0.10445064871078995, 'fraction_numerical': 0.0008211528986697323, 'mean_word_length': 2.8516761543327007, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5217818', 'n_tokens_mistral': 3388, 'n_tokens_neox': 3277, 'n_words': 563}