1 | <?php |
||
49 | class CronExpression extends SimpleCron |
||
50 | { |
||
51 | /** |
||
52 | * Constants which define the position of semantic pieces of a DateTime object |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | const SECOND = 0; |
||
57 | const MINUTE = 1; |
||
58 | const HOUR = 2; |
||
59 | const DAY = 3; |
||
60 | const MONTH = 4; |
||
61 | const WEEKDAY = 5; |
||
62 | const YEAR = 6; |
||
63 | |||
64 | /** |
||
65 | * CRON expression parts |
||
66 | * |
||
67 | * @var array $cronParts |
||
68 | */ |
||
69 | protected $cronParts; |
||
70 | |||
71 | /** |
||
72 | * CRON field factory |
||
73 | * |
||
74 | * @var FieldFactory $fieldFactory |
||
75 | */ |
||
76 | protected $fieldFactory; |
||
77 | |||
78 | /** |
||
79 | * Order in which to test of cron parts |
||
80 | * |
||
81 | * @var array $order |
||
82 | */ |
||
83 | protected static $order = array(self::YEAR, self::MONTH, self::DAY, self::WEEKDAY, self::HOUR, self::MINUTE, self::SECOND); |
||
84 | |||
85 | /** |
||
86 | * Parse a CRON expression |
||
87 | * |
||
88 | * @param string $expression CRON expression (e.g. '8 * * * *') |
||
89 | * @param SimpleFieldFactory $fieldFactory Factory to create cron fields |
||
90 | */ |
||
91 | 8 | public function __construct($expression, SimpleFieldFactory $fieldFactory) |
|
96 | |||
97 | /** |
||
98 | * Factory method to create a new CronExpression. |
||
99 | * There are several special predefined values which can be used to substitute the |
||
100 | * CRON expression. You might see them below |
||
101 | * |
||
102 | * @param string $expression The CRON expression to create |
||
103 | * @param SimpleFieldFactory $fieldFactory (optional) Field factory to use |
||
104 | * |
||
105 | * @yearly, @annually) - Run once a year, midnight, Jan. 1 - 0 0 0 1 1 * |
||
106 | * @monthly - Run once a month, midnight, first of month - 0 0 0 1 * * |
||
107 | * @weekly - Run once a week, midnight on Sun - 0 0 0 * * 0 |
||
108 | * @daily - Run once a day, midnight - 0 0 0 * * * |
||
109 | * @hourly - Run once an hour, first minute - 0 0 * * * * |
||
110 | * @byMinute - Run once a minute, first second - 0 * * * * * |
||
111 | * @bySecond - Run once a second - * * * * * * |
||
112 | * |
||
113 | * @return CronExpression |
||
114 | */ |
||
115 | 3 | public static function factory($expression, SimpleFieldFactory $fieldFactory = null) |
|
116 | { |
||
117 | $mappings = array( |
||
118 | 3 | '@yearly' => '0 0 0 1 1 *', |
|
119 | '@annually' => '0 0 0 1 1 *', |
||
120 | '@monthly' => '0 0 0 1 * *', |
||
121 | '@weekly' => '0 0 0 * * 0', |
||
122 | '@daily' => '0 0 0 * * *', |
||
123 | '@hourly' => '0 0 * * * *', |
||
124 | '@byMinute' => '0 * * * * *', |
||
125 | '@bySecond' => '* * * * * *' |
||
126 | ); |
||
127 | |||
128 | 3 | if (isset($mappings[$expression])) { |
|
129 | 3 | $expression = $mappings[$expression]; |
|
130 | } |
||
131 | |||
132 | 3 | return new static($expression, $fieldFactory ?: new FieldFactory()); |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Get all or part of the CRON expression |
||
137 | * |
||
138 | * @param string $part (optional) Specify the part to retrieve or NULL to |
||
139 | * get the full cron schedule string. |
||
140 | * |
||
141 | * @return string|null Returns the CRON expression, a part of the |
||
142 | * CRON expression, or NULL if the part was specified but not found |
||
143 | */ |
||
144 | 7 | public function getExpression($part = null) |
|
154 | |||
155 | /** |
||
156 | * Get the next or previous run date of the expression relative to a date |
||
157 | * |
||
158 | * @param string|\DateTime $currentTime (optional) Relative calculation date |
||
159 | * @param int $nth (optional) Number of matches to skip before returning |
||
160 | * @param bool $invert (optional) Set to TRUE to go backwards in time |
||
161 | * @param bool $allowCurrentDate (optional) Set to TRUE to return the |
||
162 | * current date if it matches the cron expression |
||
163 | * |
||
164 | * @return \DateTime |
||
165 | * @throws \RuntimeException on too many iterations |
||
166 | */ |
||
167 | 59 | protected function getRunDate($currentTime = null, $nth = 0, $invert = false, $allowCurrentDate = false) |
|
223 | |||
224 | /** |
||
225 | * Determine if the CRON is due to run based, on the current date or a |
||
226 | * specific date |
||
227 | * |
||
228 | * @param string|\DateTime $currentTime (optional) Relative calculation date |
||
229 | * |
||
230 | * @return bool Returns TRUE if the cron is due to run or FALSE if not |
||
231 | */ |
||
232 | 55 | public function isDue($currentTime = null) |
|
249 | |||
250 | /** |
||
251 | * Set or change the CRON expression |
||
252 | * |
||
253 | * @param string $expression CRON expression (e.g. 8 * * * *) |
||
254 | * |
||
255 | * @return CronExpression |
||
256 | * @throws \InvalidArgumentException if not a valid CRON expression |
||
257 | */ |
||
258 | 3 | public function setExpression($expression) |
|
273 | |||
274 | /** |
||
275 | * Set part of the CRON expression |
||
276 | * |
||
277 | * @param int $position The position of the CRON expression to set |
||
278 | * @param string $value The value to set |
||
279 | * |
||
280 | * @return \Cron\CronExpression |
||
281 | * @throws \InvalidArgumentException if the value is not valid for the part |
||
282 | */ |
||
283 | 3 | public function setPart($position, $value) |
|
295 | } |
||
296 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.