| Conditions | 4 |
| Paths | 5 |
| Total Lines | 76 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 45 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 46 | { |
||
| 47 | $io = new SymfonyStyle($input, $output); |
||
| 48 | $io->title('Yii3 Debug Server'); |
||
| 49 | $io->writeln('https://yiiframework.com' . "\n"); |
||
| 50 | |||
| 51 | $env = $input->getOption('env'); |
||
| 52 | if ($env === 'test') { |
||
| 53 | return ExitCode::OK; |
||
| 54 | } |
||
| 55 | |||
| 56 | $socket = Connection::create(); |
||
| 57 | $socket->bind(); |
||
| 58 | |||
| 59 | //if (socket_recvfrom($socket, $buf, 64 * 1024, 0, $source) === false) { |
||
| 60 | // echo "recv_from failed"; |
||
| 61 | //} |
||
| 62 | //var_dump($buf); |
||
| 63 | // |
||
| 64 | //return 0; |
||
| 65 | //if (!@socket_bind($socket, $address, $port)) { |
||
| 66 | // $io->error( |
||
| 67 | // sprintf( |
||
| 68 | // 'Address "%s" is already taken by another process.', |
||
| 69 | // $this->address . ':' . $this->port, |
||
| 70 | // ) |
||
| 71 | // ); |
||
| 72 | // $io->info( |
||
| 73 | // sprintf( |
||
| 74 | // 'Would you like to kill the process and rerun?' |
||
| 75 | // ) |
||
| 76 | // ); |
||
| 77 | // $result = $io->ask('Would you like to kill the process and rerun? (Y/n)'); |
||
| 78 | // |
||
| 79 | // if ($result === 'Y') { |
||
| 80 | // // todo: change to finding a process by opened port |
||
| 81 | // $pid = shell_exec( |
||
| 82 | // sprintf( |
||
| 83 | // 'ps | grep "php ./yii dev"', |
||
| 84 | // //$this->port, |
||
| 85 | // ) |
||
| 86 | // ); |
||
| 87 | // $io->info( |
||
| 88 | // sprintf( |
||
| 89 | // 'Killing the process with ID: "%s".', |
||
| 90 | // $pid, |
||
| 91 | // ) |
||
| 92 | // ); |
||
| 93 | // //shell_exec('kill ' . $pid); |
||
| 94 | // } |
||
| 95 | // //return ExitCode::IOERR; |
||
| 96 | //} |
||
| 97 | |||
| 98 | $io->success( |
||
| 99 | sprintf( |
||
| 100 | 'Listening on "%s:%d".', |
||
| 101 | $this->address, |
||
| 102 | $this->port, |
||
| 103 | ) |
||
| 104 | ); |
||
| 105 | |||
| 106 | if (\function_exists('pcntl_signal')) { |
||
| 107 | $io->success('Quit the server with CTRL-C or COMMAND-C.'); |
||
| 108 | |||
| 109 | \pcntl_signal(\SIGINT, static function () use ($socket): void { |
||
| 110 | $socket->close(); |
||
| 111 | exit(1); |
||
|
|
|||
| 112 | }); |
||
| 113 | } |
||
| 114 | |||
| 115 | $messages = $socket->read(fn (string $buffer) => $buffer); |
||
| 116 | foreach ($messages as $message) { |
||
| 117 | $io->writeln($message); |
||
| 118 | $io->newLine(); |
||
| 119 | } |
||
| 120 | return ExitCode::OK; |
||
| 121 | } |
||
| 123 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.