From 4087029cb9e3131fd3e27cc160d476cb89f8721b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 30 Nov 2020 16:51:11 +0700 Subject: [PATCH] [BetterStandardPrinter] Fixes #4691 Donot convert string literal comment (#4727) * [BetterStandardPrinter] Fixes #4691 Donot change convert string literal comment * handle spacing * [ci-review] Rector Rectify * fix conflict * [ci-review] Rector Rectify * try --debug * Trigger notification * phpstan * phpstan Co-authored-by: rector-bot --- .../MakeDispatchFirstArgumentEventRector.php | 9 ++---- .../Manipulator/VisibilityManipulator.php | 28 ++++++++--------- .../Printer/BetterStandardPrinter.php | 4 +-- src/PhpParser/Printer/ContentPatcher.php | 9 +++--- ...tChangeExplicitQuotedStringCommentTest.php | 31 +++++++++++++++++++ .../Fixture/skip_explicit_format.php.inc | 13 ++++++++ .../config/some_config.php | 13 ++++++++ .../src/Rule/ConfigurableRectorRule.php | 4 +-- 8 files changed, 83 insertions(+), 28 deletions(-) create mode 100644 tests/Issues/Issue4691/DoNotChangeExplicitQuotedStringComment/DoNotChangeExplicitQuotedStringCommentTest.php create mode 100644 tests/Issues/Issue4691/DoNotChangeExplicitQuotedStringComment/Fixture/skip_explicit_format.php.inc create mode 100644 tests/Issues/Issue4691/DoNotChangeExplicitQuotedStringComment/config/some_config.php diff --git a/rules/symfony/src/Rector/MethodCall/MakeDispatchFirstArgumentEventRector.php b/rules/symfony/src/Rector/MethodCall/MakeDispatchFirstArgumentEventRector.php index ed2a825203d..e2222104328 100644 --- a/rules/symfony/src/Rector/MethodCall/MakeDispatchFirstArgumentEventRector.php +++ b/rules/symfony/src/Rector/MethodCall/MakeDispatchFirstArgumentEventRector.php @@ -109,15 +109,12 @@ CODE_SAMPLE return $methodCall; } - private function refactorGetCallFuncCall( - MethodCall $methodCall, - FuncCall $funcCall, - Expr $firstArgumentValue - ): ?MethodCall { + private function refactorGetCallFuncCall(MethodCall $methodCall, FuncCall $funcCall, Expr $expr): ?MethodCall + { if ($this->isName($funcCall, 'get_class')) { $getClassArgumentValue = $funcCall->args[0]->value; - if ($this->areNodesEqual($firstArgumentValue, $getClassArgumentValue)) { + if ($this->areNodesEqual($expr, $getClassArgumentValue)) { unset($methodCall->args[1]); return $methodCall; diff --git a/src/PhpParser/Node/Manipulator/VisibilityManipulator.php b/src/PhpParser/Node/Manipulator/VisibilityManipulator.php index ab25617417b..85f2bf504e1 100644 --- a/src/PhpParser/Node/Manipulator/VisibilityManipulator.php +++ b/src/PhpParser/Node/Manipulator/VisibilityManipulator.php @@ -147,20 +147,6 @@ final class VisibilityManipulator $this->replaceVisibilityFlag($node, 'private'); } - /** - * @param ClassMethod|Property|ClassConst $node - */ - private function replaceVisibilityFlag(Node $node, string $visibility): void - { - $visibility = strtolower($visibility); - - if ($visibility !== self::STATIC && $visibility !== self::ABSTRACT && $visibility !== self::FINAL) { - $this->removeOriginalVisibilityFromFlags($node); - } - - $this->addVisibilityFlag($node, $visibility); - } - /** * @param Class_|ClassMethod|Property|ClassConst $node */ @@ -208,4 +194,18 @@ final class VisibilityManipulator get_class($node) )); } + + /** + * @param ClassMethod|Property|ClassConst $node + */ + private function replaceVisibilityFlag(Node $node, string $visibility): void + { + $visibility = strtolower($visibility); + + if ($visibility !== self::STATIC && $visibility !== self::ABSTRACT && $visibility !== self::FINAL) { + $this->removeOriginalVisibilityFromFlags($node); + } + + $this->addVisibilityFlag($node, $visibility); + } } diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index 4caae886c6d..341a94070e7 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -173,8 +173,8 @@ final class BetterStandardPrinter extends Standard $content = $this->contentPatcher->rollbackValidAnnotation( $contentOriginal, $content, - ContentPatcher::VALID_ANNOTATION_RETURN_EXPLICIT_FORMAT_REGEX, - ContentPatcher::INVALID_ANNOTATION_RETURN_EXPLICIT_FORMAT_REGEX + ContentPatcher::VALID_ANNOTATION_VAR_RETURN_EXPLICIT_FORMAT_REGEX, + ContentPatcher::INVALID_ANNOTATION_VAR_RETURN_EXPLICIT_FORMAT_REGEX ); // add new line in case of added stmts diff --git a/src/PhpParser/Printer/ContentPatcher.php b/src/PhpParser/Printer/ContentPatcher.php index df57795e48c..89279e3fbe7 100644 --- a/src/PhpParser/Printer/ContentPatcher.php +++ b/src/PhpParser/Printer/ContentPatcher.php @@ -81,16 +81,16 @@ 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 + * @see https://regex101.com/r/EA1xRY/6 * @var string */ - public const VALID_ANNOTATION_RETURN_EXPLICIT_FORMAT_REGEX = '#^\s{0,}\* @return\s+(\(.*\)|(".*")(\|".*"){1,})$#msU'; + public const VALID_ANNOTATION_VAR_RETURN_EXPLICIT_FORMAT_REGEX = '#\*\s+@(var|return)\s+(\(.*\)|(".*")(\|".*")|("?.*"?){1,})$#msU'; /** - * @see https://regex101.com/r/LprF44/3 + * @see https://regex101.com/r/LprF44/8 * @var string */ - public const INVALID_ANNOTATION_RETURN_EXPLICIT_FORMAT_REGEX = '#^\s{0,}\* @return([^\s].*|\s[^"\s]*)$#msU'; + public const INVALID_ANNOTATION_VAR_RETURN_EXPLICIT_FORMAT_REGEX = '#\*\s+@(var|return)([^\s].*|\s[^"\s]*|([^"]*[^"]))$#msU'; /** * @see https://regex101.com/r/4mBd0y/2 @@ -178,6 +178,7 @@ final class ContentPatcher * @see https://github.com/rectorphp/rector/issues/4476 * @see https://github.com/rectorphp/rector/issues/4620 * @see https://github.com/rectorphp/rector/issues/4652 + * @see https://github.com/rectorphp/rector/issues/4691 */ public function rollbackValidAnnotation( string $originalContent, diff --git a/tests/Issues/Issue4691/DoNotChangeExplicitQuotedStringComment/DoNotChangeExplicitQuotedStringCommentTest.php b/tests/Issues/Issue4691/DoNotChangeExplicitQuotedStringComment/DoNotChangeExplicitQuotedStringCommentTest.php new file mode 100644 index 00000000000..80922ae54d7 --- /dev/null +++ b/tests/Issues/Issue4691/DoNotChangeExplicitQuotedStringComment/DoNotChangeExplicitQuotedStringCommentTest.php @@ -0,0 +1,31 @@ +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'); + } +} diff --git a/tests/Issues/Issue4691/DoNotChangeExplicitQuotedStringComment/Fixture/skip_explicit_format.php.inc b/tests/Issues/Issue4691/DoNotChangeExplicitQuotedStringComment/Fixture/skip_explicit_format.php.inc new file mode 100644 index 00000000000..3a52167c13b --- /dev/null +++ b/tests/Issues/Issue4691/DoNotChangeExplicitQuotedStringComment/Fixture/skip_explicit_format.php.inc @@ -0,0 +1,13 @@ +parameters(); + + $parameters->set(Option::SETS, [SetList::DEAD_CODE]); +}; diff --git a/utils/phpstan-extensions/src/Rule/ConfigurableRectorRule.php b/utils/phpstan-extensions/src/Rule/ConfigurableRectorRule.php index fcb4735309a..a3a890b4789 100644 --- a/utils/phpstan-extensions/src/Rule/ConfigurableRectorRule.php +++ b/utils/phpstan-extensions/src/Rule/ConfigurableRectorRule.php @@ -67,7 +67,7 @@ final class ConfigurableRectorRule implements Rule private function hasRectorInClassName(Class_ $class): bool { - if ($class->namespacedName === null) { + if (! property_exists($class, 'namespacedName') || $class->namespacedName === null) { return false; } @@ -76,7 +76,7 @@ final class ConfigurableRectorRule implements Rule private function implementsConfigurableInterface(Class_ $class): bool { - if ($class->namespacedName === null) { + if (! property_exists($class, 'namespacedName') || $class->namespacedName === null) { return false; }