decouple CommentRemover (#4412)

This commit is contained in:
Tomas Votruba 2020-10-15 11:50:15 +02:00 committed by GitHub
parent 6f1590efcd
commit a9f737bcaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 170 additions and 36 deletions

View File

@ -16,6 +16,7 @@ jobs:
name: Rector CI
install: composer install --no-progress --ansi
run: composer rector-ci
-
name: Rector without Dev Dependencies
install: composer install --no-progress --ansi --no-dev

View File

@ -14,9 +14,6 @@ name: Rector CI
on:
pull_request: null
push:
branches:
- master
jobs:
rector-ci:

View File

@ -326,6 +326,7 @@
"validate-monorepo": "vendor/bin/monorepo-builder validate --ansi"
},
"config": {
"sort-packages": true
"sort-packages": true,
"process-timeout": 0
}
}

View File

@ -33,21 +33,6 @@ use Symplify\SmartFileSystem\SmartFileInfo;
*/
final class BetterStandardPrinter extends Standard
{
/**
* @var string
*/
private const START_COMMENT_REGEX = '#\/*\*(.*?)\*\/#s';
/**
* @var string
*/
private const START_GRID_COMMENT_REGEX = '#^(\s+)?\#(.*?)$#m';
/**
* @var string
*/
private const START_DOUBLE_SLASH_COMMENT_REGEX = '#\/\/(.*?)$#m';
/**
* @var string
*/
@ -92,10 +77,15 @@ final class BetterStandardPrinter extends Standard
*/
private $docBlockManipulator;
/**
* @var CommentRemover
*/
private $commentRemover;
/**
* @param mixed[] $options
*/
public function __construct(array $options = [])
public function __construct(CommentRemover $commentRemover, array $options = [])
{
parent::__construct($options);
@ -104,6 +94,8 @@ final class BetterStandardPrinter extends Standard
$this->insertionMap['Stmt_ClassMethod->returnType'] = [')', false, ': ', null];
$this->insertionMap['Stmt_Function->returnType'] = [')', false, ': ', null];
$this->insertionMap['Expr_Closure->returnType'] = [')', false, ': ', null];
$this->commentRemover = $commentRemover;
}
/**
@ -143,7 +135,7 @@ final class BetterStandardPrinter extends Standard
{
$printerNode = $this->print($node);
$nodeWithoutComments = $this->removeComments($printerNode);
$nodeWithoutComments = $this->commentRemover->remove($printerNode);
return trim($nodeWithoutComments);
}
@ -478,21 +470,6 @@ final class BetterStandardPrinter extends Standard
}
}
private function removeComments(string $printerNode): string
{
// remove /** ... */
$printerNode = Strings::replace($printerNode, self::START_COMMENT_REGEX);
// remove /* ... */
$printerNode = Strings::replace($printerNode, self::START_COMMENT_REGEX);
// remove # ...
$printerNode = Strings::replace($printerNode, self::START_GRID_COMMENT_REGEX);
// remove // ...
return Strings::replace($printerNode, self::START_DOUBLE_SLASH_COMMENT_REGEX);
}
/**
* @param Node[] $nodes
*/

View File

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Rector\Core\PhpParser\Printer;
use Nette\Utils\Strings;
/**
* @see \Rector\Core\Tests\PhpParser\Printer\CommentRemover\CommentRemoverTest
*/
final class CommentRemover
{
/**
* @see https://regex101.com/r/mpKusH/1
* @var string
*/
private const START_COMMENT_REGEX = '#\/*\*(.*?)\*\/#s';
/**
* @see https://regex101.com/r/YOXUuD/1
* @var string
*/
private const START_GRID_COMMENT_REGEX = '#^(\s+)?\#(.*?)$#m';
/**
* @see https://regex101.com/r/zTLygY/3
* @var string
*/
private const START_DOUBLE_SLASH_COMMENT_REGEX = '#(\/\/.*?)[^;]$#m';
public function remove(string $content): string
{
// remove /** ... */
$content = Strings::replace($content, self::START_COMMENT_REGEX);
// remove /* ... */
$content = Strings::replace($content, self::START_COMMENT_REGEX);
// remove # ...
$content = Strings::replace($content, self::START_GRID_COMMENT_REGEX);
// remove // ...
return Strings::replace($content, self::START_DOUBLE_SLASH_COMMENT_REGEX);
}
}

View File

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\PhpParser\Printer\CommentRemover;
use Iterator;
use Rector\Core\HttpKernel\RectorKernel;
use Rector\Core\PhpParser\Printer\CommentRemover;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\EasyTesting\StaticFixtureSplitter;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class CommentRemoverTest extends AbstractKernelTestCase
{
/**
* @var CommentRemover
*/
private $commentRemover;
protected function setUp(): void
{
self::bootKernel(RectorKernel::class);
$this->commentRemover = static::$container->get(CommentRemover::class);
}
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $smartFileInfo): void
{
$inputAndExpected = StaticFixtureSplitter::splitFileInfoToInputAndExpected($smartFileInfo);
$removedContent = $this->commentRemover->remove($inputAndExpected->getInput());
$this->assertSame(
$inputAndExpected->getExpected(),
$removedContent,
$smartFileInfo->getRelativeFilePathFromCwd()
);
}
public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.php.inc');
}
}

View File

@ -0,0 +1,13 @@
<?php
/* some comment */
$value = 1;
?>
-----
<?php
$value = 1;
?>

View File

@ -0,0 +1,11 @@
<?php
$value = 1;// some comment
?>
-----
<?php
$value = 1;
?>

View File

@ -0,0 +1,12 @@
<?php
# some comment
$value = 1;
?>
-----
<?php
$value = 1;
?>

View File

@ -0,0 +1,15 @@
<?php
namespace Rector\Core\Tests\PhpParser\Printer\CommentRemover\Fixture;
final class KeepLink
{
public function run($value)
{
switch ($value) {
case 'key':
return 'https://some_very_long_link.cz';
break;
}
}
}

View File

@ -0,0 +1,13 @@
<?php
// some comment
$value = 1;
?>
-----
<?php
$value = 1;
?>