diff --git a/src/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser.php b/src/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser.php index dfc9cb9c89d..2659ac93947 100644 --- a/src/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser.php +++ b/src/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser.php @@ -4,7 +4,6 @@ namespace Rector\BetterPhpDocParser\PhpDocParser; -use Nette\Utils\Strings; use PhpParser\Node; use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode; use PHPStan\PhpDocParser\Lexer\Lexer; @@ -15,6 +14,7 @@ use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\PlainValueParser; use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator; use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode; +use Rector\Util\NewLineSplitter; /** * Better version of doctrine/annotation - with phpdoc-parser and static reflection @@ -22,12 +22,6 @@ */ final readonly class StaticDoctrineAnnotationParser { - /** - * @var string - * @see https://regex101.com/r/aU2knc/1 - */ - private const NEWLINES_REGEX = "#\r?\n#"; - /** * @var string * @see https://regex101.com/r/Pthg5d/1 @@ -107,7 +101,7 @@ public function getCommentFromRestOfAnnotation( // the remaining of the annotation content is the comment $comment = substr($annotationContent, $tokenIterator->currentTokenOffset()); // we only keep the first line as this will be added as a line comment at the end of the attribute - $commentLines = Strings::split($comment, self::NEWLINES_REGEX); + $commentLines = NewLineSplitter::split($comment); return $commentLines[0]; } diff --git a/src/Console/Formatter/ColorConsoleDiffFormatter.php b/src/Console/Formatter/ColorConsoleDiffFormatter.php index b81194b694d..79d975f8425 100644 --- a/src/Console/Formatter/ColorConsoleDiffFormatter.php +++ b/src/Console/Formatter/ColorConsoleDiffFormatter.php @@ -5,6 +5,7 @@ namespace Rector\Console\Formatter; use Nette\Utils\Strings; +use Rector\Util\NewLineSplitter; use Symfony\Component\Console\Formatter\OutputFormatter; /** @@ -33,12 +34,6 @@ */ private const AT_START_REGEX = '#^(@.*)#'; - /** - * @var string - * @see https://regex101.com/r/qduj2O/1 - */ - private const NEWLINES_REGEX = "#\n\r|\n#"; - private string $template; public function __construct() @@ -59,7 +54,7 @@ private function formatWithTemplate(string $diff, string $template): string { $escapedDiff = OutputFormatter::escape(rtrim($diff)); - $escapedDiffLines = Strings::split($escapedDiff, self::NEWLINES_REGEX); + $escapedDiffLines = NewLineSplitter::split($escapedDiff); // remove description of added + remove; obvious on diffs foreach ($escapedDiffLines as $key => $escapedDiffLine) { diff --git a/src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php b/src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php index 5498528bf88..93268a1a1fb 100644 --- a/src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php +++ b/src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php @@ -92,6 +92,7 @@ public function create( if ($comment) { $attributeGroup->setAttribute(AttributeKey::ATTRIBUTE_COMMENT, $comment); } + return $attributeGroup; } diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index 9d23ec1395c..274d00f86f2 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -4,6 +4,7 @@ namespace Rector\PhpParser\Printer; +use PhpParser\Node\AttributeGroup; use Nette\Utils\Strings; use PhpParser\Comment; use PhpParser\Node; @@ -139,13 +140,14 @@ protected function p(Node $node, $parentFormatPreserved = false): string : $content; } - protected function pAttributeGroup(Node\AttributeGroup $node): string + protected function pAttributeGroup(AttributeGroup $attributeGroup): string { - $ret = parent::pAttributeGroup($node); - $comment = $node->getAttribute(AttributeKey::ATTRIBUTE_COMMENT); + $ret = parent::pAttributeGroup($attributeGroup); + $comment = $attributeGroup->getAttribute(AttributeKey::ATTRIBUTE_COMMENT); if (! in_array($comment, ['', null], true)) { $ret .= ' // ' . $comment; } + return $ret; } diff --git a/src/Util/NewLineSplitter.php b/src/Util/NewLineSplitter.php new file mode 100644 index 00000000000..3b3ef1856f6 --- /dev/null +++ b/src/Util/NewLineSplitter.php @@ -0,0 +1,24 @@ +