SegmentChecker::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the PHP-CRON-EXPR package.
7
 *
8
 * (c) Jitendra Adhikari <[email protected]>
9
 *     <https://github.com/adhocore>
10
 *
11
 * Licensed under MIT license.
12
 */
13
14
namespace Ahc\Cron;
15
16
/**
17
 * Cron Expression segment checker.
18
 *
19
 * This class checks if a cron segment satisfies given time.
20
 *
21
 * @author Jitendra Adhikari <[email protected]>
22
 */
23
class SegmentChecker
24
{
25
    /** @var ReferenceTime */
26
    protected $reference;
27
28
    /** @var Validator */
29
    protected $validator;
30
31
    public function __construct(Validator $validator = null)
32
    {
33
        $this->validator = $validator ?: new Validator;
34
    }
35
36
    public function setReference(ReferenceTime $reference)
37
    {
38
        $this->reference = $reference;
39
    }
40
41
    /**
42
     * Checks if a cron segment satisfies given time.
43
     *
44
     * @param string $segment
45
     * @param int    $pos
46
     *
47
     * @return bool
48
     */
49
    public function checkDue(string $segment, int $pos): bool
50
    {
51
        $offsets = \explode(',', \trim($segment));
52
53
        foreach ($offsets as $offset) {
54
            if ($this->isOffsetDue($offset, $pos)) {
55
                return true;
56
            }
57
        }
58
59
        return false;
60
    }
61
62
    /**
63
     * Check if a given offset at a position is due with respect to given time.
64
     *
65
     * @param string $offset
66
     * @param int    $pos
67
     *
68
     * @return bool
69
     */
70
    protected function isOffsetDue(string $offset, int $pos): bool
71
    {
72
        if (\strpos($offset, '/') !== false) {
73
            return $this->validator->inStep($this->reference->get($pos), $offset);
74
        }
75
76
        if (\strpos($offset, '-') !== false) {
77
            return $this->validator->inRange($this->reference->get($pos), $offset);
78
        }
79
80
        if (\is_numeric($offset)) {
81
            return $this->reference->isAt($offset, $pos);
82
        }
83
84
        return $this->checkModifier($offset, $pos);
85
    }
86
87
    protected function checkModifier(string $offset, int $pos): bool
88
    {
89
        $isModifier = \strpbrk($offset, 'LCW#');
90
91
        if ($pos === ReferenceTime::MONTHDAY && $isModifier) {
92
            return $this->validator->isValidMonthDay($offset, $this->reference);
93
        }
94
95
        if ($pos === ReferenceTime::WEEKDAY && $isModifier) {
96
            return $this->validator->isValidWeekDay($offset, $this->reference);
97
        }
98
99
        $this->validator->unexpectedValue($pos, $offset);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
100
        // @codeCoverageIgnoreStart
101
    }
102
103
    // @codeCoverageIgnoreEnd
104
}
105