| Total Complexity | 50 |
| Total Lines | 369 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ShellUtils 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 ShellUtils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | abstract class ShellUtils |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * helper functions |
||
| 35 | */ |
||
| 36 | use UtilsTrait; |
||
| 37 | |||
| 38 | const OUTPUT_JSON = 'json'; |
||
| 39 | const OUTPUT_DEFAULT = 'default'; |
||
| 40 | const OUTPUT_PLAINTEXT = 'plaintext'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string $outputFormat |
||
| 44 | */ |
||
| 45 | protected static $outputFormat = self::OUTPUT_DEFAULT; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * |
||
| 49 | */ |
||
| 50 | protected static function isDefaultOuput() |
||
| 51 | { |
||
| 52 | return self::$outputFormat === self::OUTPUT_DEFAULT; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Prints title action banner |
||
| 57 | * |
||
| 58 | * @access protected |
||
| 59 | * @static |
||
| 60 | * @param string $title |
||
| 61 | * |
||
| 62 | * @return void |
||
| 63 | */ |
||
| 64 | protected static function printTitle(string $title) |
||
| 65 | { |
||
| 66 | if (self::isDefaultOuput()) { |
||
| 67 | Console::log(); |
||
| 68 | Console::log($title); |
||
| 69 | Console::log(); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Print temp message during api request |
||
| 75 | * |
||
| 76 | * @access protected |
||
| 77 | * @static |
||
| 78 | * @param array $arguments |
||
| 79 | * |
||
| 80 | * @return void |
||
| 81 | */ |
||
| 82 | protected static function printTempMessage() |
||
| 83 | { |
||
| 84 | if (self::isDefaultOuput()) { |
||
| 85 | Console::reLog(Console::text(' ? ', 'green') . Console::text('waiting for api response', 'white') . Console::text(' ... ', 'green')); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Clear the temp message set during api request |
||
| 91 | * |
||
| 92 | * @access protected |
||
| 93 | * @static |
||
| 94 | * @param array $arguments |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | protected static function clearTempMessage() |
||
| 99 | { |
||
| 100 | if (self::isDefaultOuput()) { |
||
| 101 | // long blank string to overwrite previous message |
||
| 102 | Console::reLog(' '); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Print to banner |
||
| 108 | * |
||
| 109 | * @access protected |
||
| 110 | * @static |
||
| 111 | * @param array $arguments |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | protected static function printLogo() |
||
| 116 | { |
||
| 117 | if (self::isDefaultOuput()) { |
||
| 118 | //Console::log(" _ _ _ __ __ ", "darkgray"); |
||
| 119 | //Console::log(" | |___ _(_)__| |_ _ _ / _|/ _| ", "darkgray"); |
||
| 120 | //Console::log(" | / / '_| (_-< _| || | _| _| ", "darkgray"); |
||
| 121 | //Console::log(" |_\_\_| |_/__/\__|\_,_|_| |_| ", "darkgray"); |
||
| 122 | Console::log(" _ ___ ___ ___ ___ ", "darkgray"); |
||
| 123 | Console::log(" __ _| |__ _ _ ___ ___|_ _| _ \ \| _ ) ", "darkgray"); |
||
| 124 | Console::log(" / _` | '_ \ || (_-</ -_)| || _/ |) | _ \ ", "darkgray"); |
||
| 125 | Console::log(" \__,_|_.__/\_,_/__/\___|___|_| |___/|___/ ", "darkgray"); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Print version |
||
| 131 | * |
||
| 132 | * @access protected |
||
| 133 | * @static |
||
| 134 | * @param array $arguments |
||
| 135 | * |
||
| 136 | * @return void |
||
| 137 | */ |
||
| 138 | protected static function printVersion() |
||
| 139 | { |
||
| 140 | self::printLogo(); |
||
| 141 | |||
| 142 | Console::log(); |
||
| 143 | Console::log(Console::text(' Kristuff/AbuseIPDB Client version: ', 'darkgray') . Console::text(AbuseIPDBClient::VERSION, 'lightgray')); |
||
| 144 | Console::log(Console::text(' Kristuff/AbuseIPDB Core version: ', 'darkgray') . Console::text(ApiHandler::VERSION, 'lightgray')); |
||
| 145 | Console::log(Console::text(' --------------------------------------------------', 'darkgray')); |
||
| 146 | Console::log(Console::text(' Released under the MIT licence', 'darkgray')); |
||
| 147 | Console::log(Console::text(' Made with ', 'darkgray') . Console::text('♥', 'red') . Console::text(' in France', 'darkgray')); |
||
| 148 | Console::log( |
||
| 149 | Console::text(' © 2020-2021 Kristuff (', 'darkgray'). |
||
| 150 | Console::text('https://github.com/kristuff', 'darkgray', 'underlined'). |
||
| 151 | Console::text(')', 'darkgray') |
||
| 152 | ); |
||
| 153 | Console::log(Console::text(' --------------------------------------------------', 'darkgray')); |
||
| 154 | Console::log(); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Print app banner |
||
| 159 | * |
||
| 160 | * @access protected |
||
| 161 | * @static |
||
| 162 | * |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | protected static function printBanner() |
||
| 166 | { |
||
| 167 | if (self::isDefaultOuput()) { |
||
| 168 | Console::log(); |
||
| 169 | Console::log( Console::text(' Kristuff\AbuseIPDB ', 'darkgray') . Console::text(' ' . AbuseIPDBClient::VERSION . ' ', 'white', 'blue')); |
||
| 170 | Console::log(Console::text(' Made with ', 'darkgray') . Console::text('♥', 'red') . Console::text(' in France', 'darkgray')); |
||
| 171 | Console::log(' © 2020-2021 Kristuff', 'darkgray'); |
||
| 172 | Console::log(); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Print footer |
||
| 178 | * |
||
| 179 | * @access protected |
||
| 180 | * @static |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | protected static function printFooter(string $requestTime = '') |
||
| 185 | { |
||
| 186 | if (self::isDefaultOuput()) { |
||
| 187 | if (!empty($requestTime)){ |
||
| 188 | $date_utc = new \DateTime("now", new \DateTimeZone("UTC")); |
||
| 189 | Console::log( |
||
| 190 | Console::text(' Request time: ', 'darkgray') . Console::text($requestTime . 's', 'lightgray'). |
||
| 191 | Console::text(' | UTC time: ', 'darkgray') . Console::text($date_utc->format('Y-m-d H:i:s'), 'lightgray') |
||
| 192 | ); |
||
| 193 | } |
||
| 194 | Console::log(Console::text(' ------------------------------------------------------------------------------------------------------', 'darkgray')); |
||
| 195 | Console::log( |
||
| 196 | Console::text(' Kristuff\AbuseIPDB ', 'darkgray') . |
||
| 197 | Console::text(AbuseIPDBClient::VERSION, 'lightgray') . |
||
| 198 | Console::text(' | Made with ', 'darkgray') . |
||
| 199 | Console::text('♥', 'red') . |
||
| 200 | Console::text(' in France | © 2020-2021 Kristuff (https://github.com/kristuff)', 'darkgray') |
||
| 201 | ); |
||
| 202 | Console::log(); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Prints/gets a result value |
||
| 208 | * |
||
| 209 | * @access protected |
||
| 210 | * @static |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | protected static function printResult($text, $value, string $foregroundColor = 'lightred', string $backgroundColor = '', bool $print = true) |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Prints score badge |
||
| 230 | * |
||
| 231 | * @access protected |
||
| 232 | * @static |
||
| 233 | * @param string $text |
||
| 234 | * @param int $score |
||
| 235 | * @param string $textColor |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | protected static function getScoreBadge(int $score, string $padding = ' ') |
||
| 240 | { |
||
| 241 | $scoreforegroundColor = 'white'; |
||
| 242 | $scoreBackgroundColor = 'green'; |
||
| 243 | |||
| 244 | if (intval($score) > 0 ){ |
||
| 245 | $scoreforegroundColor = 'black'; |
||
| 246 | $scoreBackgroundColor = 'yellow'; |
||
| 247 | } |
||
| 248 | if (intval($score) > 50 ){ |
||
| 249 | $scoreforegroundColor = 'white'; |
||
| 250 | $scoreBackgroundColor = 'red'; |
||
| 251 | } |
||
| 252 | |||
| 253 | $badge = str_pad($score, 3, ' ',STR_PAD_LEFT); |
||
| 254 | return Console::text($padding.$badge.$padding, $scoreforegroundColor, $scoreBackgroundColor); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Check and print errors in API response. Null response object is considered as no errors |
||
| 259 | * |
||
| 260 | * @access protected |
||
| 261 | * @static |
||
| 262 | * @param object $response |
||
| 263 | * @param bool $checkForEmpty |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | protected static function printErrors($response, bool $checkForEmpty = true) |
||
| 268 | { |
||
| 269 | if (isset($response) && isset($response->errors)){ |
||
| 270 | |||
| 271 | switch (self::$outputFormat){ |
||
| 272 | case self::OUTPUT_DEFAULT: |
||
| 273 | // top error badge |
||
| 274 | Console::log(' ' . Console::text(' ERROR ','white', 'red')); |
||
| 275 | |||
| 276 | $num = 0; |
||
| 277 | // errors is an array, could have more than one error.. |
||
| 278 | foreach ($response->errors as $err){ |
||
| 279 | $num++; |
||
| 280 | |||
| 281 | Console::log(Console::text(' ✗', 'red') . self::printResult(' Number: ', $num, 'lightyellow','', false)); |
||
| 282 | self::printResult(' Status: ', $err->status ?? null, 'lightyellow',''); |
||
| 283 | |||
| 284 | if (!empty($err->source) && !empty($err->source->parameter)){ |
||
| 285 | self::printResult(' Parameter: ', $err->source->parameter, 'lightyellow'); |
||
| 286 | } |
||
| 287 | self::printResult(' Title: ', $err->title ?? null, 'lightyellow'); |
||
| 288 | self::printResult(' Detail: ', $err->detail ?? null, 'lightyellow'); |
||
| 289 | |||
| 290 | // separate errors |
||
| 291 | if (count($response->errors) > 1){ |
||
| 292 | Console::log(' ---'); |
||
| 293 | } |
||
| 294 | |||
| 295 | } |
||
| 296 | Console::log(); |
||
| 297 | break; |
||
| 298 | |||
| 299 | case self::OUTPUT_PLAINTEXT: |
||
| 300 | foreach ($response->errors as $err){ |
||
| 301 | $text = 'Error: '; |
||
| 302 | $text .= !empty($err->title) ? ' title: ' . $err->title : ''; |
||
| 303 | $text .= !empty($err->status) ? ' status: ' . $err->status : ''; |
||
| 304 | $text .= !empty($err->source) && !empty($err->source->parameter) ? ' parameter: ' . $err->source->parameter : ''; |
||
| 305 | $text .= !empty($err->detail) ? ' detail: ' . $err->detail : ''; |
||
| 306 | $text .= PHP_EOL; |
||
| 307 | echo $text; |
||
| 308 | } |
||
| 309 | break; |
||
| 310 | |||
| 311 | case self::OUTPUT_JSON: |
||
| 312 | echo json_encode($response, JSON_PRETTY_PRINT); |
||
| 313 | break; |
||
| 314 | |||
| 315 | } |
||
| 316 | return true; |
||
| 317 | } |
||
| 318 | |||
| 319 | // check for empty response ? |
||
| 320 | if ( $checkForEmpty && ( empty($response) || empty($response->data)) ){ |
||
| 321 | self::error('An unexpected error occurred.'); |
||
| 322 | return true; |
||
| 323 | } |
||
| 324 | |||
| 325 | return false; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Print a single error |
||
| 330 | * |
||
| 331 | * @access protected |
||
| 332 | * @static |
||
| 333 | * @param string $error The error message |
||
| 334 | * |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | protected static function error($error) |
||
| 338 | { |
||
| 339 | if (self::isDefaultOuput()) { |
||
| 340 | // ✗ |
||
| 341 | Console::log(' ' . Console::text(' ERROR ','white', 'red')); |
||
| 342 | Console::log( |
||
| 343 | Console::text(' ✗', 'red') . |
||
| 344 | Console::text(' Detail: ', 'white') . |
||
| 345 | Console::text($error, 'lightyellow') . |
||
| 346 | Console::text('', 'white') |
||
| 347 | ); |
||
| 348 | Console::log(); |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * helper to validate a condition or exit with an error |
||
| 354 | * |
||
| 355 | * @access protected |
||
| 356 | * @static |
||
| 357 | * @param bool $condition The condition to evaluate |
||
| 358 | * @param string $message Error message |
||
| 359 | * @param bool $print True to print error. Default is true |
||
| 360 | * |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | protected static function validate(bool $condition, string $message, bool $print = true) |
||
| 364 | { |
||
| 365 | if ( !$condition ){ |
||
| 366 | if ($print && self::isDefaultOuput()) { |
||
| 367 | Console::log(); |
||
| 368 | self::error($message); |
||
| 369 | self::printFooter(); |
||
| 370 | } |
||
| 371 | Program::exit(1); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get numeric parameter and exit on error |
||
| 377 | * |
||
| 378 | * @access protected |
||
| 379 | * @static |
||
| 380 | * @param array $arguments |
||
| 381 | * @param string $shortArg The short argument name |
||
| 382 | * @param string $longArg The long argument name |
||
| 383 | * @param int $defaultValue |
||
| 384 | * |
||
| 385 | * @return int |
||
| 386 | */ |
||
| 387 | protected static function getNumericParameter(array $arguments, string $shortArg, string $longArg, int $defaultValue) |
||
| 400 | } |
||
| 401 | |||
| 402 | } |