| Conditions | 11 | 
| Paths | 30 | 
| Total Lines | 71 | 
| Code Lines | 50 | 
| 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 | ||
| 100 | protected function start() | ||
| 101 |     { | ||
| 102 |         if ($this->getPid()) { | ||
| 103 | echo 'already running' . PHP_EOL; | ||
| 104 | exit(1); | ||
| 105 | } | ||
| 106 | |||
| 107 |         $mode = config('laravoole.base_config.mode'); | ||
| 108 |         if (!$mode) { | ||
| 109 | echo "Laravoole needs Swoole or Workerman." . PHP_EOL . | ||
| 110 | "You can install Swoole by command:" . PHP_EOL . | ||
| 111 | " pecl install swoole" . PHP_EOL . | ||
| 112 | "Or you can install Workerman by command:" . PHP_EOL . | ||
| 113 | " composer require workerman/workerman" . PHP_EOL; | ||
| 114 | exit; | ||
| 115 | } | ||
| 116 | |||
| 117 |         if(!class_exists($wrapper = "Laravoole\\Wrapper\\{$mode}Wrapper")) { | ||
| 118 | $wrapper = $mode; | ||
| 119 | } | ||
| 120 | $ref = new ReflectionClass($wrapper); | ||
| 121 | $wrapper_file = $ref->getFileName(); | ||
| 122 | |||
| 123 | $handler_config = []; | ||
| 124 | $params = $wrapper::getParams(); | ||
| 125 |         foreach ($params as $paramName => $default) { | ||
| 126 |             if (is_int($paramName)) { | ||
| 127 | $paramName = $default; | ||
| 128 | $default = null; | ||
| 129 | } | ||
| 130 | $key = $paramName; | ||
| 131 |             $value = config("laravoole.handler_config.{$key}", function () use ($key, $default) { | ||
| 132 |                 return env("LARAVOOLE_" . strtoupper($key), $default); | ||
| 133 | }); | ||
| 134 |             if ($value !== null) { | ||
| 135 |                 if ((is_array($value) || is_object($value)) && is_callable($value)) { | ||
| 136 | $value = $value(); | ||
| 137 | } | ||
| 138 | $handler_config[$paramName] = $value; | ||
| 139 | } | ||
| 140 | |||
| 141 | } | ||
| 142 | |||
| 143 |         $host = config('laravoole.base_config.host'); | ||
| 144 |         $port = config('laravoole.base_config.port'); | ||
| 145 |         $socket = @stream_socket_server("tcp://{$host}:{$port}"); | ||
| 146 |         if(!$socket) { | ||
| 147 |             throw new \Exception("Address {$host}:{$port} already in use", 1); | ||
| 148 |         } else { | ||
| 149 | fclose($socket); | ||
| 150 | } | ||
| 151 | |||
| 152 | $configs = [ | ||
| 153 | 'host' => $host, | ||
| 154 | 'port' => $port, | ||
| 155 | 'wrapper_file' => $wrapper_file, | ||
| 156 | 'wrapper' => $wrapper, | ||
| 157 |             'pid_file' => config('laravoole.base_config.pid_file'), | ||
| 158 | 'root_dir' => base_path(), | ||
| 159 |             'callbacks' => config('laravoole.base_config.callbacks'), | ||
| 160 | // for swoole / workerman | ||
| 161 | 'handler_config' => $handler_config, | ||
| 162 | // for wrapper, like http / fastcgi / websocket | ||
| 163 |             'wrapper_config' => config('laravoole.wrapper_config'), | ||
| 164 |             'base_config' => config('laravoole.base_config'), | ||
| 165 | ]; | ||
| 166 | |||
| 167 | $handle = popen(PHP_BINARY . ' ' . __DIR__ . '/../../src/Entry.php', 'w'); | ||
| 168 | fwrite($handle, serialize($configs)); | ||
| 169 | fclose($handle); | ||
| 170 | } | ||
| 171 | |||
| 188 | 
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.