[BetterStandardPrinter] Fixes #4652 Skip @return explicit format (#4666)

This commit is contained in:
Abdul Malik Ikhsan 2020-11-21 01:53:41 +07:00 committed by GitHub
parent 131357aac6
commit 36a5c2b01b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 4 deletions

View File

@ -170,6 +170,12 @@ final class BetterStandardPrinter extends Standard
ContentPatcher::VALID_ANNOTATION_ROUTE_LOCALIZATION_REGEX,
ContentPatcher::INVALID_ANNOTATION_ROUTE_LOCALIZATION_REGEX
);
$content = $this->contentPatcher->rollbackValidAnnotation(
$contentOriginal,
$content,
ContentPatcher::VALID_ANNOTATION_RETURN_EXPLICIT_FORMAT_REGEX,
ContentPatcher::INVALID_ANNOTATION_RETURN_EXPLICIT_FORMAT_REGEX
);
// add new line in case of added stmts
if (count($stmts) !== count($origStmts) && ! (bool) Strings::match($content, self::NEWLINE_END_REGEX)) {

View File

@ -80,6 +80,18 @@ final class ContentPatcher
*/
public const INVALID_ANNOTATION_ROUTE_LOCALIZATION_REGEX = '#^\s+\/\*\*\s+\s+\*\s+@.*(\s+\*\s{0,}[^"]*=\s{0,}[^"]*,?){1,}.*\)\s+\*\s+\*\/#msU';
/**
* @see https://regex101.com/r/EA1xRY/2
* @var string
*/
public const VALID_ANNOTATION_RETURN_EXPLICIT_FORMAT_REGEX = '#^\s{0,}\* @return\s+(\(.*\)|(".*")(\|".*"){1,})$#msU';
/**
* @see https://regex101.com/r/LprF44/3
* @var string
*/
public const INVALID_ANNOTATION_RETURN_EXPLICIT_FORMAT_REGEX = '#^\s{0,}\* @return([^\s].*|\s[^"\s]*)$#msU';
/**
* @see https://regex101.com/r/4mBd0y/2
* @var string
@ -99,10 +111,10 @@ final class ContentPatcher
private const SPACE_REGEX = '#\s#';
/**
* @see https://regex101.com/r/lC0i21/1
* @see https://regex101.com/r/lC0i21/2
* @var string
*/
private const STAR_QUOTE_REGEX = '#[\*"]#';
private const STAR_QUOTE_PARENTHESIS_REGEX = '#[\*"\(\)]#';
/**
* @see https://regex101.com/r/j7agVx/1
@ -165,6 +177,7 @@ final class ContentPatcher
* @see https://github.com/rectorphp/rector/issues/4581
* @see https://github.com/rectorphp/rector/issues/4476
* @see https://github.com/rectorphp/rector/issues/4620
* @see https://github.com/rectorphp/rector/issues/4652
*/
public function rollbackValidAnnotation(
string $originalContent,
@ -206,8 +219,8 @@ final class ContentPatcher
$invalidAnnotation = Strings::replace($invalidAnnotation, self::SPACE_REGEX, '');
if ($validAnnotationRegex !== self::VALID_ANNOTATION_ROUTE_REGEX) {
$validAnnotation = Strings::replace($validAnnotation, self::STAR_QUOTE_REGEX, '');
$invalidAnnotation = Strings::replace($invalidAnnotation, self::STAR_QUOTE_REGEX, '');
$validAnnotation = Strings::replace($validAnnotation, self::STAR_QUOTE_PARENTHESIS_REGEX, '');
$invalidAnnotation = Strings::replace($invalidAnnotation, self::STAR_QUOTE_PARENTHESIS_REGEX, '');
if ($validAnnotationRegex === self::VALID_ANNOTATION_ROUTE_LOCALIZATION_REGEX) {
$validAnnotation = Strings::replace($validAnnotation, self::ROUTE_LOCALIZATION_REPLACE_REGEX, '');

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\Issue4652\DoNotChangeExplicitCommentFormat;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class DoNotChangeExplicitCommentFormatTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
// bin/rector process ... --config config/some_config.php
protected function provideConfigFileInfo(): SmartFileInfo
{
return new SmartFileInfo(__DIR__ . '/config/some_config.php');
}
}

View File

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\Issue4652\DoNotChangeExplicitCommentFormat\Fixture;
/**
* @return (0)
*/
function one(int $value)
{
}
/**
* @return ""|"0"
*/
function two(int $value)
{
}
one(1);
two(2);

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::SETS, [SetList::DEAD_CODE]);
};