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') |
||
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') |
||
81 | { |
||
82 | echo $message.' [Default: '.$default.']: '; |
||
83 | |||
84 | do { |
||
85 | $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN)); |
||
86 | |||
87 | // Check for wrong filename based on OS |
||
88 | $isInputWrong = self::isFilenameWrong($input); |
||
89 | |||
90 | if (is_null($input) || empty($input)) { |
||
91 | Text::debug('Using default input: '.$default); |
||
92 | $input = $default; |
||
93 | } elseif ($isInputWrong) { |
||
94 | echo 'Please input valid filename: '; |
||
95 | } |
||
96 | } while ($isInputWrong); |
||
97 | |||
98 | return $input; |
||
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() |
||
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) |
||
203 | { |
||
204 | /* |
||
205 | * Invalid characters in files: |
||
206 | * (Windows) \/:*?"<>| |
||
207 | * (Linux) / |
||
208 | */ |
||
209 | |||
210 | $lastCharacter = substr($input, -1); |
||
211 | |||
212 | // Only last character because / and \ are for folders |
||
213 | $isInputWrong = $lastCharacter === '/' || $lastCharacter === '\\'; |
||
214 | |||
215 | // Running under Windows? |
||
216 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
||
217 | // https://regex101.com/r/wR5d0J/2/ |
||
218 | $isInputWrong = $isInputWrong || preg_match('/[:*?"<>|]/', $input); |
||
219 | } |
||
220 | |||
221 | return $isInputWrong; |
||
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() |
||
260 | } |
||
261 | } |
||
263 |