Args::getDefaultThreshold()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 9
    public function __construct()
26
    {
27 9
        $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 1
    public function isBlocking(): bool
79
    {
80 1
        if ($this->hasOption(self::_B)) {
81 1
            return $this->_options[self::_B];
82
        }
83 1
        if ($this->hasOption(self::_BLOCKING)) {
84 1
            return $this->_options[self::_BLOCKING];
85
        }
86 1
        return false;
87
    }
88
89
    /**
90
     * set options
91
     *
92
     * @param array $opts
93
     * @return Args
94
     */
95 3
    protected function setOptions(array $opts = []): Args
96
    {
97 3
        $this->_options = (empty($opts))
98 3
            ? getopt(self::SOPTS, [
99 3
                self::_FILE . self::_DESC,
100 3
                self::_LINES . self::_DESC,
101 3
                self::_METHODS . self::_DESC,
102 3
                self::_STATEMENTS . self::_DESC,
103 3
                self::_CLASSES . self::_DESC,
104 3
                self::_BLOCKING . self::_DESC . self::_DESC,
105
            ])
106 3
            : $opts;
107 3
        return $this;
108
    }
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 1
            return $this->floatOption($kshort);
121
        }
122 1
        if ($this->hasOption($klong)) {
123 1
            return $this->floatOption($klong);
124
        }
125 1
        return $this->getDefaultThreshold();
126
    }
127
128
    /**
129
     * return true if option was set
130
     *
131
     * @param string $key
132
     * @return boolean
133
     */
134 1
    protected function hasOption(string $key): bool
135
    {
136 1
        return isset($this->_options[$key]);
137
    }
138
139
    /**
140
     * returns float threshold value from key arg
141
     *
142
     * @param string $key
143
     * @return float
144
     */
145 1
    protected function floatOption(string $key): float
146
    {
147 1
        return ($this->hasOption($key))
148 1
            ? (float) $this->_options[$key]
149 1
            : 0;
150
    }
151
152
    /**
153
     * return default threshold value
154
     *
155
     * @return float
156
     */
157 1
    protected function getDefaultThreshold(): float
158
    {
159 1
        return (float) self::DEFAULT_THRESHOLD;
160
    }
161
}
162