Includes/Input.php
<?php/** * Includes/Input.php. * * @author Adam "Saibamen" Stachowicz <saibamenppl@gmail.com> * @license MIT * * @link https://github.com/Saibamen/Generate-Sort-Numbers */ namespace Includes; require_once 'Text.php'; /** * Functions for receiving input from User. */class Input{ /** * Get file size from User. * * @param string $message Message for User what he must type * @param string $default Default file size for empty input. Default is '1MB' * * @return int|string Inserted file size in bytes * * @see Input::getBytesFromString() */Function `getFileSize` has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. public static function getFileSize($message, $default = '1MB') { echo $message.' [Default: '.$default.']: '; do { $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN)); $isDefault = false; if (is_null($input) || empty($input)) { Text::debug('Using default input: '.$default); $isDefault = true; break; } $checkInput = self::getBytesFromString($input); if (!$checkInput['error']) { $input = $checkInput['bytes']; break; } echo 'Please input valid file size: '; } while (1); if ($isDefault) { // Check $default just for sure $checkDefault = self::getBytesFromString($default); // 1 MB in bytes $input = 1048576; if (!$checkDefault['error']) { $input = $checkDefault['bytes']; } } return $input; } /** * Get valid filename from User. * * @param string $message Message for User what he must type * @param string $default Default filename for empty input. Default is 'output' * * @return string Inserted filename * * @see Input::isFilenameWrong() */ public static function getFilename($message, $default = 'output') { echo $message.' [Default: '.$default.']: '; do { $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN)); // Check for wrong filename based on OS $isInputWrong = self::isFilenameWrong($input); if (is_null($input) || empty($input)) { Text::debug('Using default input: '.$default); $input = $default; } elseif ($isInputWrong) { echo 'Please input valid filename: '; } } while ($isInputWrong); return $input; } /** * Get bytes number from unit conversation. * * @param string $input User input e.g. '5B', '5MB' * @param int $notation Notation type for kilo * * @return array */Method `getBytesFromString` has 26 lines of code (exceeds 25 allowed). Consider refactoring.
Function `getBytesFromString` has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. public static function getBytesFromString($input, $notation = 1024) { $error = true; $bytes = 0; // https://regex101.com/r/v936BS/2 if (preg_match('/^(?<number>\d+(?!\.+\,+\d*))\s*(?<unit>B|KB|MB|GB|TB)$/', $input, $matches)) { $error = false; } if ($error) { return array('error' => $error, 'bytes' => $bytes); } switch ($matches['unit']) { case 'KB': $bytes = $matches['number'] * $notation; break; case 'MB': $bytes = $matches['number'] * pow($notation, 2); break; case 'GB': $bytes = $matches['number'] * pow($notation, 3); break; case 'TB': $bytes = $matches['number'] * pow($notation, 4); break; // As Bytes default: $bytes = $matches['number']; break; } return array('error' => $error, 'bytes' => $bytes); } /** * Get number from User. * * @param string $message Message for User what he must type * @param int|float $default Default number for empty input. Default is 0 * * @return float Inserted number */ public static function getNumber($message, $default = 0) { echo $message.' [Default: '.$default.']: '; do { $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN)); if (is_null($input) || empty($input)) { Text::debug('Using default input: '.$default); $input = $default; } elseif (!is_numeric($input)) { echo 'Please input number: '; } } while (!is_numeric($input)); return (float) $input; } /** * Get User confirmation. Default is YES. * * @return bool Confirmation result */ public static function getUserConfirm() { while (1) { echo 'Do you really want to continue? [Y/n]: '; $input = trim(fgets(/** @scrutinizer ignore-type */ STDIN)); // Default is YES if (is_null($input) || empty($input) || strtolower($input) == 'y' || strtolower($input) == 'yes') { return true; } elseif (strtolower($input) == 'n' || strtolower($input) == 'no') { return false; } } // Fix missing return statement warning. Return true... return true; } /** * Check for wrong filename based on OS. * * @param string $input User filename input * * @return bool Is filename wrong? */ public static function isFilenameWrong($input) { /* * Invalid characters in files: * (Windows) \/:*?"<>| * (Linux) / */ $lastCharacter = substr($input, -1); // Only last character because / and \ are for folders $isInputWrong = $lastCharacter === '/' || $lastCharacter === '\\'; // Running under Windows? if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { // https://regex101.com/r/wR5d0J/2/ $isInputWrong = $isInputWrong || preg_match('/[:*?"<>|]/', $input); } return $isInputWrong; } /** * Get exists filename. * * @param string $message Message for User what he must type * @param string $default Default filename for empty input. Default is 'output' * @param string $fileExtension File extension. Default is '.dat' * * @return string * * @see Input::getFilename() */ public static function getExistsFilename($message, $default = 'output', $fileExtension = '.dat') { $filename = self::getFilename($message, $default); do { $error = false; if (!file_exists($filename.$fileExtension)) { $error = true; $filename = self::getFilename('Please type existing file', $default); } } while ($error); return $filename; } /** * Terminate script if User denied on confirmation. * * @see Input::getUserConfirm() */ public static function dieOnDenyUserConfirm() { if (!self::getUserConfirm()) { die('Script terminated by user.'); } }}