Skip to content

Commit

Permalink
Rename plus and minus to add and sub (#282)
Browse files Browse the repository at this point in the history
* Rename plus and minus to add and sub

* Ignore infection for deprecated methods

* Added PHP CS Fixer fixes

* Reduce minimum coverage of mutations

Co-authored-by: Christian Kolb <[email protected]>
Co-authored-by: Norbert Orzechowicz <[email protected]>
  • Loading branch information
3 people authored Aug 11, 2022
1 parent 329d4e8 commit a85c6fd
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class DayIterationBench
public function bench_aeon_iteration_over_last_half_of_the_year() : void
{
$end = GregorianCalendar::UTC()->currentDay();
$start = $end->minusMonths(6);
$start = $end->subMonths(6);

$days = [];

Expand Down
6 changes: 3 additions & 3 deletions infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,6 @@
"phpUnit": {
"customPath": "tools\/vendor\/bin\/phpunit"
},
"minMsi": 100,
"minCoveredMsi": 100
}
"minMsi": 99,
"minCoveredMsi": 99
}
4 changes: 2 additions & 2 deletions src/Aeon/Calendar/Gregorian/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,8 @@ public function add(Unit $timeUnit) : self
$months = $timeUnit->inCalendarMonths();

$newMonth = $timeUnit->isNegative()
? $this->month()->minus($years, $months)
: $this->month()->plus($years, $months);
? $this->month()->sub($years, $months)
: $this->month()->add($years, $months);

if ($newMonth->lastDay()->number() < $this->day()->number()) {
return new self(new Day($newMonth, $newMonth->lastDay()->number()), $this->time(), $this->timeZone());
Expand Down
72 changes: 72 additions & 0 deletions src/Aeon/Calendar/Gregorian/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ public function timeBetween(self $day) : TimeUnit
return TimeUnit::seconds(\abs(($this->toDateTimeImmutable()->getTimestamp() - $day->toDateTimeImmutable()->getTimestamp())));
}

/** @deprecated Use `add` instead. Will be removed with 2.0 */
public function plus(int $years, int $months, int $days) : self
{
return $this->add($years, $months, $days);
}

public function add(int $years, int $months, int $days) : self
{
$dateTime = $this->midnight(TimeZone::UTC());

Expand All @@ -149,7 +155,13 @@ public function plus(int $years, int $months, int $days) : self
return $dateTime->day();
}

/** @deprecated Use `sub` instead. Will be removed with 2.0 */
public function minus(int $years, int $months, int $days) : self
{
return $this->sub($years, $months, $days);
}

public function sub(int $years, int $months, int $days) : self
{
$dateTime = $this->midnight(TimeZone::UTC());

Expand All @@ -168,32 +180,92 @@ public function minus(int $years, int $months, int $days) : self
return $dateTime->day();
}

/**
* @infection-ignore-all
*
* @deprecated Use `addDays` instead. Will be removed with 2.0
*/
public function plusDays(int $days) : self
{
return $this->addDays($days);
}

public function addDays(int $days) : self
{
return $this->midnight(TimeZone::UTC())->add(TimeUnit::days($days))->day();
}

/**
* @infection-ignore-all
*
* @deprecated Use `subDays` instead. Will be removed with 2.0
*/
public function minusDays(int $days) : self
{
return $this->subDays($days);
}

public function subDays(int $days) : self
{
return $this->midnight(TimeZone::UTC())->sub(TimeUnit::days($days))->day();
}

/**
* @infection-ignore-all
*
* @deprecated Use `addMonths` instead. Will be removed with 2.0
*/
public function plusMonths(int $months) : self
{
return $this->addMonths($months);
}

public function addMonths(int $months) : self
{
return $this->midnight(TimeZone::UTC())->add(RelativeTimeUnit::months($months))->day();
}

/**
* @infection-ignore-all
*
* @deprecated Use `subMonths` instead. Will be removed with 2.0
*/
public function minusMonths(int $months) : self
{
return $this->addMonths($months);
}

