Skip to content

Commit 7c4d7e6

Browse files
authored
Merge pull request #8 from adhocore/develop
Refactor
2 parents dfd625f + 7f605dd commit 7c4d7e6

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

src/Expression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function isCronDue($expr, $time = null)
8787
continue;
8888
}
8989

90-
if (!$checker->isDue($segment, $pos, $time)) {
90+
if (!$checker->checkDue($segment, $pos, $time)) {
9191
return false;
9292
}
9393
}

src/SegmentChecker.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public function __construct()
2828
*
2929
* @return bool
3030
*/
31-
public function isDue($segment, $pos, $time)
31+
public function checkDue($segment, $pos, $time)
3232
{
3333
$isDue = true;
3434
$offsets = \explode(',', \trim($segment));
3535

3636
foreach ($offsets as $offset) {
3737
if (null === $isDue = $this->isOffsetDue($offset, $pos, $time)) {
3838
throw new \UnexpectedValueException(
39-
sprintf('Invalid offset value %s for segment #%d', $offset, $pos)
39+
sprintf('Invalid offset value at segment #%d: %s', $pos, $offset)
4040
);
4141
}
4242

@@ -86,8 +86,6 @@ protected function checkModifier($offset, $pos, $time)
8686
return $this->validator->isValidWeekDay($offset, $time);
8787
}
8888

89-
// @codeCoverageIgnoreStart
90-
return !$isModifier;
91-
// @codeCoverageIgnoreEnd
89+
return null;
9290
}
9391
}

src/Validator.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,23 @@ public function isValidWeekDay($value, $time)
9393
$month = \str_pad($time[8], 2, '0', \STR_PAD_LEFT);
9494

9595
if (\strpos($value, 'L')) {
96-
return $this->lastWeekDay($value, $month, $time);
96+
return $this->isLastWeekDay($value, $month, $time);
9797
}
9898

99-
if (\strpos($value, '#')) {
100-
$value = \explode('#', \str_replace('0#', '7#', $value));
99+
if (!\strpos($value, '#')) {
100+
return null;
101+
}
101102

102-
if ($value[0] < 0 || $value[0] > 7 || $value[1] < 1 || $value[1] > 5 || $time[9] != $value[0]) {
103-
return false;
104-
}
103+
list($day, $nth) = \explode('#', \str_replace('0#', '7#', $value));
105104

106-
return \intval($time[7] / 7) == $value[1] - 1;
105+
if (!$this->isNthWeekDay($day, $nth) || $time[9] != $day) {
106+
return false;
107107
}
108108

109-
// @codeCoverageIgnoreStart
110-
return false;
111-
// @codeCoverageIgnoreEnd
109+
return \intval($time[7] / 7) == $nth - 1;
112110
}
113111

114-
protected function lastWeekDay($value, $month, $time)
112+
protected function isLastWeekDay($value, $month, $time)
115113
{
116114
$value = \explode('L', \str_replace('7L', '0L', $value));
117115
$decr = $time[6];
@@ -125,4 +123,9 @@ protected function lastWeekDay($value, $month, $time)
125123

126124
return false;
127125
}
126+
127+
protected function isNthWeekDay($day, $nth)
128+
{
129+
return !($day < 0 || $day > 7 || $nth < 1 || $nth > 5);
130+
}
128131
}

tests/ExpressionTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ class ExpressionTest extends \PHPUnit_Framework_TestCase
99
/**
1010
* @dataProvider scheduleProvider
1111
*/
12-
public function test_isDue($expr, $time, $foo, $expected)
12+
public function test_isDue($expr, $time, $foo, $expected, $throwsAt = false)
1313
{
14+
if ($throwsAt) {
15+
$this->setExpectedException(
16+
\UnexpectedValueException::class,
17+
"Invalid offset value at segment #$throwsAt"
18+
);
19+
}
20+
1421
$actual = Expression::isDue($expr, $time);
1522

1623
$this->assertSame($expected, $actual, 'The expression ' . $expr . ' has failed');
@@ -117,6 +124,8 @@ public function scheduleProvider()
117124
['* * * * 5#2', strtotime('2011-07-01 00:00:00'), '2011-07-08 00:00:00', false],
118125
['* * * * 5#1', strtotime('2011-07-01 00:00:00'), '2011-07-01 00:00:00', true],
119126
['* * * * 3#4', strtotime('2011-07-01 00:00:00'), '2011-07-27 00:00:00', false],
127+
['* * * * 4W', strtotime('2011-07-01 00:00:00'), '2011-07-27 00:00:00', false, 4], // seg 4
128+
['* * * 1L *', strtotime('2011-07-01 00:00:00'), '2011-07-27 00:00:00', false, 3], // seg 3
120129
];
121130
}
122131
}

0 commit comments

Comments
 (0)