Skip to content

Commit

Permalink
use \DateTimeImmutable()
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Jul 25, 2017
1 parent 4bccf11 commit 85367a6
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 95 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ $smart = new SmartSleep($schedule);
And now we can sleep

```php
$seconds = $smart->sleepForSeconds(new \DateTime());
$seconds = $smart->sleepForSeconds(new \DateTimeImmutable());

sleep($seconds);
```
Expand All @@ -62,7 +62,7 @@ The rule corresponds to specific day in the specified time interval.
Can be used for public holidays.

```php
$rule = new SpecificDayRule(new \DateTime('2017-01-01'), $start_hour, $end_hour, $max_sleep_seconds)
$rule = new SpecificDayRule(new \DateTimeImmutable('2017-01-01'), $start_hour, $end_hour, $max_sleep_seconds)
```

### EverydayRule
Expand Down
4 changes: 2 additions & 2 deletions src/Rule/EverydayRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public function __construct($start, $end, $seconds)
}

/**
* @param \DateTime $time
* @param \DateTimeImmutable $time
*
* @return bool
*/
public function isMatched(\DateTime $time)
public function isMatched(\DateTimeImmutable $time)
{
return $this->start() <= $time->format('G') && $this->end() > $time->format('G');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Rule/HolidayRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public function __construct($start, $end, $seconds)
}

