Total Complexity | 41 |
Total Lines | 242 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 2 | Features | 0 |
Complex classes like Input often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Input, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Input |
||
19 | { |
||
20 | /** |
||
21 | * Get file size from User. |
||
22 | * |
||
23 | * @param string $message Message for User what he must type |
||
24 | * @param string $default Default file size for empty input. Default is '1MB' |
||
25 | * |
||
26 | * @return int|string Inserted file size in bytes |
||
27 | * |
||
28 | * @see Input::getBytesFromString() |
||
29 | */ |
||
30 | public static function getFileSize($message, $default = '1MB') |
||
31 | { |
||
32 | echo $message.' [Default: '.$default.']: '; |
||
33 | |||
34 | do { |
||
35 | $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN)); |
||
36 | |||
37 | $isDefault = false; |
||
38 | |||
39 | if (is_null($input) || empty($input)) { |
||
40 | Text::debug('Using default input: '.$default); |
||
41 | $isDefault = true; |
||
42 | break; |
||
43 | } |
||
44 | |||
45 | $checkInput = self::getBytesFromString($input); |
||
46 | |||
47 | if (!$checkInput['error']) { |
||
48 | $input = $checkInput['bytes']; |
||
49 | break; |
||
50 | } |
||
51 | |||
52 | echo 'Please input valid file size: '; |
||
53 | } while (1); |
||
54 | |||
55 | if ($isDefault) { |
||
56 | // Check $default just for sure |
||
57 | $checkDefault = self::getBytesFromString($default); |
||
58 | |||
59 | // 1 MB in bytes |
||
60 | $input = 1048576; |
||
61 | |||
62 | if (!$checkDefault['error']) { |
||
63 | $input = $checkDefault['bytes']; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | return $input; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Get valid filename from User. |
||
72 | * |
||
73 | * @param string $message Message for User what he must type |
||
74 | * @param string $default Default filename for empty input. Default is 'output' |
||
75 | * |
||
76 | * @return string Inserted filename |
||
77 | * |
||
78 | * @see Input::isFilenameWrong() |
||
79 | */ |
||
80 | public static function getFilename($message, $default = 'output') |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get bytes number from unit conversation. |
||
103 | * |
||
104 | * @param string $input User input e.g. '5B', '5MB' |
||
105 | * @param int $notation Notation type for kilo |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public static function getBytesFromString($input, $notation = 1024) |
||
110 | { |
||
111 | $error = true; |
||
112 | $bytes = 0; |
||
113 | |||
114 | // https://regex101.com/r/v936BS/2 |
||
115 | if (preg_match('/^(?<number>\d+(?!\.+\,+\d*))\s*(?<unit>B|KB|MB|GB|TB)$/', $input, $matches)) { |
||
116 | $error = false; |
||
117 | } |
||
118 | |||
119 | if ($error) { |
||
120 | return array('error' => $error, 'bytes' => $bytes); |
||
121 | } |
||
122 | |||
123 | switch ($matches['unit']) { |
||
124 | case 'KB': |
||
125 | $bytes = $matches['number'] * $notation; |
||
126 | break; |
||
127 | case 'MB': |
||
128 | $bytes = $matches['number'] * pow($notation, 2); |
||
129 | break; |
||
130 | case 'GB': |
||
131 | $bytes = $matches['number'] * pow($notation, 3); |
||
132 | break; |
||
133 | case 'TB': |
||
134 | $bytes = $matches['number'] * pow($notation, 4); |
||
135 | break; |
||
136 | // As Bytes |
||
137 | default: |
||
138 | $bytes = $matches['number']; |
||
139 | break; |
||
140 | } |
||
141 | |||
142 | return array('error' => $error, 'bytes' => $bytes); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Get number from User. |
||
147 | * |
||
148 | * @param string $message Message for User what he must type |
||
149 | * @param int|float $default Default number for empty input. Default is 0 |
||
150 | * |
||
151 | * @return float Inserted number |
||
152 | */ |
||
153 | public static function getNumber($message, $default = 0) |
||
154 | { |
||
155 | echo $message.' [Default: '.$default.']: '; |
||
156 | |||
157 | do { |
||
158 | $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN)); |
||
159 | |||
160 | if (is_null($input) || empty($input)) { |
||
161 | Text::debug('Using default input: '.$default); |
||
162 | $input = $default; |
||
163 | } elseif (!is_numeric($input)) { |
||
164 | echo 'Please input number: '; |
||
165 | } |
||
166 | } while (!is_numeric($input)); |
||
167 | |||
168 | return (float) $input; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Get User confirmation. Default is YES. |
||
173 | * |
||
174 | * @return bool Confirmation result |
||
175 | */ |
||
176 | public static function getUserConfirm() |
||
177 | { |
||
178 | while (1) { |
||
179 | echo 'Do you really want to continue? [Y/n]: '; |
||
180 | |||
181 | $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN)); |
||
182 | |||
183 | // Default is YES |
||
184 | if (is_null($input) || empty($input) || strtolower($input) == 'y' || strtolower($input) == 'yes') { |
||
185 | return true; |
||
186 | } elseif (strtolower($input) == 'n' || strtolower($input) == 'no') { |
||
187 | return false; |
||
188 | } |
||
189 | } |
||
190 | |||
191 | // Fix missing return statement warning. Return true... |
||
192 | return true; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Check for wrong filename based on OS. |
||
197 | * |
||
198 | * @param string $input User filename input |
||
199 | * |
||
200 | * @return bool Is filename wrong? |
||
201 | */ |
||
202 | public static function isFilenameWrong($input) |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Get exists filename. |
||
226 | * |
||
227 | * @param string $message Message for User what he must type |
||
228 | * @param string $default Default filename for empty input. Default is 'output' |
||
229 | * @param string $fileExtension File extension. Default is '.dat' |
||
230 | * |
||
231 | * @return string |
||
232 | * |
||
233 | * @see Input::getFilename() |
||
234 | */ |
||
235 | public static function getExistsFilename($message, $default = 'output', $fileExtension = '.dat') |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Terminate script if User denied on confirmation. |
||
253 | * |
||
254 | * @see Input::getUserConfirm() |
||
255 | */ |
||
256 | public static function dieOnDenyUserConfirm() |
||
263 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.