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 config/set/code-quality/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ services:
Rector\CodeQuality\Rector\FuncCall\StrlenZeroToIdenticalEmptyStringRector: ~
Rector\CodeQuality\Rector\If_\RemoveAlwaysTrueConditionSetInConstructorRector: ~
Rector\CodeQuality\Rector\Catch_\ThrowWithPreviousExceptionRector: ~
Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php declare(strict_types=1);

namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Type\StringType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

/**
* @see \Rector\CodeQuality\Tests\Rector\FuncCall\RemoveSoleValueSprintfRector\RemoveSoleValueSprintfRectorTest
*/
final class RemoveSoleValueSprintfRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Remove sprintf() wrapper if not needed', [
new CodeSample(
<<<'PHP'
class SomeClass
{
public function run()
{
$value = sprintf('%s', 'hi');

$welcome = 'hello';
$value = sprintf('%s', $welcome);
}
}
PHP
,
<<<'PHP'
class SomeClass
{
public function run()
{
$value = 'hi';

$welcome = 'hello';
$value = $welcome;
}
}
PHP
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'sprintf')) {
return null;
}

if (count($node->args) !== 2) {
return null;
}

$maskArgument = $node->args[0]->value;
if (! $maskArgument instanceof Node\Scalar\String_) {
return null;
}

if ($maskArgument->value !== '%s') {
return null;
}

$valueArgument = $node->args[1]->value;
if (! $this->isStaticType($valueArgument, StringType::class)) {
return null;
}

return $valueArgument;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\CodeQuality\Tests\Rector\FuncCall\RemoveSoleValueSprintfRector\Fixture;

class SomeClass
{
public function run()
{
$value = sprintf('%s', 'hi');

$welcome = 'hello';
$value = sprintf('%s', $welcome);
}
}

?>
-----
<?php

namespace Rector\CodeQuality\Tests\Rector\FuncCall\RemoveSoleValueSprintfRector\Fixture;

class SomeClass
{
public function run()
{
$value = 'hi';

$welcome = 'hello';
$value = $welcome;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace Rector\CodeQuality\Tests\Rector\FuncCall\RemoveSoleValueSprintfRector;

use Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveSoleValueSprintfRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

/**
* @return string[]
*/
public function provideDataForTest(): iterable
{
yield [__DIR__ . '/Fixture/fixture.php.inc'];
}

protected function getRectorClass(): string
{
return RemoveSoleValueSprintfRector::class;
}
}