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

strip empty starting/ending php tags #1329

Merged
merged 42 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
fe5a519
strip empty starting/ending php tags
clxmstaab Nov 28, 2021
481d359
Update BetterStandardPrinter.php
clxmstaab Nov 28, 2021
80fff97
Update BetterStandardPrinter.php
clxmstaab Nov 28, 2021
83b511a
ues Strings::replace() instead
clxmstaab Nov 28, 2021
77919a8
fix param order
clxmstaab Nov 28, 2021
fdadf2f
extract constants
clxmstaab Nov 28, 2021
a54697c
rename const
clxmstaab Nov 28, 2021
76625d4
added e2e test
clxmstaab Nov 29, 2021
eb5839b
simplify repro
staabm Nov 29, 2021
f88aacc
fix repro
clxmstaab Nov 29, 2021
20b6ad7
disable newline trimming for src/view.php
clxmstaab Nov 29, 2021
46abb6b
editorconfig: don't insert final newline
clxmstaab Nov 29, 2021
4b6838c
added regex101 links
clxmstaab Nov 29, 2021
c161283
use local rector binary for e2e tests
clxmstaab Nov 29, 2021
9949595
use local rector path config
clxmstaab Nov 29, 2021
f9d5b97
fix
clxmstaab Nov 30, 2021
14b9713
fix
clxmstaab Nov 30, 2021
655e297
fix
clxmstaab Nov 30, 2021
87beaee
disable PR fix
clxmstaab Nov 30, 2021
6c56429
re-enable fix
clxmstaab Nov 30, 2021
376d859
resolve FileWithoutNamespace recursively
clxmstaab Dec 2, 2021
93b99ac
support expected-output per e2e test
clxmstaab Dec 2, 2021
6f57d72
debug-out
clxmstaab Dec 2, 2021
838aa91
more debug
clxmstaab Dec 2, 2021
1cc31df
debug
clxmstaab Dec 2, 2021
1f222a6
typo
clxmstaab Dec 2, 2021
8805133
Update e2e.yaml
clxmstaab Dec 2, 2021
1b7ffc3
fix
clxmstaab Dec 2, 2021
4cb35ea
Update e2e.yaml
clxmstaab Dec 2, 2021
a59ff1c
Update e2e.yaml
clxmstaab Dec 2, 2021
7c5ecec
Update e2e.yaml
clxmstaab Dec 2, 2021
7a136cc
Update e2e.yaml
clxmstaab Dec 2, 2021
1182594
Update e2e.yaml
clxmstaab Dec 2, 2021
86987a6
Update e2e.yaml
clxmstaab Dec 2, 2021
1a3c741
Update e2e.yaml
clxmstaab Dec 2, 2021
1281736
Update expected-output.diff
clxmstaab Dec 2, 2021
f90114d
fix
clxmstaab Dec 2, 2021
1b23c3c
Update expected-output.diff
clxmstaab Dec 2, 2021
e50639f
adding a empty file, for better test-coverage
clxmstaab Dec 2, 2021
bc41062
Update e2e/plain-views/src/empty.php
staabm Dec 2, 2021
8b07f50
extracted e2eTestRunner from github action workflow
clxmstaab Dec 3, 2021
e19db97
added a success message
clxmstaab Dec 3, 2021
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
5 changes: 4 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 @@ -43,6 +44,8 @@ jobs:
run: composer install --ansi
working-directory: ${{ matrix.directory }}

# run e2e test
-
run: ./../../bin/rector process src --dry-run --ansi -a vendor/autoload.php
run: php ../e2eTestRunner.php
working-directory: ${{ matrix.directory }}

36 changes: 36 additions & 0 deletions e2e/e2eTestRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env php
<?php

// runs a rector e2e test.
// checks whether we expect a certain output, or alternatively that rector just processed everything without errors

$projectRoot = __DIR__ .'/../';
$rectorBin = $projectRoot .'bin/rector';
$autoloadFile = $projectRoot .'vendor/autoload.php';

$e2eCommand = 'php '. $rectorBin .' process --dry-run --no-ansi --no-progress-bar -a '. $autoloadFile;
exec($e2eCommand, $output, $exitCode);
$output = trim(implode("\n", $output));

$expectedDiff = 'expected-output.diff';
if (!file_exists($expectedDiff)) {
echo $output;
exit($exitCode);
}

$matchedExpectedOutput = false;
$expectedOutput = trim(file_get_contents($expectedDiff));
if ($output === $expectedOutput) {
$matchedExpectedOutput = true;
}

if ($matchedExpectedOutput === false) {
echo "\nEXPECTED:\n";
var_dump($expectedOutput);
echo "\nACTUAL:\n";
var_dump($output);
exit(1);
}

echo "[OK] end-to-end test successfully completed.";
exit(0);
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
}
16 changes: 16 additions & 0 deletions e2e/plain-views/expected-output.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
1 file with changes
===================

1) src/view.php:0

---------- begin diff ----------
@@ @@
-
- <div>
+<div>
<?php echo 1 ?>
- </div>
+ </div>
----------- end diff -----------

[OK] 1 file would have changed (dry-run) by Rector
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/',
]);
};

1 change: 1 addition & 0 deletions e2e/plain-views/src/empty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
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, [
staabm marked this conversation as resolved.
Show resolved Hide resolved
__DIR__.'/src/',
]);
};

20 changes: 19 additions & 1 deletion src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@
*/
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 @@ -102,6 +114,12 @@ 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) {
$content = Strings::replace($content, self::EMPTY_STARTING_TAG_REGEX, '');
$content = Strings::replace(\rtrim($content), self::EMPTY_ENDING_TAG_REGEX, '', ) ."\n";
staabm marked this conversation as resolved.
Show resolved Hide resolved
}

// 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 Expand Up @@ -427,7 +445,7 @@ protected function pCommaSeparated(array $nodes): string
private function resolveNewStmts(array $stmts): array
{
if (count($stmts) === 1 && $stmts[0] instanceof FileWithoutNamespace) {
return $stmts[0]->stmts;
return $this->resolveNewStmts($stmts[0]->stmts);
}

return $stmts;
Expand Down