/**
* @param \DateTime $time
* @param \DateTimeImmutable $time
*
* @return bool
*/
public function isMatched(\DateTime $time)
public function isMatched(\DateTimeImmutable $time)
{
return
$time->format('N') > 5 &&
Expand Down
10 changes: 5 additions & 5 deletions src/Rule/OnceDayRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ class OnceDayRule implements Rule

public function __construct()
{
$this->time = new \DateTime(); // default time
$this->time = new \DateTimeImmutable(); // default time
}

/**
* @param \DateTime $time
* @param \DateTimeImmutable $time
*
* @return bool
*/
public function isMatched(\DateTime $time)
public function isMatched(\DateTimeImmutable $time)
{
$this->time = clone $time; // save current time
$this->time = $time; // save current time

return true;
}
Expand All @@ -37,7 +37,7 @@ public function isMatched(\DateTime $time)
*/
public function seconds()
{
$offset_time = clone $this->time;
$offset_time = $this->time;
$offset_time->modify('+1 day')->setTime(0, 0, 0);
$offset = $offset_time->getTimestamp() - $this->time->getTimestamp(); // offset to next day

Expand Down
14 changes: 6 additions & 8 deletions src/Rule/OnceMonthRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ class OnceMonthRule implements Rule

public function __construct()
{
$this->time = new \DateTime(); // default time
$this->time = new \DateTimeImmutable(); // default time
}

/**
* @param \DateTime $time
* @param \DateTimeImmutable $time
*
* @return bool
*/
public function isMatched(\DateTime $time)
public function isMatched(\DateTimeImmutable $time)
{
$this->time = clone $time; // save current time
$this->time = $time; // save current time

return true;
}
Expand All @@ -38,11 +38,9 @@ public function isMatched(\DateTime $time)
public function seconds()
{
// interval duration [next month, next month +1)
$offset_time = clone $this->time;
$offset_time->modify('first day of this month')->modify('+1 month')->setTime(0, 0, 0);
$offset_time = $this->time->modify('first day of this month')->modify('+1 month')->setTime(0, 0, 0);

$limit_time = clone $offset_time;
$limit_time->modify('+1 month');
$limit_time = $offset_time->modify('+1 month');
$limit = $limit_time->getTimestamp() - $offset_time->getTimestamp();

// offset to next month
Expand Down
11 changes: 5 additions & 6 deletions src/Rule/OnceWeekRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ class OnceWeekRule implements Rule

public function __construct()
{
$this->time = new \DateTime(); // default time
$this->time = new \DateTimeImmutable(); // default time
}

/**
* @param \DateTime $time
* @param \DateTimeImmutable $time
*
* @return bool
*/
public function isMatched(\DateTime $time)
public function isMatched(\DateTimeImmutable $time)
{
$this->time = clone $time; // save current time
$this->time = $time; // save current time

return true;
}
Expand All @@ -37,8 +37,7 @@ public function isMatched(\DateTime $time)
*/
public function seconds()
{
$offset_time = clone $this->time;
$offset_time->modify('+1 week')->setTime(0, 0, 0);
$offset_time = $this->time->modify('+1 week')->setTime(0, 0, 0);
$offset = $offset_time->getTimestamp() - $this->time->getTimestamp(); // offset to next week

return $offset + rand(0, 604800); // 604800 is a 1 week
Expand Down
4 changes: 2 additions & 2 deletions src/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ interface Rule
public function seconds();

/**
* @param \DateTime $time
* @param \DateTimeImmutable $time
*
* @return bool
*/
public function isMatched(\DateTime $time);
public function isMatched(\DateTimeImmutable $time);
}
12 changes: 6 additions & 6 deletions src/Rule/SpecificDayRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ class SpecificDayRule implements HourIntervalRule
use RandSecondsRuleTrait;

/**
* @var \DateTime
* @var \DateTimeImmutable
*/
private $day;

/**
* @param \DateTime $day
* @param \DateTimeImmutable $day
* @param int $start
* @param int $end
* @param int $seconds
*/
public function __construct(\DateTime $day, $start, $end, $seconds)
public function __construct(\DateTimeImmutable $day, $start, $end, $seconds)
{
$this->day = clone $day;
$this->day = $day;
$this->setStart($start);
$this->setEnd($end);
$this->setSeconds($seconds);
}

/**
* @param \DateTime $time
* @param \DateTimeImmutable $time
*
* @return bool
*/
public function isMatched(\DateTime $time)
public function isMatched(\DateTimeImmutable $time)
{
return
$this->day->format('Ymd') == $time->format('Ymd') &&
Expand Down
4 changes: 2 additions & 2 deletions src/Rule/WeekdayRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public function __construct($start, $end, $seconds)
}

/**
* @param \DateTime $time
* @param \DateTimeImmutable $time
*
* @return bool
*/
public function isMatched(\DateTime $time)
public function isMatched(\DateTimeImmutable $time)
{
return
$time->format('N') <= 5 &&
Expand Down
4 changes: 2 additions & 2 deletions src/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public function getIterator()
}

/**
* @param \DateTime $time
* @param \DateTimeImmutable $time
*
* @return Rule|null
*/
public function matchedRule(\DateTime $time)
public function matchedRule(\DateTimeImmutable $time)
{
foreach ($this->rules as $rule) {
if ($rule->isMatched($time)) {
Expand Down
4 changes: 2 additions & 2 deletions src/SmartSleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public function __construct(Schedule $schedule)
}

/**
* @param \DateTime $now
* @param \DateTimeImmutable $now
*
* @return int
*/
public function sleepForSeconds(\DateTime $now)
public function sleepForSeconds(\DateTimeImmutable $now)
{
$rule = $this->schedule->matchedRule($now);
if ($rule instanceof Rule) {
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/simple_schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
new EverydayRule(23, 24, 60), // [23:00, 24:00)
]));

$seconds = $smart->sleepForSeconds(new \DateTime());
$seconds = $smart->sleepForSeconds(new \DateTimeImmutable());

echo sprintf('Sleep %s s.'.PHP_EOL, $seconds);
14 changes: 7 additions & 7 deletions tests/unit/Rule/EverydayRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ class EverydayRuleTest extends \PHPUnit_Framework_TestCase
public function getMatches()
{
return [
[new \DateTime('2:59'), 3, 5, 10, false],
[new \DateTime('3:00'), 3, 5, 50, true],
[new \DateTime('4:00'), 3, 5, 100, true],
[new \DateTime('4:59'), 3, 5, 200, true],
[new \DateTime('5:00'), 3, 5, 550, false],
[new \DateTimeImmutable('2:59'), 3, 5, 10, false],
[new \DateTimeImmutable('3:00'), 3, 5, 50, true],
[new \DateTimeImmutable('4:00'), 3, 5, 100, true],
[new \DateTimeImmutable('4:59'), 3, 5, 200, true],
[new \DateTimeImmutable('5:00'), 3, 5, 550, false],
];
}

/**
* @dataProvider getMatches
*
* @param \DateTime $time
* @param \DateTimeImmutable $time
* @param int $start
* @param int $end
* @param int $seconds
* @param bool $match
*/
public function testIsMatched(\DateTime $time, $start, $end, $seconds, $match)
public function testIsMatched(\DateTimeImmutable $time, $start, $end, $seconds, $match)
{
$rule = new EverydayRule($start, $end, $seconds);

Expand Down
26 changes: 13 additions & 13 deletions tests/unit/Rule/HolidayRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ public function getMatches()
{
return [
// test by day time
[new \DateTime('25-06-2016 2:59'), 3, 5, 10, false],
[new \DateTime('25-06-2016 3:00'), 3, 5, 20, true],
[new \DateTime('25-06-2016 4:00'), 3, 5, 50, true],
[new \DateTime('25-06-2016 4:59'), 3, 5, 100, true],
[new \DateTime('25-06-2016 5:00'), 3, 5, 150, false],
[new \DateTimeImmutable('25-06-2016 2:59'), 3, 5, 10, false],
[new \DateTimeImmutable('25-06-2016 3:00'), 3, 5, 20, true],
[new \DateTimeImmutable('25-06-2016 4:00'), 3, 5, 50, true],
[new \DateTimeImmutable('25-06-2016 4:59'), 3, 5, 100, true],
[new \DateTimeImmutable('25-06-2016 5:00'), 3, 5, 150, false],
// tests by week day
[new \DateTime('20-06-2016 4:00'), 3, 5, 300, false],
[new \DateTime('21-06-2016 4:00'), 3, 5, 550, false],
[new \DateTime('22-06-2016 4:00'), 3, 5, 0, false],
[new \DateTime('24-06-2016 4:00'), 3, 5, 333, false],
[new \DateTime('25-06-2016 4:00'), 3, 5, 123, true],
[new \DateTime('26-06-2016 4:00'), 3, 5, 999, true],
[new \DateTimeImmutable('20-06-2016 4:00'), 3, 5, 300, false],
[new \DateTimeImmutable('21-06-2016 4:00'), 3, 5, 550, false],
[new \DateTimeImmutable('22-06-2016 4:00'), 3, 5, 0, false],
[new \DateTimeImmutable('24-06-2016 4:00'), 3, 5, 333, false],
[new \DateTimeImmutable('25-06-2016 4:00'), 3, 5, 123, true],
[new \DateTimeImmutable('26-06-2016 4:00'), 3, 5, 999, true],
];
}

/**
* @dataProvider getMatches
*
* @param \DateTime $time
* @param \DateTimeImmutable $time
* @param int $start
* @param int $end
* @param int $seconds
* @param bool $match
*/
public function testIsMatched(\DateTime $time, $start, $end, $seconds, $match)
public function testIsMatched(\DateTimeImmutable $time, $start, $end, $seconds, $match)
{
$rule = new HolidayRule($start, $end, $seconds);

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Rule/OnceDayRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function testSecondsFromConstruct()

public function testSecondsFromMatched()
{
$time = new \DateTime('23-06-2016 13:42:15');
$time = new \DateTimeImmutable('23-06-2016 13:42:15');

$limit_time = new \DateTime('25-06-2016 00:00:00');
$limit_time = new \DateTimeImmutable('25-06-2016 00:00:00');
$limit = $limit_time->getTimestamp() - $time->getTimestamp();

$this->rule->isMatched($time);
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/Rule/OnceMonthRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function setUp()

public function testSecondsFromConstruct()
{
$limit_time = new \DateTime('+2 month 00:00:00');
$limit_time = new \DateTimeImmutable('+2 month 00:00:00');
$limit_time->modify('first day of this month');
$limit = $limit_time->getTimestamp() - time();

Expand All @@ -37,8 +37,8 @@ public function testSecondsFromConstruct()

public function testSecondsFromMatched()
{
$time = new \DateTime('23-06-2016 13:42:15');
$limit_time = new \DateTime('01-08-2016 13:42:15');
$time = new \DateTimeImmutable('23-06-2016 13:42:15');
$limit_time = new \DateTimeImmutable('01-08-2016 13:42:15');
$limit = $limit_time->getTimestamp() - $time->getTimestamp();

$this->rule->isMatched($time);
Expand All @@ -50,8 +50,8 @@ public function testSecondsFromMatched()

public function testSecondsFromMatchedForFebruary()
{
$time = new \DateTime('31-01-2016 13:42:15');
$limit_time = new \DateTime('01-03-2016 13:42:15');
$time = new \DateTimeImmutable('31-01-2016 13:42:15');
$limit_time = new \DateTimeImmutable('01-03-2016 13:42:15');
$limit = $limit_time->getTimestamp() - $time->getTimestamp();

$this->rule->isMatched($time);
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Rule/OnceWeekRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function testSecondsFromConstruct()

public function testSecondsFromMatched()
{
$time = new \DateTime('23-06-2016 13:42:15');
$limit_time = new \DateTime('07-07-2016 00:00:00');
$time = new \DateTimeImmutable('23-06-2016 13:42:15');
$limit_time = new \DateTimeImmutable('07-07-2016 00:00:00');
$limit = $limit_time->getTimestamp() - $time->getTimestamp();

$this->rule->isMatched($time);
Expand Down
Loading

0 comments on commit 85367a6

Please sign in to comment.