| Conditions | 10 |
| Paths | 15 |
| Total Lines | 61 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 23 | public function run(): string|int |
||
| 24 | { |
||
| 25 | $docroot = $this->docroot; |
||
| 26 | $port = (string)$this->port; |
||
| 27 | |||
| 28 | try { |
||
| 29 | $sizeStr = trim(exec('stty size')); |
||
| 30 | if ($sizeStr) { |
||
| 31 | $size = explode(' ', $sizeStr); |
||
| 32 | $columns = $size[1]; |
||
| 33 | } else { |
||
| 34 | $columns = '80'; |
||
| 35 | } |
||
| 36 | } catch (Throwable) { |
||
| 37 | $columns = '80'; |
||
| 38 | } |
||
| 39 | |||
| 40 | $opts = new Opts(); |
||
| 41 | $host = $opts->get('-h', $opts->get('--host', 'localhost')); |
||
| 42 | $port = $opts->get('-p', $opts->get('--port', $port)); |
||
| 43 | $filter = $opts->get('-f', $opts->get('--filter', '')); |
||
| 44 | $quiet = $opts->has('-q'); |
||
| 45 | |||
| 46 | $descriptors = [ |
||
| 47 | 0 => ['pipe', 'r'], |
||
| 48 | 1 => ['pipe', 'w'], |
||
| 49 | 2 => ['pipe', 'w'], |
||
| 50 | ]; |
||
| 51 | $process = proc_open( |
||
| 52 | 'CONIA_CLI_SERVER=1 ' . |
||
| 53 | "CONIA_DOCUMENT_ROOT={$docroot} " . |
||
| 54 | "CONIA_TERMINAL_COLUMNS={$columns} " . |
||
| 55 | "php -S {$host}:{$port} " . |
||
| 56 | ($quiet ? '-q ' : '') . |
||
| 57 | " -t {$docroot}" . DIRECTORY_SEPARATOR . ' ' . __DIR__ . DIRECTORY_SEPARATOR . 'CliRouter.php ', |
||
| 58 | $descriptors, |
||
| 59 | $pipes |
||
| 60 | ); |
||
| 61 | |||
| 62 | if (is_resource($process)) { |
||
| 63 | while (!feof($pipes[1])) { |
||
| 64 | $output = fgets($pipes[2], 1024); |
||
| 65 | |||
| 66 | if (strlen($output) === 0) { |
||
| 67 | break; |
||
| 68 | } |
||
| 69 | |||
| 70 | if (!preg_match('/^\[.*?\] \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}/', $output)) { |
||
| 71 | $pos = (int)strpos($output, ']'); |
||
| 72 | |||
| 73 | if (!$filter || !preg_match($filter, substr($output, (int)strpos($output, '/')))) { |
||
| 74 | echo substr($output, $pos + 2); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | fclose($pipes[1]); |
||
| 80 | proc_close($process); |
||
| 81 | } |
||
| 82 | |||
| 83 | return 0; |
||
| 84 | } |
||
| 86 |