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 |
||
90 | protected function start() |
||
91 | { |
||
92 | if ($this->getPid()) { |
||
93 | echo 'already running' . PHP_EOL; |
||
94 | exit(1); |
||
95 | } |
||
96 | |||
97 | $mode = config('laravoole.base_config.mode'); |
||
98 | if (!$mode) { |
||
99 | echo "Laravoole needs Swoole or Workerman." . PHP_EOL . |
||
100 | "You can install Swoole by command:" . PHP_EOL . |
||
101 | " pecl install swoole" . PHP_EOL . |
||
102 | "Or you can install Workerman by command:" . PHP_EOL . |
||
103 | " composer require workerman/workerman" . PHP_EOL; |
||
104 | exit; |
||
105 | } |
||
106 | |||
107 | if(!class_exists($wrapper = "Laravoole\\Wrapper\\{$mode}Wrapper")) { |
||
108 | $wrapper = $mode; |
||
109 | } |
||
110 | $ref = new ReflectionClass($wrapper); |
||
111 | $wrapper_file = $ref->getFileName(); |
||
112 | |||
113 | $handler_config = []; |
||
114 | $params = $wrapper::getParams(); |
||
115 | foreach ($params as $paramName => $default) { |
||
116 | if (is_int($paramName)) { |
||
117 | $paramName = $default; |
||
118 | $default = null; |
||
119 | } |
||
120 | $key = $paramName; |
||
121 | $value = config("laravoole.handler_config.{$key}", function () use ($key, $default) { |
||
122 | return env("LARAVOOLE_" . strtoupper($key), $default); |
||
123 | }); |
||
124 | if ($value !== null) { |
||
125 | if ((is_array($value) || is_object($value)) && is_callable($value)) { |
||
126 | $value = $value(); |
||
127 | } |
||
128 | $handler_config[$paramName] = $value; |
||
129 | } |
||
130 | |||
131 | } |
||
132 | |||
133 | $host = config('laravoole.base_config.host'); |
||
134 | $port = config('laravoole.base_config.port'); |
||
135 | $socket = @stream_socket_server("tcp://{$host}:{$port}"); |
||
136 | if(!$socket) { |
||
137 | throw new \Exception("Address {$host}:{$port} already in use", 1); |
||
138 | } else { |
||
139 | fclose($socket); |
||
140 | } |
||
141 | |||
142 | $configs = [ |
||
143 | 'host' => $host, |
||
144 | 'port' => $port, |
||
145 | 'wrapper_file' => $wrapper_file, |
||
146 | 'wrapper' => $wrapper, |
||
147 | 'pid_file' => config('laravoole.base_config.pid_file'), |
||
148 | 'root_dir' => base_path(), |
||
149 | 'callbacks' => config('laravoole.base_config.callbacks'), |
||
150 | // for swoole / workerman |
||
151 | 'handler_config' => $handler_config, |
||
152 | // for wrapper, like http / fastcgi / websocket |
||
153 | 'wrapper_config' => config('laravoole.wrapper_config'), |
||
154 | 'base_config' => config('laravoole.base_config'), |
||
155 | ]; |
||
156 | |||
157 | $handle = popen(PHP_BINARY . ' ' . __DIR__ . '/../../src/Entry.php', 'w'); |
||
158 | fwrite($handle, serialize($configs)); |
||
159 | fclose($handle); |
||
160 | } |
||
161 | |||
178 |
Adding a
@return
annotation 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.