2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-09-26 11:43:26 +00:00
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\FuncCall;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2019-10-02 22:32:59 +00:00
|
|
|
use PhpParser\Node\Scalar\String_;
|
2019-09-26 11:43:26 +00:00
|
|
|
use PHPStan\Type\StringType;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-26 11:43:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\FuncCall\RemoveSoleValueSprintfRector\RemoveSoleValueSprintfRectorTest
|
|
|
|
*/
|
|
|
|
final class RemoveSoleValueSprintfRector extends AbstractRector
|
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2019-09-26 11:43:26 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition('Remove sprintf() wrapper if not needed', [
|
2019-09-26 11:43:26 +00:00
|
|
|
new CodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2019-09-26 11:43:26 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$value = sprintf('%s', 'hi');
|
|
|
|
|
|
|
|
$welcome = 'hello';
|
|
|
|
$value = sprintf('%s', $welcome);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2019-09-26 11:43:26 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2019-09-26 11:43:26 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$value = 'hi';
|
|
|
|
|
|
|
|
$welcome = 'hello';
|
|
|
|
$value = $welcome;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2019-09-26 11:43:26 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-09-26 11:43:26 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [FuncCall::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if (! $this->isName($node, 'sprintf')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-25 00:22:45 +00:00
|
|
|
if (count($node->args) !== 2) {
|
2019-09-26 11:43:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$maskArgument = $node->args[0]->value;
|
2019-10-02 22:32:59 +00:00
|
|
|
if (! $maskArgument instanceof String_) {
|
2019-09-26 11:43:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($maskArgument->value !== '%s') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$valueArgument = $node->args[1]->value;
|
2021-02-28 07:47:48 +00:00
|
|
|
if (! $this->nodeTypeResolver->isStaticType($valueArgument, StringType::class)) {
|
2019-09-26 11:43:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $valueArgument;
|
|
|
|
}
|
|
|
|
}
|