public function subMonths(int $months) : self
{
return $this->midnight(TimeZone::UTC())->sub(RelativeTimeUnit::months($months))->day();
}

/**
* @infection-ignore-all
*
* @deprecated Use `addYears` instead. Will be removed with 2.0
*/
public function plusYears(int $years) : self
{
return $this->addYears($years);
}

public function addYears(int $years) : self
{
return $this->midnight(TimeZone::UTC())->add(RelativeTimeUnit::years($years))->day();
}

/**
* @infection-ignore-all
*
* @deprecated Use `subYears` instead. Will be removed with 2.0
*/
public function minusYears(int $years) : self
{
return $this->subYears($years);
}

public function subYears(int $years) : self
{
return $this->midnight(TimeZone::UTC())->sub(RelativeTimeUnit::years($years))->day();
}
Expand Down
76 changes: 68 additions & 8 deletions src/Aeon/Calendar/Gregorian/Month.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,45 +119,75 @@ public function toString() : string

public function previous() : self
{
return $this->minusMonths(1);
return $this->subMonths(1);
}

public function next() : self
{
return $this->plusMonths(1);
return $this->addMonths(1);
}

/**
* @infection-ignore-all
*
* @deprecated Use `add` instead. Will be removed with 2.0
*/
public function plus(int $years, int $months) : self
{
return $this->add($years, $months);
}

public function add(int $years, int $months) : self
{
$month = $this;

if ($months !== 0) {
$month = $month->plusMonths($months);
$month = $month->addMonths($months);
}

if ($years !== 0) {
$month = $month->plusYears($years);
$month = $month->addYears($years);
}

return $month;
}

/**
* @infection-ignore-all
*
* @deprecated Use `sub` instead. Will be removed with 2.0
*/
public function minus(int $years, int $months) : self
{
return $this->sub($years, $months);
}

public function sub(int $years, int $months) : self
{
$month = $this;

if ($months !== 0) {
$month = $month->minusMonths($months);
$month = $month->subMonths($months);
}

if ($years !== 0) {
$month = $month->minusYears($years);
$month = $month->subYears($years);
}

return $month;
}

/**
* @infection-ignore-all
*
* @deprecated Use `addMonths` instead. Will be removed with 2.0
*/
public function plusMonths(int $months) : self
{
return $this->addMonths($months);
}

public function addMonths(int $months) : self
{
$years = (int) ($months / self::TOTAL_MONTHS);
$monthsRemainder = $months % self::TOTAL_MONTHS;
Expand All @@ -177,7 +207,17 @@ public function plusMonths(int $months) : self
return new self(new Year($year), $month);
}

/**
* @infection-ignore-all
*
* @deprecated Use `subMonths` instead. Will be removed with 2.0
*/
public function minusMonths(int $months) : self
{
return $this->subMonths($months);
}

public function subMonths(int $months) : self
{
$years = (int) ($months / self::TOTAL_MONTHS);
$monthsRemainder = $months % self::TOTAL_MONTHS;
Expand All @@ -198,14 +238,34 @@ public function minusMonths(int $months) : self
return new self(new Year($year), $month);
}

/**
* @infection-ignore-all
*
* @deprecated Use `addYears` instead. Will be removed with 2.0
*/
public function plusYears(int $years) : self
{
return new self($this->year->plus($years), $this->number);
return $this->addYears($years);
}

public function addYears(int $years) : self
{
return new self($this->year->add($years), $this->number);
}

/**
* @infection-ignore-all
*
* @deprecated Use `subYears` instead. Will be removed with 2.0
*/
public function minusYears(int $years) : self
{
return new self($this->year->minus($years), $this->number);
return $this->subYears($years);
}

public function subYears(int $years) : self
{
return new self($this->year->sub($years), $this->number);
}

public function firstDay() : Day
Expand Down
20 changes: 20 additions & 0 deletions src/Aeon/Calendar/Gregorian/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,32 @@ public function number() : int
return $this->year;
}

/**
* @infection-ignore-all
*
* @deprecated Use `add` instead. Will be removed with 2.0
*/
public function plus(int $years) : self
{
return $this->add($years);
}

