Skip to content

Commit

Permalink
[Util] Make consistent new line splitter to Util (#5651)
Browse files Browse the repository at this point in the history
* [Util] Make consistent new line splitter to Util

* [Util] Make consistent new line splitter to Util
  • Loading branch information
samsonasik committed Feb 21, 2024
1 parent 7e741fe commit 290926c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
Expand Up @@ -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;
Expand All @@ -15,19 +14,14 @@
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
* @see \Rector\Tests\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\StaticDoctrineAnnotationParserTest
*/
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
Expand Down Expand Up @@ -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];
}

Expand Down
9 changes: 2 additions & 7 deletions src/Console/Formatter/ColorConsoleDiffFormatter.php
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Console\Formatter;

use Nette\Utils\Strings;
use Rector\Util\NewLineSplitter;
use Symfony\Component\Console\Formatter\OutputFormatter;

/**
Expand Down Expand Up @@ -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()
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php
Expand Up @@ -92,6 +92,7 @@ public function create(
if ($comment) {
$attributeGroup->setAttribute(AttributeKey::ATTRIBUTE_COMMENT, $comment);
}

return $attributeGroup;
}

Expand Down
8 changes: 5 additions & 3 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Expand Up @@ -4,6 +4,7 @@

namespace Rector\PhpParser\Printer;

use PhpParser\Node\AttributeGroup;
use Nette\Utils\Strings;
use PhpParser\Comment;
use PhpParser\Node;
Expand Down Expand Up @@ -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;
}

Expand Down
24 changes: 24 additions & 0 deletions src/Util/NewLineSplitter.php
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Rector\Util;

use Nette\Utils\Strings;

final class NewLineSplitter
{
/**
* @var string
* @see https://regex101.com/r/qduj2O/4
*/
private const NEWLINES_REGEX = "#\r?\n#";

/**
* @return string[]
*/
public static function split(string $content): array
{
return Strings::split($content, self::NEWLINES_REGEX);
}
}

0 comments on commit 290926c

Please sign in to comment.