mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-08 04:10:51 +00:00
[Carbon 2] Init (#4595)
* [Carbon] Init new rules * add ChangeDiffForHumansArgsRector * [Carbon] Add ChangeCarbonSingularMethodCallToPluralRector * [ci-review] Rector Rectify Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
parent
26efdc6622
commit
7c3e803416
|
@ -1,29 +1,14 @@
|
|||
{
|
||||
"name": "rector/rector",
|
||||
"description": "Instant upgrade and refactoring of your PHP code",
|
||||
"keywords": [
|
||||
"instant upgrades",
|
||||
"instant refactoring",
|
||||
"ast",
|
||||
"automated refactoring"
|
||||
],
|
||||
"keywords": ["instant upgrades", "instant refactoring", "ast", "automated refactoring"],
|
||||
"homepage": "https://getrector.org",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Tomas Votruba",
|
||||
"email": "tomas.vot@gmail.com",
|
||||
"homepage": "https://tomasvotruba.com"
|
||||
},
|
||||
{
|
||||
"name": "Jan Mikes",
|
||||
"email": "j.mikes@me.com",
|
||||
"homepage": "https://janmikes.cz"
|
||||
}
|
||||
],
|
||||
"bin": [
|
||||
"bin/rector"
|
||||
{ "name": "Tomas Votruba", "email": "tomas.vot@gmail.com", "homepage": "https://tomasvotruba.com" },
|
||||
{ "name": "Jan Mikes", "email": "j.mikes@me.com", "homepage": "https://janmikes.cz" }
|
||||
],
|
||||
"bin": ["bin/rector"],
|
||||
"require": {
|
||||
"php": "^7.2.4|^8.0",
|
||||
"ext-json": "*",
|
||||
|
@ -179,7 +164,8 @@
|
|||
"Rector\\Transform\\": "rules/transform/src",
|
||||
"Rector\\Twig\\": "rules/twig/src",
|
||||
"Rector\\TypeDeclaration\\": "rules/type-declaration/src",
|
||||
"Rector\\VendorLocker\\": "packages/vendor-locker/src"
|
||||
"Rector\\VendorLocker\\": "packages/vendor-locker/src",
|
||||
"Rector\\Carbon\\": "rules/carbon/src"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
|
@ -303,7 +289,8 @@
|
|||
"Rector\\Utils\\NodeDocumentationGenerator\\Tests\\": "utils/node-documentation-generator/tests",
|
||||
"Rector\\Utils\\PHPStanAttributeTypeSyncer\\": "utils/phpstan-attribute-type-syncer/src",
|
||||
"Rector\\Utils\\PHPStanStaticTypeMapperChecker\\": "utils/phpstan-static-type-mapper-checker/src",
|
||||
"Rector\\Utils\\ProjectValidator\\": "utils/project-validator/src"
|
||||
"Rector\\Utils\\ProjectValidator\\": "utils/project-validator/src",
|
||||
"Rector\\Carbon\\Tests\\": "rules/carbon/tests"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
|
|
14
config/set/carbon-2.php
Normal file
14
config/set/carbon-2.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Carbon\Rector\MethodCall\ChangeCarbonSingularMethodCallToPluralRector;
|
||||
use Rector\Carbon\Rector\MethodCall\ChangeDiffForHumansArgsRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
# source: https://carbon.nesbot.com/docs/#api-carbon-2
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(ChangeDiffForHumansArgsRector::class);
|
||||
$services->set(ChangeCarbonSingularMethodCallToPluralRector::class);
|
||||
};
|
|
@ -725,4 +725,9 @@ final class SetList
|
|||
* @var string
|
||||
*/
|
||||
public const EARLY_RETURN = __DIR__ . '/../../../../config/set/early-return.php';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const CARBON_2 = __DIR__ . '/../../../../config/set/carbon-2.php';
|
||||
}
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Carbon\Rector\MethodCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see https://carbon.nesbot.com/docs/#api-carbon-2
|
||||
*
|
||||
* @see \Rector\Carbon\Tests\Rector\MethodCall\ChangeCarbonSingularMethodCallToPluralRector\ChangeCarbonSingularMethodCallToPluralRectorTest
|
||||
*/
|
||||
final class ChangeCarbonSingularMethodCallToPluralRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private const SINGULAR_TO_PLURAL_NAMES = [
|
||||
'addSecond' => 'addSeconds',
|
||||
'addMinute' => 'addMinutes',
|
||||
'addDay' => 'addDays',
|
||||
'addHour' => 'addHours',
|
||||
'addWeek' => 'addWeeks',
|
||||
'addMonth' => 'addMonths',
|
||||
'addYear' => 'addYears',
|
||||
];
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Change setter methods with args to their plural names on Carbon\Carbon', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
use Carbon\Carbon;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(Carbon $carbon, $value): void
|
||||
{
|
||||
$carbon->addMinute($value);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
use Carbon\Carbon;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(Carbon $carbon, $value): void
|
||||
{
|
||||
$carbon->addMinutes($value);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [MethodCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ((array) $node->args === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (self::SINGULAR_TO_PLURAL_NAMES as $singularName => $pluralName) {
|
||||
if (! $this->isName($node->name, $singularName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$node->name = new Identifier($pluralName);
|
||||
return $node;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Carbon\Rector\MethodCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see https://carbon.nesbot.com/docs/#api-carbon-2
|
||||
*
|
||||
* @see \Rector\Carbon\Tests\Rector\MethodCall\ChangeDiffForHumansArgsRector\ChangeDiffForHumansArgsRectorTest
|
||||
*/
|
||||
final class ChangeDiffForHumansArgsRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Change methods arguments of diffForHumans() on Carbon\Carbon', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
use Carbon\Carbon;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(Carbon $carbon): void
|
||||
{
|
||||
$carbon->diffForHumans(null, true);
|
||||
|
||||
$carbon->diffForHumans(null, false);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
use Carbon\Carbon;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(Carbon $carbon): void
|
||||
{
|
||||
$carbon->diffForHumans(null, \Carbon\CarbonInterface::DIFF_ABSOLUTE);
|
||||
|
||||
$carbon->diffForHumans(null, \Carbon\CarbonInterface::DIFF_RELATIVE_AUTO);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [MethodCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isOnClassMethodCall($node, 'Carbon\Carbon', 'diffForHumans')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! isset($node->args[1])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$secondArgValue = $node->args[1]->value;
|
||||
if ($this->isTrue($secondArgValue)) {
|
||||
$node->args[1]->value = $this->createClassConstFetch('Carbon\CarbonInterface', 'DIFF_ABSOLUTE');
|
||||
return $node;
|
||||
}
|
||||
|
||||
if ($this->isFalse($secondArgValue)) {
|
||||
$node->args[1]->value = $this->createClassConstFetch('Carbon\CarbonInterface', 'DIFF_RELATIVE_AUTO');
|
||||
return $node;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Carbon\Tests\Rector\MethodCall\ChangeCarbonSingularMethodCallToPluralRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Carbon\Rector\MethodCall\ChangeCarbonSingularMethodCallToPluralRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class ChangeCarbonSingularMethodCallToPluralRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return ChangeCarbonSingularMethodCallToPluralRector::class;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Carbon\Tests\Rector\MethodCall\ChangeCarbonSingularMethodCallToPluralRector\Fixture;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(Carbon $carbon, $value): void
|
||||
{
|
||||
$carbon->addMinute($value);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Carbon\Tests\Rector\MethodCall\ChangeCarbonSingularMethodCallToPluralRector\Fixture;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(Carbon $carbon, $value): void
|
||||
{
|
||||
$carbon->addMinutes($value);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Carbon\Tests\Rector\MethodCall\ChangeCarbonSingularMethodCallToPluralRector\Fixture;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
final class SkipNoValue
|
||||
{
|
||||
public function run(Carbon $carbon, $value): void
|
||||
{
|
||||
$carbon->addMinute();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Carbon\Tests\Rector\MethodCall\ChangeDiffForHumansArgsRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Carbon\Rector\MethodCall\ChangeDiffForHumansArgsRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class ChangeDiffForHumansArgsRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return ChangeDiffForHumansArgsRector::class;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Carbon\Tests\Rector\MethodCall\ChangeDiffForHumansArgsRector\Fixture;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(Carbon $carbon): void
|
||||
{
|
||||
$carbon->diffForHumans(null, true);
|
||||
|
||||
$carbon->diffForHumans(null, false);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Carbon\Tests\Rector\MethodCall\ChangeDiffForHumansArgsRector\Fixture;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(Carbon $carbon): void
|
||||
{
|
||||
$carbon->diffForHumans(null, \Carbon\CarbonInterface::DIFF_ABSOLUTE);
|
||||
|
||||
$carbon->diffForHumans(null, \Carbon\CarbonInterface::DIFF_RELATIVE_AUTO);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user