Skip to content

Commit

Permalink
[Carbon] Init set to migrate DateTime to Carbon (#5868)
Browse files Browse the repository at this point in the history
* [Carbon] Init

* add fixture

* add day count suport

* add months

* add DateTimeMethodCallToCarbonRector
  • Loading branch information
TomasVotruba committed May 18, 2024
1 parent 5df95d9 commit 575921d
Show file tree
Hide file tree
Showing 21 changed files with 726 additions and 2 deletions.
57 changes: 56 additions & 1 deletion build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# 371 Rules Overview
# 374 Rules Overview

<br>

## Categories

- [Arguments](#arguments) (4)

- [Carbon](#carbon) (3)

- [CodeQuality](#codequality) (75)

- [CodingStyle](#codingstyle) (28)
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions config/set/datetime-to-carbon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

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,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DateFuncCallToCarbonRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector\Fixture;

class SomeClass
{
public function run()
{
$date = date('Y-m-d');
}
}

?>
-----
<?php

namespace Rector\Tests\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector\Fixture;

class SomeClass
{
public function run()
{
$date = \Carbon\Carbon::now()->format('Y-m-d');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DateFuncCallToCarbonRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Carbon\Rector\MethodCall\DateTimeMethodCallToCarbonRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DateTimeMethodCallToCarbonRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Carbon\Rector\MethodCall\DateTimeMethodCallToCarbonRector\Fixture;

class SomeClass
{
public function run()
{
$date = (new \DateTime('today +20 day'))->format('Y-m-d');
}
}

?>
-----
<?php

namespace Rector\Tests\Carbon\Rector\MethodCall\DateTimeMethodCallToCarbonRector\Fixture;

class SomeClass
{
public function run()
{
$date = (\Carbon\Carbon::today()->addDays(20))->format('Y-m-d');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Carbon\Rector\MethodCall\DateTimeMethodCallToCarbonRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DateTimeMethodCallToCarbonRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DateTimeInstanceToCarbonRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\Fixture;

final class DateTimeNow
{
public function run()
{
$date = new \DateTime('now');
}
}

?>
-----
<?php

namespace Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\Fixture;

final class DateTimeNow
{
public function run()
{
$date = \Carbon\Carbon::now();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\Fixture;

final class DateTimeWithMonths
{
public function run()
{
$date = new \DateTime('+ 2 months');
}
}

?>
-----
<?php

namespace Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\Fixture;

final class DateTimeWithMonths
{
public function run()
{
$date = \Carbon\Carbon::now()->addMonths(2);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\Fixture;

final class DateTimeTodayWith5Days
{
public function run()
{
$date = new \DateTime('today + 5 day');

$middleDate = new \DateTime('today +7day');

$nextDate = new \DateTime('today +10 days');
}
}

?>
-----
<?php

namespace Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\Fixture;

final class DateTimeTodayWith5Days
{
public function run()
{
$date = \Carbon\Carbon::today()->addDays(5);

$middleDate = \Carbon\Carbon::today()->addDays(7);

$nextDate = \Carbon\Carbon::today()->addDays(10);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\Fixture;

final class DateTimeNow
{
public function run()
{
$date = new \DateTime('now');
}
}

?>
-----
<?php

namespace Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\Fixture;

final class DateTimeNow
{
public function run()
{
$date = \Carbon\Carbon::now();
}
}

?>

0 comments on commit 575921d

Please sign in to comment.