Validator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A multithread() 0 3 2
A maxChildRuntime() 0 7 1
A laggerTimeout() 0 7 1
A cronExpression() 0 26 5
A forkLimit() 0 7 1
A niceness() 0 7 1
1
<?php namespace Comodojo\Extender\Utils;
2
3
use \Comodojo\Foundation\Validation\DataFilter;
4
use \Cron\CronExpression;
5
use \Exception;
6
7
/**
8
 * @package     Comodojo Extender
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @license     MIT
11
 *
12
 * LICENSE:
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
 * THE SOFTWARE.
21
 */
22
23
class Validator {
24
25
    /**
26
     * Validate a cron expression and, if valid, return next run timestamp plus
27
     * an array of expression parts
28
     *
29
     * @param   string  $expression
30
     *
31
     * @return  array   Next run timestamp at first position, expression parts at second
32
     * @throws  \Exception
33
     */
34
    public static function cronExpression($expression) {
35
36
        try {
37
38
            $cron = CronExpression::factory($expression);
39
40
            $s = $cron->getNextRunDate()->format('c');
41
42
            $e = $cron->getExpression();
43
44
            $e_array = preg_split('/\s/', $e, -1, PREG_SPLIT_NO_EMPTY);
45
46
            $e_count = count($e_array);
0 ignored issues
show
Bug introduced by
It seems like $e_array can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            $e_count = count(/** @scrutinizer ignore-type */ $e_array);
Loading history...
47
48
            if ( $e_count < 5 || $e_count > 6 ) throw new Exception($e." is not a valid cron expression");
49
50
            if ( $e_count == 5 ) $e_array[] = "*";
51
52
        }
53
        catch (Exception $e) {
54
55
            throw $e;
56
57
        }
58
59
        return array($s, $e_array);
60
61
    }
62
63
    public static function laggerTimeout($timeout) {
64
65
        return DataFilter::filterInteger(
66
            $timeout,
67
            0,
0 ignored issues
show
Bug introduced by
0 of type integer is incompatible with the type array expected by parameter $min of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
            /** @scrutinizer ignore-type */ 0,
Loading history...
68
            PHP_INT_MAX,
0 ignored issues
show
Bug introduced by
Comodojo\Extender\Utils\PHP_INT_MAX of type integer is incompatible with the type array expected by parameter $max of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            /** @scrutinizer ignore-type */ PHP_INT_MAX,
Loading history...
69
            5
0 ignored issues
show
Bug introduced by
5 of type integer is incompatible with the type array expected by parameter $default of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
            /** @scrutinizer ignore-type */ 5
Loading history...
70
        );
71
72
    }
73
74
    public static function maxChildRuntime($runtime) {
75
76
        return DataFilter::filterInteger(
77
            $runtime,
78
            1,
0 ignored issues
show
Bug introduced by
1 of type integer is incompatible with the type array expected by parameter $min of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            /** @scrutinizer ignore-type */ 1,
Loading history...
79
            PHP_INT_MAX,
0 ignored issues
show
Bug introduced by
Comodojo\Extender\Utils\PHP_INT_MAX of type integer is incompatible with the type array expected by parameter $max of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            /** @scrutinizer ignore-type */ PHP_INT_MAX,
Loading history...
80
            600
0 ignored issues
show
Bug introduced by
600 of type integer is incompatible with the type array expected by parameter $default of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            /** @scrutinizer ignore-type */ 600
Loading history...
81
        );
82
83
    }
84
85
    public static function forkLimit($limit) {
86
87
        return DataFilter::filterInteger(
88
            $limit,
89
            0,
0 ignored issues
show
Bug introduced by
0 of type integer is incompatible with the type array expected by parameter $min of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
            /** @scrutinizer ignore-type */ 0,
Loading history...
90
            PHP_INT_MAX,
0 ignored issues
show
Bug introduced by
Comodojo\Extender\Utils\PHP_INT_MAX of type integer is incompatible with the type array expected by parameter $max of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
            /** @scrutinizer ignore-type */ PHP_INT_MAX,
Loading history...
91
            5
0 ignored issues
show
Bug introduced by
5 of type integer is incompatible with the type array expected by parameter $default of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
            /** @scrutinizer ignore-type */ 5
Loading history...
92
        );
93
94
    }
95
96
    public static function niceness($niceness) {
97
98
        return DataFilter::filterInteger(
99
            $niceness,
100
            -20,
0 ignored issues
show
Bug introduced by
-20 of type integer is incompatible with the type array expected by parameter $min of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
            /** @scrutinizer ignore-type */ -20,
Loading history...
101
            20,
0 ignored issues
show
Bug introduced by
20 of type integer is incompatible with the type array expected by parameter $max of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
            /** @scrutinizer ignore-type */ 20,
Loading history...
102
            0
0 ignored issues
show
Bug introduced by
0 of type integer is incompatible with the type array expected by parameter $default of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
            /** @scrutinizer ignore-type */ 0
Loading history...
103
        );
104
105
    }
106
107
    public static function multithread($multithread) {
108
109
        return $multithread === true && Checks::multithread();
110
111
    }
112
113
}
114