Conditions | 10 |
Paths | 8 |
Total Lines | 52 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
74 | public static function parse(string $expression, ?int $timestamp = null): int |
||
75 | { |
||
76 | $cronExpression = trim($expression); |
||
77 | |||
78 | $cronRegex = '/^((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+' |
||
79 | . '((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+' |
||
80 | . '((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+' |
||
81 | . '((\*(\/[0-9]+)?)|[0-9\-\,\/]+)\s+' |
||
82 | . '((\*(\/[0-9]+)?)|[0-9\-\,\/]+)$/i'; |
||
83 | if (!preg_match($cronRegex, $cronExpression)) { |
||
84 | throw new InvalidArgumentException(sprintf( |
||
85 | 'Invalid cron expression [%s]', |
||
86 | $expression |
||
87 | )); |
||
88 | } |
||
89 | |||
90 | $crons = preg_split('/[\s]+/i', $cronExpression); |
||
91 | if ($crons === false) { |
||
92 | return 0; |
||
93 | } |
||
94 | |||
95 | $start = time(); |
||
96 | if ($timestamp !== null) { |
||
97 | $start = $timestamp; |
||
98 | } |
||
99 | |||
100 | $dates = [ |
||
101 | 'minutes' => self::parseNumber($crons[0], 0, 59), |
||
102 | 'hours' => self::parseNumber($crons[1], 0, 23), |
||
103 | 'dom' => self::parseNumber($crons[2], 1, 31), |
||
104 | 'month' => self::parseNumber($crons[3], 1, 12), |
||
105 | 'dow' => self::parseNumber($crons[4], 0, 6), |
||
106 | ]; |
||
107 | |||
108 | // limited to time()+366 - no need |
||
109 | // to check more than 1 year ahead |
||
110 | $total = 60 * 60 * 24 * 366; |
||
111 | |||
112 | for ($i = 0; $i <= $total; $i += 60) { |
||
113 | $current = $start + $i; |
||
114 | if ( |
||
115 | in_array((int) date('j', $current), $dates['dom']) && |
||
116 | in_array((int) date('n', $current), $dates['month']) && |
||
117 | in_array((int) date('w', $current), $dates['dow']) && |
||
118 | in_array((int) date('G', $current), $dates['hours']) && |
||
119 | in_array((int) date('i', $current), $dates['minutes']) |
||
120 | ) { |
||
121 | return $current; |
||
122 | } |
||
123 | } |
||
124 | |||
125 | return 0; |
||
126 | } |
||
159 |