Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AppInstance 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 AppInstance, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class AppInstance |
||
13 | { |
||
14 | use \PHPDaemon\Traits\ClassWatchdog; |
||
15 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
16 | |||
17 | /** |
||
18 | * Event: config updated |
||
19 | */ |
||
20 | const EVENT_CONFIG_UPDATED = 1; |
||
21 | |||
22 | /** |
||
23 | * Event: graceful shutdown |
||
24 | */ |
||
25 | const EVENT_GRACEFUL_SHUTDOWN = 2; |
||
26 | |||
27 | /** |
||
28 | * Event: shutdown |
||
29 | */ |
||
30 | const EVENT_SHUTDOWN = 3; |
||
31 | |||
32 | /** |
||
33 | * @var boolean If true, it's allowed to be run without defined config section' |
||
34 | */ |
||
35 | public static $runOnDemand = true; |
||
36 | |||
37 | /** |
||
38 | * @var string Optional passphrase |
||
39 | */ |
||
40 | public $passphrase; |
||
41 | |||
42 | /** |
||
43 | * @var boolean Ready to run? |
||
44 | */ |
||
45 | public $ready = false; |
||
46 | |||
47 | /** |
||
48 | * @var object Related config section |
||
49 | */ |
||
50 | public $config; |
||
51 | |||
52 | /** |
||
53 | * @var boolean Is RPC enabled? |
||
54 | */ |
||
55 | public $enableRPC = false; |
||
56 | |||
57 | /** |
||
58 | * @var null|string Default class of incoming requests |
||
59 | */ |
||
60 | public $requestClass; |
||
61 | |||
62 | /** |
||
63 | * @var string Instance name |
||
64 | */ |
||
65 | protected $name; |
||
66 | |||
67 | /** |
||
68 | * Application constructor |
||
69 | * @param string $name Instance name |
||
70 | * @return void |
||
|
|||
71 | */ |
||
72 | public function __construct($name = '') |
||
120 | |||
121 | /** |
||
122 | * Returns whether if this application is enabled |
||
123 | * @return boolean |
||
124 | */ |
||
125 | public function isEnabled() |
||
129 | |||
130 | /** |
||
131 | * Function to get default config options from application |
||
132 | * Override to set your own |
||
133 | * @return boolean |
||
134 | */ |
||
135 | protected function getConfigDefaults() |
||
139 | |||
140 | /** |
||
141 | * Called when creates instance of the application |
||
142 | * @return void |
||
143 | */ |
||
144 | protected function init() |
||
147 | |||
148 | /** |
||
149 | * Called when the worker is ready to go |
||
150 | * @return void |
||
151 | */ |
||
152 | public function onReady() |
||
155 | |||
156 | /** |
||
157 | * @param string $name Instance name |
||
158 | * @param boolean $spawn If true, we spawn an instance if absent |
||
159 | * @return AppInstance |
||
160 | */ |
||
161 | public static function getInstance($name, $spawn = true) |
||
165 | |||
166 | /** |
||
167 | * Function handles incoming Remote Procedure Calls |
||
168 | * You can override it |
||
169 | * @param string $method Method name |
||
170 | * @param array $args Arguments |
||
171 | * @return mixed Result |
||
172 | */ |
||
173 | public function RPCall($method, $args) |
||
181 | |||
182 | /** |
||
183 | * Returns a config section |
||
184 | * @return Config\Section |
||
185 | */ |
||
186 | public function getConfig() |
||
190 | |||
191 | /** |
||
192 | * Returns the instance name |
||
193 | * @return string |
||
194 | */ |
||
195 | public function getName() |
||
199 | |||
200 | /** |
||
201 | * Send broadcast RPC |
||
202 | * You can override it |
||
203 | * @param string $method Method name |
||
204 | * @param array $args Arguments |
||
205 | * @param callable $cb Callback |
||
206 | * @return boolean Success |
||
207 | */ |
||
208 | View Code Duplication | public function broadcastCall($method, $args = [], $cb = null) |
|
217 | |||
218 | /** |
||
219 | * Send RPC, executed once in any worker |
||
220 | * You can override it |
||
221 | * @param string $method Method name |
||
222 | * @param array $args Arguments |
||
223 | * @param mixed $cb Callback |
||
224 | * @return boolean Success |
||
225 | */ |
||
226 | View Code Duplication | public function singleCall($method, $args = [], $cb = null) |
|
235 | |||
236 | /** |
||
237 | * Send RPC, executed once in certain worker |
||
238 | * You can override it |
||
239 | * @param integer $workerId Worker Id |
||
240 | * @param string $method Method name |
||
241 | * @param array $args Arguments |
||
242 | * @param mixed $cb Callback |
||
243 | * @return boolean Success |
||
244 | */ |
||
245 | View Code Duplication | public function directCall($workerId, $method, $args = [], $cb = null) |
|
255 | |||
256 | /** |
||
257 | * Log something |
||
258 | * @param string $message Message |
||
259 | * @return void |
||
260 | */ |
||
261 | public function log($message) |
||
265 | |||
266 | /** |
||
267 | * Handle the request |
||
268 | * @param object $parent Parent request |
||
269 | * @param object $upstream Upstream application |
||
270 | * @return object Request |
||
271 | */ |
||
272 | public function handleRequest($parent, $upstream) |
||
277 | |||
278 | /** |
||
279 | * Create Request instance |
||
280 | * @param object $req Generic |
||
281 | * @param object $upstream Upstream application instance |
||
282 | * @return object Request |
||
283 | */ |
||
284 | public function beginRequest($req, $upstream) |
||
292 | |||
293 | /** |
||
294 | * Handle the worker status |
||
295 | * @param integer $ret Status code |
||
296 | * @return boolean Result |
||
297 | */ |
||
298 | public function handleStatus($ret) |
||
310 | |||
311 | /** |
||
312 | * Called when worker is going to update configuration |
||
313 | * @todo call it only when application section config is changed |
||
314 | * @return void |
||
315 | */ |
||
316 | public function onConfigUpdated() |
||
319 | |||
320 | /** |
||
321 | * Called when application instance is going to shutdown |
||
322 | * @return boolean Ready to shutdown? |
||
323 | */ |
||
324 | protected function onShutdown($graceful = false) |
||
328 | } |
||
329 |
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.