|
23 | 23 | use Rector\ValueObject\Reporting\FileDiff; |
24 | 24 | use RectorPrefix202403\Symfony\Component\Console\Style\SymfonyStyle; |
25 | 25 | use Throwable; |
| 26 | +use RectorPrefix202403\Nette\Utils\Strings; |
26 | 27 | final class FileProcessor |
27 | 28 | { |
28 | 29 | /** |
@@ -75,6 +76,11 @@ final class FileProcessor |
75 | 76 | * @var \Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator |
76 | 77 | */ |
77 | 78 | private $nodeScopeAndMetadataDecorator; |
| 79 | + /** |
| 80 | + * @var string |
| 81 | + * @see https://regex101.com/r/llm7XZ/1 |
| 82 | + */ |
| 83 | + private const OPEN_TAG_SPACED_REGEX = '#^[ \\t]+<\\?php#m'; |
78 | 84 | public function __construct(FormatPerservingPrinter $formatPerservingPrinter, RectorNodeTraverser $rectorNodeTraverser, SymfonyStyle $symfonyStyle, FileDiffFactory $fileDiffFactory, ChangedFilesDetector $changedFilesDetector, ErrorFactory $errorFactory, FilePathHelper $filePathHelper, PostFileProcessor $postFileProcessor, RectorParser $rectorParser, NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator) |
79 | 85 | { |
80 | 86 | $this->formatPerservingPrinter = $formatPerservingPrinter; |
@@ -162,11 +168,16 @@ private function printFile(File $file, Configuration $configuration, string $fil |
162 | 168 | * Handle new line or space before <?php or InlineHTML node wiped on print format preserving |
163 | 169 | * On very first content level |
164 | 170 | */ |
165 | | - $originalFileContent = $file->getOriginalFileContent(); |
166 | | - $ltrimOriginalFileContent = \ltrim($originalFileContent); |
| 171 | + $ltrimOriginalFileContent = \ltrim($file->getOriginalFileContent()); |
167 | 172 | if ($ltrimOriginalFileContent === $newContent) { |
168 | 173 | return; |
169 | 174 | } |
| 175 | + // handle space before <?php |
| 176 | + $ltrimNewContent = Strings::replace($newContent, self::OPEN_TAG_SPACED_REGEX, '<?php'); |
| 177 | + $ltrimOriginalFileContent = Strings::replace($ltrimOriginalFileContent, self::OPEN_TAG_SPACED_REGEX, '<?php'); |
| 178 | + if ($ltrimOriginalFileContent === $ltrimNewContent) { |
| 179 | + return; |
| 180 | + } |
170 | 181 | } |
171 | 182 | // change file content early to make $file->hasChanged() based on new content |
172 | 183 | $file->changeFileContent($newContent); |
|
0 commit comments