mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-21 07:52:01 +02:00
Updated Rector to commit 575921d2bca4a0ef36d55c6fa4ebf1ea01bacc4d
575921d2bc
[Carbon] Init set to migrate DateTime to Carbon (#5868)
This commit is contained in:
parent
7ac6f7bf7e
commit
b98d0a8754
12
config/set/datetime-to-carbon.php
Normal file
12
config/set/datetime-to-carbon.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector;
|
||||
use Rector\Carbon\Rector\MethodCall\DateTimeMethodCallToCarbonRector;
|
||||
use Rector\Carbon\Rector\New_\DateTimeInstanceToCarbonRector;
|
||||
use Rector\Config\RectorConfig;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->rules([DateFuncCallToCarbonRector::class, DateTimeInstanceToCarbonRector::class, DateTimeMethodCallToCarbonRector::class]);
|
||||
};
|
@ -1,4 +1,4 @@
|
||||
# 371 Rules Overview
|
||||
# 374 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
@ -6,6 +6,8 @@
|
||||
|
||||
- [Arguments](#arguments) (4)
|
||||
|
||||
- [Carbon](#carbon) (3)
|
||||
|
||||
- [CodeQuality](#codequality) (75)
|
||||
|
||||
- [CodingStyle](#codingstyle) (28)
|
||||
@ -142,6 +144,59 @@ Replaces defined map of arguments in defined methods and their calls.
|
||||
|
||||
<br>
|
||||
|
||||
## Carbon
|
||||
|
||||
### DateFuncCallToCarbonRector
|
||||
|
||||
Convert `date()` function call to Carbon::*()
|
||||
|
||||
- class: [`Rector\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector`](../rules/Carbon/Rector/FuncCall/DateFuncCallToCarbonRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- $date = date('Y-m-d');
|
||||
+ $date = \Carbon\Carbon::now()->format('Y-m-d')
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### DateTimeInstanceToCarbonRector
|
||||
|
||||
Convert new `DateTime()` to Carbon::*()
|
||||
|
||||
- class: [`Rector\Carbon\Rector\New_\DateTimeInstanceToCarbonRector`](../rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php)
|
||||
|
||||
```diff
|
||||
-$date = new \DateTime('today');
|
||||
+$date = \Carbon\Carbon::today();
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### DateTimeMethodCallToCarbonRector
|
||||
|
||||
Convert new `DateTime()` with a method call to Carbon::*()
|
||||
|
||||
- class: [`Rector\Carbon\Rector\MethodCall\DateTimeMethodCallToCarbonRector`](../rules/Carbon/Rector/MethodCall/DateTimeMethodCallToCarbonRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- $date = (new \DateTime('today +20 day'))->format('Y-m-d');
|
||||
+ $date = \Carbon\Carbon::today()->addDays(20)->format('Y-m-d')
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## CodeQuality
|
||||
|
||||
### AbsolutizeRequireAndIncludePathRector
|
||||
|
58
rules/Carbon/NodeFactory/CarbonCallFactory.php
Normal file
58
rules/Carbon/NodeFactory/CarbonCallFactory.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Carbon\NodeFactory;
|
||||
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Scalar\LNumber;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
final class CarbonCallFactory
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
* @see https://regex101.com/r/9vGt8r/1
|
||||
*/
|
||||
private const DAY_COUNT_REGEX = '#\\+(\\s+)?(?<count>\\d+)(\\s+)?(day|days)#';
|
||||
/**
|
||||
* @var string
|
||||
* @see https://regex101.com/r/6VUUQF/1
|
||||
*/
|
||||
private const MONTH_COUNT_REGEX = '#\\+(\\s+)?(?<count>\\d+)(\\s+)?(month|months)#';
|
||||
/**
|
||||
* @var array<self::*_REGEX, string>
|
||||
*/
|
||||
private const REGEX_TO_METHOD_NAME_MAP = [self::DAY_COUNT_REGEX => 'addDays', self::MONTH_COUNT_REGEX => 'addMonths'];
|
||||
/**
|
||||
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall
|
||||
*/
|
||||
public function createFromDateTimeString(FullyQualified $carbonFullyQualified, String_ $string)
|
||||
{
|
||||
$dateTimeValue = $string->value;
|
||||
if ($dateTimeValue === 'now') {
|
||||
return new StaticCall($carbonFullyQualified, new Identifier('now'));
|
||||
}
|
||||
if ($dateTimeValue === 'today') {
|
||||
return new StaticCall($carbonFullyQualified, new Identifier('today'));
|
||||
}
|
||||
$hasToday = Strings::match($dateTimeValue, '#today#');
|
||||
if ($hasToday !== null) {
|
||||
$carbonCall = new StaticCall($carbonFullyQualified, new Identifier('today'));
|
||||
} else {
|
||||
$carbonCall = new StaticCall($carbonFullyQualified, new Identifier('now'));
|
||||
}
|
||||
foreach (self::REGEX_TO_METHOD_NAME_MAP as $regex => $methodName) {
|
||||
$match = Strings::match($dateTimeValue, $regex);
|
||||
if ($match === null) {
|
||||
continue;
|
||||
}
|
||||
$countLNumber = new LNumber((int) $match['count']);
|
||||
$carbonCall = new MethodCall($carbonCall, new Identifier($methodName), [new Arg($countLNumber)]);
|
||||
}
|
||||
return $carbonCall;
|
||||
}
|
||||
}
|
69
rules/Carbon/Rector/FuncCall/DateFuncCallToCarbonRector.php
Normal file
69
rules/Carbon/Rector/FuncCall/DateFuncCallToCarbonRector.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Carbon\Rector\FuncCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector\DateFuncCallToCarbonRectorTest
|
||||
*/
|
||||
final class DateFuncCallToCarbonRector extends AbstractRector
|
||||
{
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Convert date() function call to Carbon::*()', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$date = date('Y-m-d');
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$date = \Carbon\Carbon::now()->format('Y-m-d')
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [FuncCall::class];
|
||||
}
|
||||
/**
|
||||
* @param FuncCall $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if (!$this->isName($node->name, 'date')) {
|
||||
return null;
|
||||
}
|
||||
if (\count($node->getArgs()) !== 1) {
|
||||
return null;
|
||||
}
|
||||
$firstArg = $node->getArgs()[0];
|
||||
if (!$firstArg->value instanceof String_) {
|
||||
return null;
|
||||
}
|
||||
// create now and format()
|
||||
$nowStaticCall = new StaticCall(new FullyQualified('Carbon\\Carbon'), 'now');
|
||||
return new MethodCall($nowStaticCall, 'format', [new Arg($firstArg->value)]);
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Carbon\Rector\MethodCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Carbon\NodeFactory\CarbonCallFactory;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\Carbon\Rector\MethodCall\DateTimeMethodCallToCarbonRector\DateTimeMethodCallToCarbonRectorTest
|
||||
*/
|
||||
final class DateTimeMethodCallToCarbonRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Carbon\NodeFactory\CarbonCallFactory
|
||||
*/
|
||||
private $carbonCallFactory;
|
||||
public function __construct(CarbonCallFactory $carbonCallFactory)
|
||||
{
|
||||
$this->carbonCallFactory = $carbonCallFactory;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Convert new DateTime() with a method call to Carbon::*()', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$date = (new \DateTime('today +20 day'))->format('Y-m-d');
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$date = \Carbon\Carbon::today()->addDays(20)->format('Y-m-d')
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [MethodCall::class];
|
||||
}
|
||||
/**
|
||||
* @param MethodCall $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if (!$node->var instanceof New_) {
|
||||
return null;
|
||||
}
|
||||
$new = $node->var;
|
||||
if (!$new->class instanceof Name) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->isName($new->class, 'DateTime')) {
|
||||
return null;
|
||||
}
|
||||
if (\count($new->getArgs()) !== 1) {
|
||||
// @todo handle in separate static call
|
||||
return null;
|
||||
}
|
||||
$firstArg = $new->getArgs()[0];
|
||||
if (!$firstArg->value instanceof String_) {
|
||||
return null;
|
||||
}
|
||||
$carbonFullyQualified = new FullyQualified('Carbon\\Carbon');
|
||||
$carbonCall = $this->carbonCallFactory->createFromDateTimeString($carbonFullyQualified, $firstArg->value);
|
||||
$node->var = $carbonCall;
|
||||
return $node;
|
||||
}
|
||||
}
|
70
rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php
Normal file
70
rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Carbon\Rector\New_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Carbon\NodeFactory\CarbonCallFactory;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @changelog https://github.com/briannesbitt/Carbon/issues/231
|
||||
*
|
||||
* @see \Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\DateTimeInstanceToCarbonRectorTest
|
||||
*/
|
||||
final class DateTimeInstanceToCarbonRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Carbon\NodeFactory\CarbonCallFactory
|
||||
*/
|
||||
private $carbonCallFactory;
|
||||
public function __construct(CarbonCallFactory $carbonCallFactory)
|
||||
{
|
||||
$this->carbonCallFactory = $carbonCallFactory;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Convert new DateTime() to Carbon::*()', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
$date = new \DateTime('today');
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
$date = \Carbon\Carbon::today();
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [New_::class];
|
||||
}
|
||||
/**
|
||||
* @param New_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if (!$this->isName($node->class, 'DateTime')) {
|
||||
return null;
|
||||
}
|
||||
// no arg? ::now()
|
||||
$carbonFullyQualified = new FullyQualified('Carbon\\Carbon');
|
||||
if ($node->args === []) {
|
||||
return new StaticCall($carbonFullyQualified, new Identifier('now'));
|
||||
}
|
||||
if (\count($node->getArgs()) === 1) {
|
||||
$firstArg = $node->getArgs()[0];
|
||||
if ($firstArg->value instanceof String_) {
|
||||
return $this->carbonCallFactory->createFromDateTimeString($carbonFullyQualified, $firstArg->value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '5df95d9a955523a1737442a26f4a412cafc41226';
|
||||
public const PACKAGE_VERSION = '575921d2bca4a0ef36d55c6fa4ebf1ea01bacc4d';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2024-05-18 19:46:02';
|
||||
public const RELEASE_DATE = '2024-05-18 15:27:32';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
@ -375,7 +375,7 @@ final class RectorConfigBuilder
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function withPreparedSets(bool $deadCode = \false, bool $codeQuality = \false, bool $codingStyle = \false, bool $typeDeclarations = \false, bool $privatization = \false, bool $naming = \false, bool $instanceOf = \false, bool $earlyReturn = \false, bool $strictBooleans = \false) : self
|
||||
public function withPreparedSets(bool $deadCode = \false, bool $codeQuality = \false, bool $codingStyle = \false, bool $typeDeclarations = \false, bool $privatization = \false, bool $naming = \false, bool $instanceOf = \false, bool $earlyReturn = \false, bool $strictBooleans = \false, bool $carbon = \false) : self
|
||||
{
|
||||
if ($deadCode) {
|
||||
$this->sets[] = SetList::DEAD_CODE;
|
||||
@ -404,6 +404,9 @@ final class RectorConfigBuilder
|
||||
if ($strictBooleans) {
|
||||
$this->sets[] = SetList::STRICT_BOOLEANS;
|
||||
}
|
||||
if ($carbon) {
|
||||
$this->sets[] = SetList::CARBON;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
|
@ -109,4 +109,8 @@ final class SetList implements SetListInterface
|
||||
* @var string
|
||||
*/
|
||||
public const INSTANCEOF = __DIR__ . '/../../../config/set/instanceof.php';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const CARBON = __DIR__ . '/../../../config/set/datetime-to-carbon.php';
|
||||
}
|
||||
|
4
vendor/composer/autoload_classmap.php
vendored
4
vendor/composer/autoload_classmap.php
vendored
@ -1053,6 +1053,10 @@ return array(
|
||||
'Rector\\Caching\\ValueObject\\CacheItem' => $baseDir . '/src/Caching/ValueObject/CacheItem.php',
|
||||
'Rector\\Caching\\ValueObject\\Storage\\FileCacheStorage' => $baseDir . '/src/Caching/ValueObject/Storage/FileCacheStorage.php',
|
||||
'Rector\\Caching\\ValueObject\\Storage\\MemoryCacheStorage' => $baseDir . '/src/Caching/ValueObject/Storage/MemoryCacheStorage.php',
|
||||
'Rector\\Carbon\\NodeFactory\\CarbonCallFactory' => $baseDir . '/rules/Carbon/NodeFactory/CarbonCallFactory.php',
|
||||
'Rector\\Carbon\\Rector\\FuncCall\\DateFuncCallToCarbonRector' => $baseDir . '/rules/Carbon/Rector/FuncCall/DateFuncCallToCarbonRector.php',
|
||||
'Rector\\Carbon\\Rector\\MethodCall\\DateTimeMethodCallToCarbonRector' => $baseDir . '/rules/Carbon/Rector/MethodCall/DateTimeMethodCallToCarbonRector.php',
|
||||
'Rector\\Carbon\\Rector\\New_\\DateTimeInstanceToCarbonRector' => $baseDir . '/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php',
|
||||
'Rector\\ChangesReporting\\Annotation\\AnnotationExtractor' => $baseDir . '/src/ChangesReporting/Annotation/AnnotationExtractor.php',
|
||||
'Rector\\ChangesReporting\\Annotation\\RectorsChangelogResolver' => $baseDir . '/src/ChangesReporting/Annotation/RectorsChangelogResolver.php',
|
||||
'Rector\\ChangesReporting\\Contract\\Output\\OutputFormatterInterface' => $baseDir . '/src/ChangesReporting/Contract/Output/OutputFormatterInterface.php',
|
||||
|
4
vendor/composer/autoload_static.php
vendored
4
vendor/composer/autoload_static.php
vendored
@ -1272,6 +1272,10 @@ class ComposerStaticInitc5b31423e044e1f31bdf2918ae19cbd3
|
||||
'Rector\\Caching\\ValueObject\\CacheItem' => __DIR__ . '/../..' . '/src/Caching/ValueObject/CacheItem.php',
|
||||
'Rector\\Caching\\ValueObject\\Storage\\FileCacheStorage' => __DIR__ . '/../..' . '/src/Caching/ValueObject/Storage/FileCacheStorage.php',
|
||||
'Rector\\Caching\\ValueObject\\Storage\\MemoryCacheStorage' => __DIR__ . '/../..' . '/src/Caching/ValueObject/Storage/MemoryCacheStorage.php',
|
||||
'Rector\\Carbon\\NodeFactory\\CarbonCallFactory' => __DIR__ . '/../..' . '/rules/Carbon/NodeFactory/CarbonCallFactory.php',
|
||||
'Rector\\Carbon\\Rector\\FuncCall\\DateFuncCallToCarbonRector' => __DIR__ . '/../..' . '/rules/Carbon/Rector/FuncCall/DateFuncCallToCarbonRector.php',
|
||||
'Rector\\Carbon\\Rector\\MethodCall\\DateTimeMethodCallToCarbonRector' => __DIR__ . '/../..' . '/rules/Carbon/Rector/MethodCall/DateTimeMethodCallToCarbonRector.php',
|
||||
'Rector\\Carbon\\Rector\\New_\\DateTimeInstanceToCarbonRector' => __DIR__ . '/../..' . '/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php',
|
||||
'Rector\\ChangesReporting\\Annotation\\AnnotationExtractor' => __DIR__ . '/../..' . '/src/ChangesReporting/Annotation/AnnotationExtractor.php',
|
||||
'Rector\\ChangesReporting\\Annotation\\RectorsChangelogResolver' => __DIR__ . '/../..' . '/src/ChangesReporting/Annotation/RectorsChangelogResolver.php',
|
||||
'Rector\\ChangesReporting\\Contract\\Output\\OutputFormatterInterface' => __DIR__ . '/../..' . '/src/ChangesReporting/Contract/Output/OutputFormatterInterface.php',
|
||||
|
Loading…
x
Reference in New Issue
Block a user