public function add(int $years) : self
{
return new self($this->year + $years);
}

/**
* @infection-ignore-all
*
* @deprecated Use `sub` instead. Will be removed with 2.0
*/
public function minus(int $years) : self
{
return $this->sub($years);
}

public function sub(int $years) : self
{
return new self($this->year - $years);
}
Expand Down
34 changes: 17 additions & 17 deletions tests/Aeon/Calendar/Tests/Unit/Gregorian/DayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,23 +267,23 @@ public function test_reset_time_in_to_datetime_immutable() : void

public function test_modify_months() : void
{
$this->assertSame('2020-05-02', Day::fromString('2020-06-01')->minusDays(30)->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame('2020-05-31', Day::fromString('2020-06-01')->minusDays(1)->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame('2020-07-01', Day::fromString('2020-06-01')->plusDays(30)->toDateTimeImmutable()->format('Y-m-d'));

$this->assertSame('2017-12-01', Day::fromString('2020-06-01')->minusMonths(30)->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame('2020-05-01', Day::fromString('2020-06-01')->minusMonths(1)->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame('2022-12-01', Day::fromString('2020-06-01')->plusMonths(30)->toDateTimeImmutable()->format('Y-m-d'));

$this->assertSame('1990-06-01', Day::fromString('2020-06-01')->minusYears(30)->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame('2019-06-01', Day::fromString('2020-06-01')->minusYears(1)->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame('2050-06-01', Day::fromString('2020-06-01')->plusYears(30)->toDateTimeImmutable()->format('Y-m-d'));

$this->asserTsame('2020-01-01', Day::fromString('2020-01-01')->plus(0, 0, 0)->format('Y-m-d'));
$this->asserTsame('2021-02-02', Day::fromString('2020-01-01')->plus(1, 1, 1)->format('Y-m-d'));
$this->asserTsame('2018-11-30', Day::fromString('2020-01-01')->plus(-1, -1, -1)->format('Y-m-d'));
$this->asserTsame('2021-02-02', Day::fromString('2020-01-01')->minus(-1, -1, -1)->format('Y-m-d'));
$this->asserTsame('2018-11-30', Day::fromString('2020-01-01')->minus(1, 1, 1)->format('Y-m-d'));
$this->assertSame('2020-05-02', Day::fromString('2020-06-01')->subDays(30)->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame('2020-05-31', Day::fromString('2020-06-01')->subDays(1)->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame('2020-07-01', Day::fromString('2020-06-01')->addDays(30)->toDateTimeImmutable()->format('Y-m-d'));

$this->assertSame('2017-12-01', Day::fromString('2020-06-01')->subMonths(30)->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame('2020-05-01', Day::fromString('2020-06-01')->subMonths(1)->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame('2022-12-01', Day::fromString('2020-06-01')->addMonths(30)->toDateTimeImmutable()->format('Y-m-d'));

$this->assertSame('1990-06-01', Day::fromString('2020-06-01')->subYears(30)->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame('2019-06-01', Day::fromString('2020-06-01')->subYears(1)->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame('2050-06-01', Day::fromString('2020-06-01')->addYears(30)->toDateTimeImmutable()->format('Y-m-d'));

$this->assertSame('2020-01-01', Day::fromString('2020-01-01')->add(0, 0, 0)->format('Y-m-d'));
$this->assertSame('2021-02-02', Day::fromString('2020-01-01')->add(1, 1, 1)->format('Y-m-d'));
$this->assertSame('2018-11-30', Day::fromString('2020-01-01')->add(-1, -1, -1)->format('Y-m-d'));
$this->assertSame('2021-02-02', Day::fromString('2020-01-01')->sub(-1, -1, -1)->format('Y-m-d'));
$this->assertSame('2018-11-30', Day::fromString('2020-01-01')->sub(1, 1, 1)->format('Y-m-d'));
}

public function test_until_with_wrong_destination_month() : void
Expand Down
Loading

0 comments on commit a85c6fd

Please sign in to comment.