Complex classes like Server often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Server, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
30 | class Server implements HandlerFactory |
||
31 | { |
||
32 | private const SWOOLE_EXT_NAME = 'swoole'; |
||
33 | |||
34 | /** |
||
35 | * @var SwooleServer|null |
||
36 | */ |
||
37 | protected $handler; |
||
38 | |||
39 | /** |
||
40 | * @var Configuration |
||
41 | */ |
||
42 | protected $configuration; |
||
43 | |||
44 | /** |
||
45 | * @var SplQueue[] |
||
46 | */ |
||
47 | protected $listeners = []; |
||
48 | |||
49 | /** |
||
50 | * @var LoggerInterface |
||
51 | */ |
||
52 | protected $logger; |
||
53 | |||
54 | /** |
||
55 | * @var HandlerFactory |
||
56 | */ |
||
57 | protected $handlerFactory; |
||
58 | |||
59 | /** |
||
60 | * @var Client[] |
||
61 | */ |
||
62 | private $clients = []; |
||
63 | |||
64 | /** |
||
65 | * @var bool |
||
66 | */ |
||
67 | private $running = false; |
||
68 | |||
69 | 15 | public function __construct( |
|
82 | |||
83 | /** |
||
84 | * Return server configuration. |
||
85 | * |
||
86 | * @return Configuration |
||
87 | */ |
||
88 | 2 | public function getConfiguration(): Configuration |
|
92 | |||
93 | /** |
||
94 | * @param int $id |
||
95 | * @return Client |
||
96 | * @throws ClientException if client was not found |
||
97 | */ |
||
98 | 6 | public function getClient(int $id): Client |
|
105 | |||
106 | /** |
||
107 | * Adds listener that is attached to server once it is run. |
||
108 | * |
||
109 | * @param Listener $listener |
||
110 | */ |
||
111 | 11 | public function addListener(Listener $listener): void |
|
119 | |||
120 | 11 | protected function addListenerByType(Listener $listener, string $type): void |
|
129 | |||
130 | /** |
||
131 | * Checks if listener exists. |
||
132 | * |
||
133 | * @param Listener $listener |
||
134 | * @return bool |
||
135 | */ |
||
136 | 1 | public function hasListener(Listener $listener): bool |
|
149 | |||
150 | /** |
||
151 | * Returns information about server. |
||
152 | * |
||
153 | * @return ServerStats |
||
154 | */ |
||
155 | 1 | public function getServerStats(): ServerStats |
|
162 | |||
163 | public function createHandler(Configuration $configuration) |
||
175 | |||
176 | 10 | public function start(): void |
|
184 | |||
185 | 1 | public function stop(): void |
|
193 | |||
194 | 1 | public function isRunning(): bool |
|
198 | |||
199 | 10 | protected function createListeners(): void |
|
207 | |||
208 | 6 | private function createClient($handler, int $clientId): Client |
|
212 | |||
213 | 4 | private function destroyClient(int $clientId): void |
|
217 | |||
218 | 10 | protected function createOnConnectListener(): void |
|
234 | |||
235 | 10 | protected function createOnCloseListener(): void |
|
250 | |||
251 | 10 | protected function createOnShutdownListener(): void |
|
267 | |||
268 | 10 | protected function createOnStartListener(): void |
|
282 | |||
283 | 10 | protected function createOnReceiveListener(): void |
|
298 | } |
||
299 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: