Passed
Push — master ( a5bee2...63ddd2 )
by Pierre
32:12 queued 15s
created

Args::isBlocking()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PierInfor\Undercover;
6
7
use PierInfor\Undercover\Interfaces\IArgs;
8
9
/**
10
 * Args is a command line argument checker
11
 *
12
 * @author Pierre Fromager <info@pier_infor.fr>
13
 * @package PierInfor\Undercover
14
 * @version 1.0
15
 */
16
class Args implements IArgs
17
{
18
19
    protected $_filename;
20
    protected $_options;
21
22
    /**
23
     * constructor
24
     */
25 8
    public function __construct()
26
    {
27 8
        $this->setOptions();
28
    }
29
30
    /**
31
     * return filename from params
32
     *
33
     * @return string
34
     */
35 1
    public function getFilename(): string
36
    {
37 1
        if ($this->hasOption(self::_F)) {
38 1
            return $this->_options[self::_F];
39
        }
40 1
        if ($this->hasOption(self::_FILE)) {
41 1
            return $this->_options[self::_FILE];
42
        }
43 1
        return self::DEFAULT_FILENAME;
44
    }
45
46
    /**
47
     * return thresholds from params
48
     *
49
     * @return array
50
     */
51 1
    public function getThresholds(): array
52
    {
53
        return [
54 1
            self::_LINES => (float) $this->getThresholdByKeys(
55 1
                self::_L,
56 1
                self::_LINES
57
            ),
58 1
            self::_METHODS => (float) $this->getThresholdByKeys(
59 1
                self::_M,
60 1
                self::_METHODS
61
            ),
62 1
            self::_STATEMENTS => (float) $this->getThresholdByKeys(
63 1
                self::_S,
64 1
                self::_STATEMENTS
65
            ),
66 1
            self::_CLASSES => (float) $this->getThresholdByKeys(
67 1
                self::_C,
68 1
                self::_CLASSES
69
            ),
70
        ];
71
    }
72
73
    /**
74
     * return true if blocking option was set
75
     *
76
     * @return bool
77
     */
78
    public function isBlocking(): bool
79 3
    {
80
        if ($this->hasOption(self::_B)) {
81 3
            return $this->_options[self::_B];
82 3
        }
83 3
        if ($this->hasOption(self::_BLOCKING)) {
84 3
            return $this->_options[self::_BLOCKING];
85 3
        }
86 3
        return false;
87 3
    }
88
89 3
    /**
90 3
     * set options
91
     *
92
     * @param array $opts
93
     * @return Args
94
     */
95
    protected function setOptions(array $opts = []): Args
96
    {
97
        $this->_options = (empty($opts))
98
            ? getopt(self::SOPTS, [
99
                self::_FILE . self::_DESC,
100 1
                self::_LINES . self::_DESC,
101
                self::_METHODS . self::_DESC,
102 1
                self::_STATEMENTS . self::_DESC,
103 1
                self::_CLASSES . self::_DESC,
104
                self::_BLOCKING . self::_DESC . self::_DESC,
105 1
            ])
106 1
            : $opts;
107
        return $this;
108 1
    }
109
110
    /**
111
     * return threshold from both short and long options
112
     *
113
     * @param string $kshort
114
     * @param string $klong
115
     * @return float
116
     */
117 1
    protected function getThresholdByKeys(string $kshort, string $klong): float
118
    {
119 1
        if ($this->hasOption($kshort)) {
120
            return $this->floatOption($kshort);
121
        }
122
        if ($this->hasOption($klong)) {
123
            return $this->floatOption($klong);
124
        }
125
        return $this->getDefaultThreshold();
126
    }
127
128 1
    /**
129
     * return true if option was set
130 1
     *
131 1
     * @param string $key
132 1
     * @return boolean
133
     */
134
    protected function hasOption(string $key): bool
135
    {
136
        return isset($this->_options[$key]);
137
    }
138
139
    /**
140 1
     * returns float threshold value from key arg
141
     *
142 1
     * @param string $key
143
     * @return float
144
     */
145
    protected function floatOption(string $key): float
146
    {
147
        return ($this->hasOption($key))
148
            ? (float) $this->_options[$key]
149
            : 0;
150
    }
151
152
    /**
153
     * return default threshold value
154
     *
155
     * @return float
156
     */
157
    protected function getDefaultThreshold(): float
158
    {
159
        return (float) self::DEFAULT_THRESHOLD;
160
    }
161
}
162