mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-20 08:05:29 +01:00
[CodeQuality] Add IntvalToTypeCastRector (#2510)
[CodeQuality] Add IntvalToTypeCastRector
This commit is contained in:
commit
b711990463
@ -45,3 +45,4 @@ services:
|
||||
Rector\SOLID\Rector\ClassMethod\UseInterfaceOverImplementationInConstructorRector: ~
|
||||
Rector\CodeQuality\Rector\FuncCall\AddPregQuoteDelimiterRector: ~
|
||||
Rector\CodeQuality\Rector\FuncCall\ArrayMergeOfNonArraysToSimpleArrayRector: ~
|
||||
Rector\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector: ~
|
||||
|
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Rector\FuncCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Cast\Int_;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see https://github.com/kalessil/phpinspectionsea/commit/25f53c8c7e08234c34b0d21f308f7c5cbd7a6c95
|
||||
* @see https://www.php.net/manual/en/function.intval.php
|
||||
*
|
||||
* @see \Rector\CodeQuality\Tests\Rector\FuncCall\IntvalToTypeCastRector\IntvalToTypeCastRectorTest
|
||||
*/
|
||||
final class IntvalToTypeCastRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Change intval() to faster and readable (int) $value', [
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run($value)
|
||||
{
|
||||
return intval($value);
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run($value)
|
||||
{
|
||||
return (int) $value;
|
||||
}
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [FuncCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FuncCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isName($node, 'intval')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isset($node->args[1])) {
|
||||
$secondArgumentValue = $this->getValue($node->args[1]->value);
|
||||
// default value
|
||||
if ($secondArgumentValue !== 10) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return new Int_($node->args[0]->value);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\FuncCall\IntvalToTypeCastRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run($value)
|
||||
{
|
||||
$value = intval($value);
|
||||
|
||||
$value = intval($value, 10);
|
||||
|
||||
$value = intval($value, 8);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\FuncCall\IntvalToTypeCastRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run($value)
|
||||
{
|
||||
$value = (int) $value;
|
||||
|
||||
$value = (int) $value;
|
||||
|
||||
$value = intval($value, 8);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\FuncCall\IntvalToTypeCastRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class IntvalToTypeCastRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideDataForTest()
|
||||
*/
|
||||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
}
|
||||
|
||||
public function provideDataForTest(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return IntvalToTypeCastRector::class;
|
||||
}
|
||||
}
|
@ -67,18 +67,14 @@ final class TemplateVariablesFactory
|
||||
return '';
|
||||
}
|
||||
|
||||
$sourceDocBlock = <<<'PHP'
|
||||
/**
|
||||
%s
|
||||
*/
|
||||
PHP;
|
||||
|
||||
$sourceAsString = '';
|
||||
foreach ($source as $singleSource) {
|
||||
$sourceAsString .= ' * @see ' . $singleSource . PHP_EOL;
|
||||
}
|
||||
|
||||
return sprintf($sourceDocBlock, rtrim($sourceAsString));
|
||||
$sourceAsString .= ' *';
|
||||
|
||||
return rtrim($sourceAsString);
|
||||
}
|
||||
|
||||
private function createNodeTypePhp(Configuration $configuration): string
|
||||
|
@ -9,9 +9,8 @@ use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
_Source_
|
||||
|
||||
/**
|
||||
_Source_
|
||||
* @see \Rector\_Package_\Tests\Rector\_Category_\_Name_\_Name_Test
|
||||
*/
|
||||
final class _Name_ extends AbstractRector
|
||||
|
Loading…
x
Reference in New Issue
Block a user