Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Handle printFormatPreserving on inline html with open and close php tag #1368

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
php_version: ['8.0']
directory:
- 'e2e/template-extends'
- 'e2e/plain-views'

name: End to end test - ${{ matrix.directory }}

Expand All @@ -44,5 +45,5 @@ jobs:
working-directory: ${{ matrix.directory }}

-
run: ./../../bin/rector process src --dry-run --ansi -a vendor/autoload.php
run: ./../../bin/rector process --dry-run --ansi -a vendor/autoload.php
working-directory: ${{ matrix.directory }}
3 changes: 3 additions & 0 deletions e2e/plain-views/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[src/view.php]
insert_final_newline = false
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions e2e/plain-views/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
7 changes: 7 additions & 0 deletions e2e/plain-views/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"require": {
"php": "^8.0"
},
"minimum-stability": "dev",
"prefer-stable": true
}
15 changes: 15 additions & 0 deletions e2e/plain-views/rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Core\Configuration\Option;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set(Option::PATHS, [
__DIR__.'/src/',
]);
};

4 changes: 4 additions & 0 deletions e2e/plain-views/src/view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

<div>
<?php echo 1 ?>
</div>
6 changes: 6 additions & 0 deletions e2e/template-extends/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Core\Configuration\Option;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set(Option::PATHS, [
__DIR__.'/src/',
]);
};

31 changes: 31 additions & 0 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,29 @@
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\PhpParser\Printer\Whitespace\IndentCharacterDetector;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\Util\StringUtils;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\SmartFileSystem\SmartFileSystem;

/**
* @see \Rector\Core\Tests\PhpParser\Printer\BetterStandardPrinterTest
*/
final class BetterStandardPrinter extends Standard
{
/**
* @var string
* @see https://regex101.com/r/QA7mai/1
*/
private const EMPTY_STARTING_TAG_REGEX = '/^<\\?php\\s+\\?>\\n?/';

/**
* @var string
* @see https://regex101.com/r/IVNkrt/1
*/
private const EMPTY_ENDING_TAG_REGEX = '/<\\?php$/';

/**
* @var string
* @see https://regex101.com/r/jUFizd/1
Expand Down Expand Up @@ -76,6 +91,8 @@ final class BetterStandardPrinter extends Standard
public function __construct(
private IndentCharacterDetector $indentCharacterDetector,
private DocBlockUpdater $docBlockUpdater,
private CurrentFileProvider $currentFileProvider,
private SmartFileSystem $smartFileSystem,
array $options = []
) {
parent::__construct($options);
Expand All @@ -102,6 +119,20 @@ public function printFormatPreserving(array $stmts, array $origStmts, array $ori

$content = parent::printFormatPreserving($newStmts, $origStmts, $origTokens);

// strip empty starting/ending php tags
if (array_key_exists(0, $stmts) && $stmts[0] instanceof FileWithoutNamespace) {
$originalContent = $content;

$newContent = Strings::replace($content, self::EMPTY_STARTING_TAG_REGEX, '');
$newContent = Strings::replace(\rtrim($newContent), self::EMPTY_ENDING_TAG_REGEX, '',) . "\n";

if ($originalContent !== $newContent) {
/** @var File $file */
$file = $this->currentFileProvider->getFile();
return $this->smartFileSystem->readFile($file->getFilePath());
}
}

// add new line in case of added stmts
if (count($stmts) !== count($origStmts) && ! StringUtils::isMatch($content, self::NEWLINE_END_REGEX)) {
$content .= $this->nl;
Expand Down
10 changes: 8 additions & 2 deletions src/ValueObjectFactory/ProcessResultFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector;
use Rector\Core\ValueObject\Application\File;
use Rector\Core\ValueObject\ProcessResult;
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\PostRector\Collector\NodesToRemoveCollector;

final class ProcessResultFactory
Expand All @@ -28,11 +29,16 @@ public function create(array $files): ProcessResult
foreach ($files as $file) {
$errors = array_merge($errors, $file->getErrors());

if ($file->getFileDiff() === null) {
$fileDiff = $file->getFileDiff();
if (! $fileDiff instanceof FileDiff) {
continue;
}

$fileDiffs[] = $file->getFileDiff();
if ($fileDiff->getDiff() === '') {
continue;
}

$fileDiffs[] = $fileDiff;
}

return new ProcessResult(
Expand Down