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 |
||
14 | class AppInstance |
||
15 | { |
||
16 | use \PHPDaemon\Traits\ClassWatchdog; |
||
17 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
18 | |||
19 | /** |
||
20 | * Event: config updated |
||
21 | */ |
||
22 | const EVENT_CONFIG_UPDATED = 1; |
||
23 | |||
24 | /** |
||
25 | * Event: graceful shutdown |
||
26 | */ |
||
27 | const EVENT_GRACEFUL_SHUTDOWN = 2; |
||
28 | |||
29 | /** |
||
30 | * Event: shutdown |
||
31 | */ |
||
32 | const EVENT_SHUTDOWN = 3; |
||
33 | |||
34 | /** |
||
35 | * @var boolean If true, it's allowed to be run without defined config section' |
||
36 | */ |
||
37 | public static $runOnDemand = true; |
||
38 | |||
39 | /** |
||
40 | * @var string Optional passphrase |
||
41 | */ |
||
42 | public $passphrase; |
||
43 | |||
44 | /** |
||
45 | * @var boolean Ready to run? |
||
46 | */ |
||
47 | public $ready = false; |
||
48 | |||
49 | /** |
||
50 | * @var object Related config section |
||
51 | */ |
||
52 | public $config; |
||
53 | |||
54 | /** |
||
55 | * @var boolean Is RPC enabled? |
||
56 | */ |
||
57 | public $enableRPC = false; |
||
58 | |||
59 | /** |
||
60 | * @var null|string Default class of incoming requests |
||
61 | */ |
||
62 | public $requestClass; |
||
63 | |||
64 | /** |
||
65 | * @var string Instance name |
||
66 | */ |
||
67 | protected $name; |
||
68 | |||
69 | /** |
||
70 | * Application constructor |
||
71 | * @param string $name Instance name |
||
72 | * @return void |
||
|
|||
73 | */ |
||
74 | public function __construct($name = '') |
||
125 | |||
126 | /** |
||
127 | * Returns whether if this application is enabled |
||
128 | * @return boolean |
||
129 | */ |
||
130 | public function isEnabled() |
||
134 | |||
135 | /** |
||
136 | * Function to get default config options from application |
||
137 | * Override to set your own |
||
138 | * @return boolean |
||
139 | */ |
||
140 | protected function getConfigDefaults() |
||
144 | |||
145 | /** |
||
146 | * Called when creates instance of the application |
||
147 | * @return void |
||
148 | */ |
||
149 | protected function init() |
||
152 | |||
153 | /** |
||
154 | * Called when the worker is ready to go |
||
155 | * @return void |
||
156 | */ |
||
157 | public function onReady() |
||
160 | |||
161 | /** |
||
162 | * @param string $name Instance name |
||
163 | * @param boolean $spawn If true, we spawn an instance if absent |
||
164 | * @return AppInstance |
||
165 | */ |
||
166 | public static function getInstance($name, $spawn = true) |
||
170 | |||
171 | /** |
||
172 | * Function handles incoming Remote Procedure Calls |
||
173 | * You can override it |
||
174 | * @param string $method Method name |
||
175 | * @param array $args Arguments |
||
176 | * @return mixed Result |
||
177 | */ |
||
178 | public function RPCall($method, $args) |
||
186 | |||
187 | /** |
||
188 | * Returns a config section |
||
189 | * @return Config\Section |
||
190 | */ |
||
191 | public function getConfig() |
||
195 | |||
196 | /** |
||
197 | * Returns the instance name |
||
198 | * @return string |
||
199 | */ |
||
200 | public function getName() |
||
204 | |||
205 | /** |
||
206 | * Send broadcast RPC |
||
207 | * You can override it |
||
208 | * @param string $method Method name |
||
209 | * @param array $args Arguments |
||
210 | * @param callable $cb Callback |
||
211 | * @return boolean Success |
||
212 | */ |
||
213 | View Code Duplication | public function broadcastCall($method, $args = [], $cb = null) |
|
222 | |||
223 | /** |
||
224 | * Send RPC, executed once in any worker |
||
225 | * You can override it |
||
226 | * @param string $method Method name |
||
227 | * @param array $args Arguments |
||
228 | * @param mixed $cb Callback |
||
229 | * @return boolean Success |
||
230 | */ |
||
231 | View Code Duplication | public function singleCall($method, $args = [], $cb = null) |
|
240 | |||
241 | /** |
||
242 | * Send RPC, executed once in certain worker |
||
243 | * You can override it |
||
244 | * @param integer $workerId Worker Id |
||
245 | * @param string $method Method name |
||
246 | * @param array $args Arguments |
||
247 | * @param mixed $cb Callback |
||
248 | * @return boolean Success |
||
249 | */ |
||
250 | View Code Duplication | public function directCall($workerId, $method, $args = [], $cb = null) |
|
260 | |||
261 | /** |
||
262 | * Log something |
||
263 | * @param string $message Message |
||
264 | * @return void |
||
265 | */ |
||
266 | public function log($message) |
||
270 | |||
271 | /** |
||
272 | * Handle the request |
||
273 | * @param object $parent Parent request |
||
274 | * @param object $upstream Upstream application |
||
275 | * @return object Request |
||
276 | */ |
||
277 | public function handleRequest($parent, $upstream) |
||
282 | |||
283 | /** |
||
284 | * Create Request instance |
||
285 | * @param object $req Generic |
||
286 | * @param object $upstream Upstream application instance |
||
287 | * @return object Request |
||
288 | */ |
||
289 | public function beginRequest($req, $upstream) |
||
297 | |||
298 | /** |
||
299 | * Handle the worker status |
||
300 | * @param integer $ret Status code |
||
301 | * @return boolean Result |
||
302 | */ |
||
303 | public function handleStatus($ret) |
||
315 | |||
316 | /** |
||
317 | * Called when worker is going to update configuration |
||
318 | * @todo call it only when application section config is changed |
||
319 | * @return void |
||
320 | */ |
||
321 | public function onConfigUpdated() |
||
324 | |||
325 | /** |
||
326 | * Called when application instance is going to shutdown |
||
327 | * @return boolean Ready to shutdown? |
||
328 | */ |
||
329 | protected function onShutdown($graceful = false) |
||
333 | } |
||
334 |
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.