Skip to content

Commit e0967be

Browse files
committed
refactor: strict php7 typehints
1 parent c95954e commit e0967be

File tree

3 files changed

+59
-40
lines changed

3 files changed

+59
-40
lines changed

src/Expression.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the PHP-CRON-EXPR package.
57
*
@@ -72,7 +74,7 @@ public function __construct(SegmentChecker $checker = null)
7274
}
7375
}
7476

75-
public static function instance()
77+
public static function instance(): self
7678
{
7779
if (null === static::$instance) {
7880
static::$instance = new static;
@@ -85,11 +87,11 @@ public static function instance()
8587
* Parse cron expression to decide if it can be run on given time (or default now).
8688
*
8789
* @param string $expr The cron expression.
88-
* @param int $time The timestamp to validate the cron expr against. Defaults to now.
90+
* @param mixed $time The timestamp to validate the cron expr against. Defaults to now.
8991
*
9092
* @return bool
9193
*/
92-
public static function isDue($expr, $time = null)
94+
public static function isDue(string $expr, $time = null): bool
9395
{
9496
return static::instance()->isCronDue($expr, $time);
9597
}
@@ -102,7 +104,7 @@ public static function isDue($expr, $time = null)
102104
*
103105
* @return array Due job names: [job1name, ...];
104106
*/
105-
public static function getDues(array $jobs, $time = null)
107+
public static function getDues(array $jobs, $time = null): array
106108
{
107109
return static::instance()->filter($jobs, $time);
108110
}
@@ -117,7 +119,7 @@ public static function getDues(array $jobs, $time = null)
117119
*
118120
* @return bool
119121
*/
120-
public function isCronDue($expr, $time = null)
122+
public function isCronDue(string $expr, $time = null): bool
121123
{
122124
list($expr, $times) = $this->process($expr, $time);
123125

@@ -142,7 +144,7 @@ public function isCronDue($expr, $time = null)
142144
*
143145
* @return array Due job names: [job1name, ...];
144146
*/
145-
public function filter(array $jobs, $time = null)
147+
public function filter(array $jobs, $time = null): array
146148
{
147149
$dues = $cache = [];
148150
$time = $this->normalizeTime($time);
@@ -168,9 +170,9 @@ public function filter(array $jobs, $time = null)
168170
* @param string $expr
169171
* @param mixed $time
170172
*
171-
* @return array
173+
* @return array [expr, time]
172174
*/
173-
protected function process($expr, $time)
175+
protected function process(string $expr, $time): array
174176
{
175177
$expr = $this->normalizeExpr($expr);
176178
$expr = \preg_split('~\s+~', $expr); // 14
@@ -187,7 +189,7 @@ protected function process($expr, $time)
187189
return [$expr, $times];
188190
}
189191

190-
protected function normalizeTime($time)
192+
protected function normalizeTime($time): int
191193
{
192194
if (empty($time)) {
193195
$time = \time();
@@ -200,7 +202,7 @@ protected function normalizeTime($time)
200202
return $time;
201203
}
202204

203-
protected function normalizeExpr($expr)
205+
protected function normalizeExpr(string $expr): string
204206
{
205207
$expr = \trim($expr);
206208

src/SegmentChecker.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the PHP-CRON-EXPR package.
57
*
@@ -37,19 +39,12 @@ public function __construct(Validator $validator = null)
3739
*
3840
* @return bool
3941
*/
40-
public function checkDue($segment, $pos, $times)
42+
public function checkDue(string $segment, int $pos, array $times): bool
4143
{
42-
$isDue = true;
4344
$offsets = \explode(',', \trim($segment));
4445

4546
foreach ($offsets as $offset) {
46-
if (null === $isDue = $this->isOffsetDue($offset, $pos, $times)) {
47-
throw new \UnexpectedValueException(
48-
sprintf('Invalid offset value at segment #%d: %s', $pos, $offset)
49-
);
50-
}
51-
52-
if ($isDue) {
47+
if ($this->isOffsetDue($offset, $pos, $times)) {
5348
return true;
5449
}
5550
}
@@ -64,9 +59,9 @@ public function checkDue($segment, $pos, $times)
6459
* @param int $pos
6560
* @param array $times
6661
*
67-
* @return bool|null
62+
* @return bool
6863
*/
69-
protected function isOffsetDue($offset, $pos, $times)
64+
protected function isOffsetDue(string $offset, int $pos, array $times): bool
7065
{
7166
if (\strpos($offset, '/') !== false) {
7267
return $this->validator->inStep($times[$pos], $offset);
@@ -83,7 +78,7 @@ protected function isOffsetDue($offset, $pos, $times)
8378
return $this->checkModifier($offset, $pos, $times);
8479
}
8580

86-
protected function checkModifier($offset, $pos, $times)
81+
protected function checkModifier(string $offset, int $pos, array $times): bool
8782
{
8883
$isModifier = \strpbrk($offset, 'LCW#');
8984

@@ -95,6 +90,6 @@ protected function checkModifier($offset, $pos, $times)
9590
return $this->validator->isValidWeekDay($offset, $times);
9691
}
9792

98-
return null;
93+
$this->validator->unexpectedValue($pos, $offset);
9994
}
10095
}

src/Validator.php

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the PHP-CRON-EXPR package.
57
*
@@ -28,7 +30,7 @@ class Validator
2830
*
2931
* @return bool
3032
*/
31-
public function inRange($value, $offset)
33+
public function inRange(int $value, string $offset): bool
3234
{
3335
$parts = \explode('-', $offset);
3436

@@ -43,7 +45,7 @@ public function inRange($value, $offset)
4345
*
4446
* @return bool
4547
*/
46-
public function inStep($value, $offset)
48+
public function inStep(int $value, string $offset): bool
4749
{
4850
$parts = \explode('/', $offset, 2);
4951

@@ -58,7 +60,7 @@ public function inStep($value, $offset)
5860
$parts = \explode('/', $offset, 2);
5961
$subparts = \explode('-', $parts[0], 2) + [1 => $value];
6062

61-
return $this->inStepRange($value, $subparts[0], $subparts[1], $parts[1]);
63+
return $this->inStepRange((int) $value, (int) $subparts[0], (int) $subparts[1], (int) $parts[1]);
6264
}
6365

6466
/**
@@ -71,7 +73,7 @@ public function inStep($value, $offset)
7173
*
7274
* @return bool
7375
*/
74-
public function inStepRange($value, $start, $end, $step)
76+
public function inStepRange(int $value, int $start, int $end, int $step): bool
7577
{
7678
if (($start + $step) > $end) {
7779
return false;
@@ -92,31 +94,33 @@ public function inStepRange($value, $start, $end, $step)
9294
* @param string $value
9395
* @param array $time
9496
*
95-
* @return bool|null
97+
* @return bool
9698
*/
97-
public function isValidMonthDay($value, $time)
99+
public function isValidMonthDay(string $value, array $time): bool
98100
{
99101
if ($value == 'L') {
100102
return $time[2] == $time[6];
101103
}
102104

103105
if ($pos = \strpos($value, 'W')) {
104106
$value = \substr($value, 0, $pos);
105-
$month = \str_pad($time[8], 2, '0', \STR_PAD_LEFT);
107+
$month = $this->zeroPad($time[8]);
106108

107-
return $this->isClosestWeekDay($value, $month, $time);
109+
return $this->isClosestWeekDay((int) $value, $month, $time);
108110
}
111+
112+
$this->unexpectedValue(2, $value);
109113
}
110114

111-
protected function isClosestWeekDay($value, $month, $time)
115+
protected function isClosestWeekDay(int $value, string $month, array $time): bool
112116
{
113117
foreach ([0, -1, 1, -2, 2] as $i) {
114118
$incr = $value + $i;
115119
if ($incr < 1 || $incr > $time[6]) {
116120
continue;
117121
}
118122

119-
$incr = \str_pad($incr, 2, '0', \STR_PAD_LEFT);
123+
$incr = $this->zeroPad($incr);
120124
$parts = \explode(' ', \date('N m j', \strtotime("{$time[5]}-$month-$incr")));
121125
if ($parts[0] < 6 && $parts[1] == $month) {
122126
return $time[2] == $parts[2];
@@ -136,30 +140,43 @@ protected function isClosestWeekDay($value, $month, $time)
136140
* @param string $value
137141
* @param array $time
138142
*
139-
* @return bool|null
143+
* @return bool
140144
*/
141-
public function isValidWeekDay($value, $time)
145+
public function isValidWeekDay(string $value, array $time): bool
142146
{
143-
$month = \str_pad($time[8], 2, '0', \STR_PAD_LEFT);
147+
$month = $this->zeroPad($time[8]);
144148

145149
if (\strpos($value, 'L')) {
146150
return $this->isLastWeekDay($value, $month, $time);
147151
}
148152

149153
if (!\strpos($value, '#')) {
150-
return null;
154+
$this->unexpectedValue(4, $value);;
151155
}
152156

153157
list($day, $nth) = \explode('#', \str_replace('0#', '7#', $value));
154158

155-
if (!$this->isNthWeekDay($day, $nth) || $time[9] != $day) {
159+
if (!$this->isNthWeekDay((int) $day, (int) $nth) || $time[9] != $day) {
156160
return false;
157161
}
158162

159163
return \intval($time[7] / 7) == $nth - 1;
160164
}
161165

162-
protected function isLastWeekDay($value, $month, $time)
166+
/**
167+
* @param int $pos
168+
* @param string $value
169+
*
170+
* @throws \UnexpectedValueException
171+
*/
172+
public function unexpectedValue(int $pos, string $value)
173+
{
174+
throw new \UnexpectedValueException(
175+
\sprintf('Invalid offset value at segment #%d: %s', $pos, $value)
176+
);
177+
}
178+
179+
protected function isLastWeekDay(string $value, string $month, array $time): bool
163180
{
164181
$value = \explode('L', \str_replace('7L', '0L', $value));
165182
$decr = $time[6];
@@ -174,8 +191,13 @@ protected function isLastWeekDay($value, $month, $time)
174191
return false;
175192
}
176193

177-
protected function isNthWeekDay($day, $nth)
194+
protected function isNthWeekDay(int $day, int $nth): bool
178195
{
179196
return !($day < 0 || $day > 7 || $nth < 1 || $nth > 5);
180197
}
198+
199+
protected function zeroPad($value): string
200+
{
201+
return \str_pad((string) $value, 2, '0', \STR_PAD_LEFT);
202+
}
181203
}

0 commit comments

Comments
 (0)