Conditions | 7 |
Paths | 5 |
Total Lines | 91 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 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".', |
||
101 | $socket->getUri(), |
||
102 | ) |
||
103 | ); |
||
104 | |||
105 | if (\function_exists('pcntl_signal')) { |
||
106 | $io->success('Quit the server with CTRL-C or COMMAND-C.'); |
||
107 | |||
108 | \pcntl_signal(\SIGINT, static function () use ($socket): void { |
||
109 | $socket->close(); |
||
110 | exit(1); |
||
|
|||
111 | }); |
||
112 | } |
||
113 | |||
114 | foreach ($socket->read() as $message) { |
||
115 | switch ($message[0]) { |
||
116 | case Connection::TYPE_ERROR: |
||
117 | $io->writeln('Connection closed with error: ' . $message[1]); |
||
118 | break 2; |
||
119 | default: |
||
120 | $data = \json_decode($message[1], null, 512, JSON_THROW_ON_ERROR); |
||
121 | if ($data[0] === Connection::MESSAGE_TYPE_VAR_DUMPER) { |
||
122 | $io->title('VarDumper'); |
||
123 | } elseif ($data[0] === Connection::MESSAGE_TYPE_LOGGER) { |
||
124 | $io->write('Logger'); |
||
125 | } |
||
126 | $io->writeln( |
||
127 | sprintf( |
||
128 | "\033[1;37m\033[47m%s\033[0m", |
||
129 | $data[1] |
||
130 | ) |
||
131 | ); |
||
132 | $io->newLine(); |
||
133 | } |
||
134 | } |
||
135 | return ExitCode::OK; |
||
136 | } |
||
138 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.