Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ parameters:
- '#Class "Rector\\Php70\\Rector\\StaticCall\\StaticCallOnNonStaticToInstanceCallRector" is missing @see annotation with test case class reference#'
- '#Class "Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictStringParamConcatRector" is missing @see annotation with test case class reference#'
- '#Class "Rector\\CodingStyle\\Rector\\Enum_\\EnumCaseToPascalCaseRector" is missing @see annotation with test case class reference#'
- '#Class "Rector\\CodeQuality\\Rector\\Concat\\JoinStringConcatRector" is missing @see annotation with test case class reference#'

# false positive
-
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

58 changes: 8 additions & 50 deletions rules/CodeQuality/Rector/Concat/JoinStringConcatRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,19 @@

namespace Rector\CodeQuality\Rector\Concat;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\String_;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\Util\StringUtils;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodeQuality\Rector\Concat\JoinStringConcatRector\JoinStringConcatRectorTest
* @deprecated as depends on context and personal preference, hard to generalize. Handle it manually or via a custom rule instead.
*/
final class JoinStringConcatRector extends AbstractRector
final class JoinStringConcatRector extends AbstractRector implements DeprecatedInterface
{
private const int LINE_BREAK_POINT = 100;

/**
* @see https://regex101.com/r/VaXM1t/1
* @see https://stackoverflow.com/questions/4147646/determine-if-utf-8-text-is-all-ascii
*/
private const string ASCII_REGEX = '#[^\x00-\x7F]#';

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -69,42 +60,9 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $node->left instanceof String_) {
return null;
}

if (! $node->right instanceof String_) {
return null;
}

$leftStartLine = $node->left->getStartLine();
$rightStartLine = $node->right->getStartLine();

if ($leftStartLine > 0 && $rightStartLine > 0 && $rightStartLine > $leftStartLine) {
return null;
}

return $this->joinConcatIfStrings($node->left, $node->right);
}

private function joinConcatIfStrings(String_ $leftString, String_ $rightString): ?String_
{
$leftValue = $leftString->value;
$rightValue = $rightString->value;

if (str_contains($leftValue, "\n") || str_contains($rightValue, "\n")) {
return null;
}

$joinedStringValue = $leftValue . $rightValue;
if (StringUtils::isMatch($joinedStringValue, self::ASCII_REGEX)) {
return null;
}

if (Strings::length($joinedStringValue) >= self::LINE_BREAK_POINT) {
return null;
}

return new String_($joinedStringValue);
throw new ShouldNotHappenException(sprintf(
'"%s" is deprecated as depends on context and personal preference, hard to generalize. Handle it manually or via a custom rule instead',
self::class
));
}
}
2 changes: 0 additions & 2 deletions src/Config/Level/CodeQualityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Rector\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector;
use Rector\CodeQuality\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector;
use Rector\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector;
use Rector\CodeQuality\Rector\Concat\JoinStringConcatRector;
use Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector;
use Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector;
use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector;
Expand Down Expand Up @@ -138,7 +137,6 @@ final class CodeQualityLevel
SingleInArrayToCompareRector::class,
SimplifyIfElseToTernaryRector::class,
TernaryImplodeToImplodeRector::class,
JoinStringConcatRector::class,
ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class,
ExplicitBoolCompareRector::class,
CombineIfRector::class,
Expand Down
Loading