Skip to content

Commit

Permalink
Carbon use parse method (#6183)
Browse files Browse the repository at this point in the history
* Implement time manipulation on date now

* Add link to regex for checks

* Use Carbon:parse function to convert datetime to carbon to prevent all missing usecases

* Update code examples

* Change tests to check for parse static call

* Add today as method call and fix tests

* Change the tests to parse method

* Update function return types

* Rewrite Carbon Call Factory to combine regex and fallback to parse

* Add tests for tomorrow and yesterday

* Test for fallback to parse

* Fix correct strings utils

* Create const for regex

* Fallback if unit or operator not set

* Add text for date manipulation

* Remove month test this is combined

* Change classname

* Fix most of possible cases for carbon

* Fix some static errors

* When set time always add all args

* Use correct var for identifier name

* Add method call for set date

* Add some extra tests

* Fix tests

* Correct test with time

* do correct time checks

* Make sure property exists

* Change checks

* Refactor use of reference

* Rebuild the carboncall

* Check correct instances

* Check size of callstack

* Check correct instance

* Add count check

* change check for rebuild

* Skip checks

* Check for empty callstack

* Move rebuild to separate function

* Fix time when set to 00:00

* Add full datetime method

* Add test for full date

* Add change to config

* remove setDate & setTime functions

* Add test for date where no string args

* Configure rule config for test

* Revert classname changes

* Update config/set/datetime-to-carbon.php

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>

---------

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
florisbosch and samsonasik committed Jul 31, 2024
1 parent 97653a9 commit 71ae13a
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 131 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

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

final class DateTimeNowAddSub
{
public function run()
{
$addSeconds = new \DateTime('+5 seconds');
$addMinutes = new \DateTime('+5 minutes');
$addHours = new \DateTime('+5 hours');
$addDays = new \DateTime('+5 days');
$addWeeks = new \DateTime('+5 weeks');
$addMonths = new \DateTime('+5 months');
$addYears = new \DateTime('+5 years');

$subSeconds = new \DateTime('-5 seconds');
$subMinuts = new \DateTime('-5 minutes');
$subHours = new \DateTime('-5 hours');
$subDays = new \DateTime('-5 days');
$subWeeks = new \DateTime('-5 weeks');
$subMonths = new \DateTime('-5 months');
$subYears = new \DateTime('-5 years');
}
}

?>
-----
<?php

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

final class DateTimeNowAddSub
{
public function run()
{
$addSeconds = \Carbon\Carbon::now()->addSeconds(5);
$addMinutes = \Carbon\Carbon::now()->addMinutes(5);
$addHours = \Carbon\Carbon::now()->addHours(5);
$addDays = \Carbon\Carbon::now()->addDays(5);
$addWeeks = \Carbon\Carbon::now()->addWeeks(5);
$addMonths = \Carbon\Carbon::now()->addMonths(5);
$addYears = \Carbon\Carbon::now()->addYears(5);

$subSeconds = \Carbon\Carbon::now()->subSeconds(5);
$subMinuts = \Carbon\Carbon::now()->subMinutes(5);
$subHours = \Carbon\Carbon::now()->subHours(5);
$subDays = \Carbon\Carbon::now()->subDays(5);
$subWeeks = \Carbon\Carbon::now()->subWeeks(5);
$subMonths = \Carbon\Carbon::now()->subMonths(5);
$subYears = \Carbon\Carbon::now()->subYears(5);
}
}

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

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

final class DateTimeParse
{
public function run()
{
$date = new \DateTime('next week');
$exactDate = new \DateTime('2024-07-25');
$textualDate = new \DateTime('2 days ago');
}
}

?>
-----
<?php

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

final class DateTimeParse
{
public function run()
{
$date = \Carbon\Carbon::parse('next week');
$exactDate = \Carbon\Carbon::parse('2024-07-25');
$textualDate = \Carbon\Carbon::parse('2 days ago');
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

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

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

Expand All @@ -16,11 +16,11 @@ final class DateTimeWithMonths

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

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

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

Expand All @@ -16,11 +16,11 @@ final class DateTimeNow

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

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

final class DateTimeWithDateTime
{
public function run()
{
$date = new \DateTime('2024-07-30');
$time = new \DateTime('11:12:13');
$datetime = new \DateTime('2024-07-30 11:12:13');

$tomorrowTime = new \DateTime('tomorrow 12:00');
$yesterdayTime = new \DateTime('yesterday 12:00');

$dateNoon = new \DateTime('2024-07-30 noon');
$restStringToParse = new \DateTime('tomorrow noon');

$fullDate = new \DateTime('2024-07-30T11:12:13.000Z');
}
}

?>
-----
<?php

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

final class DateTimeWithDateTime
{
public function run()
{
$date = \Carbon\Carbon::parse('2024-07-30');
$time = \Carbon\Carbon::parse('11:12:13');
$datetime = \Carbon\Carbon::parse('2024-07-30 11:12:13');

$tomorrowTime = \Carbon\Carbon::parse('12:00 tomorrow');
$yesterdayTime = \Carbon\Carbon::parse('12:00 yesterday');

$dateNoon = \Carbon\Carbon::parse('2024-07-30 noon');
$restStringToParse = \Carbon\Carbon::parse('noon tomorrow');

$fullDate = \Carbon\Carbon::parse('2024-07-30T11:12:13.000Z');
}
}

?>

This file was deleted.

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 DateTimeYesterday
{
public function run()
{
$date = new \DateTime('yesterday');
}
}

?>
-----
<?php

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

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

?>
Loading

0 comments on commit 71ae13a

Please sign in to comment.