2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodingStyle\Rector\Encapsed;
|
2019-07-10 07:35:03 +00:00
|
|
|
|
2024-05-02 02:21:56 +00:00
|
|
|
use RectorPrefix202405\Nette\Utils\Strings;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Concat;
|
|
|
|
use PhpParser\Node\Expr\ConstFetch;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Scalar\Encapsed;
|
|
|
|
use PhpParser\Node\Scalar\EncapsedStringPart;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-07-10 07:35:03 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector\EncapsedStringsToSprintfRectorTest
|
2019-07-10 07:35:03 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class EncapsedStringsToSprintfRector extends AbstractRector
|
2019-07-10 07:35:03 +00:00
|
|
|
{
|
2021-08-08 17:03:07 +00:00
|
|
|
/**
|
|
|
|
* @var array<string, array<class-string<Type>>>
|
|
|
|
*/
|
|
|
|
private const FORMAT_SPECIFIERS = ['%s' => ['PHPStan\\Type\\StringType'], '%d' => ['PHPStan\\Type\\Constant\\ConstantIntegerType', 'PHPStan\\Type\\IntegerRangeType', 'PHPStan\\Type\\IntegerType']];
|
2019-11-07 23:02:26 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-05-12 13:15:45 +00:00
|
|
|
private $sprintfFormat = '';
|
2019-11-07 23:02:26 +00:00
|
|
|
/**
|
|
|
|
* @var Expr[]
|
|
|
|
*/
|
|
|
|
private $argumentVariables = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-07-10 07:35:03 +00:00
|
|
|
{
|
2023-03-24 08:09:24 +00:00
|
|
|
return new RuleDefinition('Convert enscaped {$string} to more readable sprintf or concat, if no mask is used', [new CodeSample(<<<'CODE_SAMPLE'
|
2023-09-02 15:36:16 +00:00
|
|
|
echo "Unsupported format {$format} - use another";
|
|
|
|
|
|
|
|
echo "Try {$allowed}";
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2023-09-02 15:36:16 +00:00
|
|
|
echo sprintf('Unsupported format %s - use another', $format);
|
|
|
|
|
|
|
|
echo 'Try ' . $allowed;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-07-10 07:35:03 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-07-10 07:35:03 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-07-10 07:35:03 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Encapsed::class];
|
2019-07-10 07:35:03 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Encapsed $node
|
2019-07-10 07:35:03 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-07-10 07:35:03 +00:00
|
|
|
{
|
2022-08-20 13:58:40 +00:00
|
|
|
if ($this->shouldSkip($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-11-07 23:02:26 +00:00
|
|
|
$this->sprintfFormat = '';
|
|
|
|
$this->argumentVariables = [];
|
2019-07-10 07:35:03 +00:00
|
|
|
foreach ($node->parts as $part) {
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($part instanceof EncapsedStringPart) {
|
2019-11-07 23:02:26 +00:00
|
|
|
$this->collectEncapsedStringPart($part);
|
2021-11-28 18:05:13 +00:00
|
|
|
} else {
|
2019-11-07 23:02:26 +00:00
|
|
|
$this->collectExpr($part);
|
2019-07-10 07:35:03 +00:00
|
|
|
}
|
2019-11-07 23:02:26 +00:00
|
|
|
}
|
|
|
|
return $this->createSprintfFuncCallOrConcat($this->sprintfFormat, $this->argumentVariables);
|
|
|
|
}
|
2022-08-20 13:58:40 +00:00
|
|
|
private function shouldSkip(Encapsed $encapsed) : bool
|
|
|
|
{
|
2022-12-23 08:45:28 +00:00
|
|
|
return $encapsed->hasAttribute(AttributeKey::DOC_LABEL);
|
2022-08-20 13:58:40 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function collectEncapsedStringPart(EncapsedStringPart $encapsedStringPart) : void
|
2019-11-07 23:02:26 +00:00
|
|
|
{
|
|
|
|
$stringValue = $encapsedStringPart->value;
|
|
|
|
if ($stringValue === "\n") {
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->argumentVariables[] = new ConstFetch(new Name('PHP_EOL'));
|
2021-01-02 21:59:58 +00:00
|
|
|
$this->sprintfFormat .= '%s';
|
2019-11-07 23:02:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->sprintfFormat .= Strings::replace($stringValue, '#%#', '%%');
|
2019-11-07 23:02:26 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function collectExpr(Expr $expr) : void
|
2019-11-07 23:02:26 +00:00
|
|
|
{
|
2021-10-07 19:40:12 +00:00
|
|
|
$type = $this->nodeTypeResolver->getType($expr);
|
2021-08-08 17:03:07 +00:00
|
|
|
$found = \false;
|
|
|
|
foreach (self::FORMAT_SPECIFIERS as $key => $types) {
|
|
|
|
if (\in_array(\get_class($type), $types, \true)) {
|
|
|
|
$this->sprintfFormat .= $key;
|
|
|
|
$found = \true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$found) {
|
|
|
|
$this->sprintfFormat .= '%s';
|
|
|
|
}
|
2019-11-07 23:02:26 +00:00
|
|
|
// remove: ${wrap} → $wrap
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($expr instanceof Variable) {
|
|
|
|
$expr->setAttribute(AttributeKey::ORIGINAL_NODE, null);
|
2019-11-07 23:02:26 +00:00
|
|
|
}
|
|
|
|
$this->argumentVariables[] = $expr;
|
|
|
|
}
|
2020-02-01 16:04:38 +00:00
|
|
|
/**
|
|
|
|
* @param Expr[] $argumentVariables
|
2023-03-24 08:09:24 +00:00
|
|
|
* @return \PhpParser\Node\Expr\BinaryOp\Concat|\PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr|null
|
2020-02-01 16:04:38 +00:00
|
|
|
*/
|
2023-03-24 08:09:24 +00:00
|
|
|
private function createSprintfFuncCallOrConcat(string $mask, array $argumentVariables)
|
2020-02-01 16:04:38 +00:00
|
|
|
{
|
2023-03-24 08:09:24 +00:00
|
|
|
$bareMask = \str_repeat('%s', \count($argumentVariables));
|
|
|
|
if ($mask === $bareMask) {
|
|
|
|
if (\count($argumentVariables) === 1) {
|
|
|
|
return $argumentVariables[0];
|
|
|
|
}
|
|
|
|
return $this->nodeFactory->createConcat($argumentVariables);
|
2020-02-01 16:04:38 +00:00
|
|
|
}
|
2023-09-02 15:36:16 +00:00
|
|
|
$singleValueConcat = $this->createSingleValueEdgeConcat($argumentVariables, $mask);
|
|
|
|
if ($singleValueConcat instanceof Concat) {
|
|
|
|
return $singleValueConcat;
|
|
|
|
}
|
2021-08-16 07:19:49 +00:00
|
|
|
// checks for windows or linux line ending. \n is contained in both.
|
2023-03-24 08:09:24 +00:00
|
|
|
if (\strpos($mask, "\n") !== \false) {
|
2021-01-02 21:59:58 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-03-24 08:09:24 +00:00
|
|
|
$arguments = [new Arg(new String_($mask))];
|
2020-02-01 16:04:38 +00:00
|
|
|
foreach ($argumentVariables as $argumentVariable) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$arguments[] = new Arg($argumentVariable);
|
2020-02-01 16:04:38 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new FuncCall(new Name('sprintf'), $arguments);
|
2020-02-01 16:04:38 +00:00
|
|
|
}
|
2023-09-02 15:36:16 +00:00
|
|
|
/**
|
|
|
|
* @param Expr[] $argumentVariables
|
|
|
|
*/
|
|
|
|
private function createSingleValueEdgeConcat(array $argumentVariables, string $mask) : ?Concat
|
|
|
|
{
|
|
|
|
if (\count($argumentVariables) !== 1) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-09-02 15:54:43 +00:00
|
|
|
if (\substr_count($mask, '%s') !== 1 && \substr_count($mask, '%d') !== 1) {
|
2023-09-02 15:36:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$cleanMask = Strings::replace($mask, '#\\%\\%#', '%');
|
2023-09-02 15:54:43 +00:00
|
|
|
if (\substr_compare($mask, '%s', -\strlen('%s')) === 0 || \substr_compare($mask, '%d', -\strlen('%d')) === 0) {
|
2023-09-02 15:36:16 +00:00
|
|
|
$bareString = new String_(\substr($cleanMask, 0, -2));
|
|
|
|
return new Concat($bareString, $argumentVariables[0]);
|
|
|
|
}
|
2023-09-02 15:54:43 +00:00
|
|
|
if (\strncmp($mask, '%s', \strlen('%s')) === 0 || \strncmp($mask, '%d', \strlen('%d')) === 0) {
|
2023-09-02 15:36:16 +00:00
|
|
|
$bareString = new String_(\substr($cleanMask, 2));
|
|
|
|
return new Concat($argumentVariables[0], $bareString);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2019-07-10 07:35:03 +00:00
|
|
|
}
|