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 Async 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 Async, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Async implements LoggerAwareInterface |
||
17 | { |
||
18 | const PROMISE_REACT = 'React\Promise\PromiseInterface'; |
||
19 | const PROMISE_GUZZLE = 'GuzzleHttp\Promise\PromiseInterface'; |
||
20 | const PROMISE_HTTP = 'Http\Promise\Promise'; |
||
21 | const PROMISE_AMP = 'Amp\Promise'; |
||
22 | /** |
||
23 | * @var LoopInterface |
||
24 | */ |
||
25 | protected $loop; |
||
26 | /** |
||
27 | * @var LoggerInterface |
||
28 | */ |
||
29 | protected $logger; |
||
30 | |||
31 | /** |
||
32 | * @var callable that maps to _execute or _executeReactLoop ... |
||
33 | */ |
||
34 | protected $exec; |
||
35 | |||
36 | public function __construct() |
||
41 | |||
42 | public function promise(Generator $flow): Promise |
||
54 | |||
55 | public function execute(Generator $flow, callable $callback = null) |
||
69 | |||
70 | private function _execute(Generator $flow, callable $callback = null, int $depth = 0) |
||
74 | |||
75 | private function _executeReactLoop(Generator $flow, callable $callback = null, int $depth = 0) |
||
81 | |||
82 | private function _executeAmpLoop(Generator $flow, callable $callback = null, int $depth = 0) |
||
89 | |||
90 | |||
91 | private function _run(Generator $flow, callable $callback = null, int $depth = 0) |
||
188 | |||
189 | /** |
||
190 | * Handle known promise interfaces |
||
191 | * |
||
192 | * @param Generator $flow |
||
193 | * @param callable $callback |
||
194 | * @param int $depth |
||
195 | * @param \React\Promise\PromiseInterface|\GuzzleHttp\Promise\PromiseInterface $value |
||
196 | */ |
||
197 | private function handlePromise(Generator $flow, callable $callback, int $depth, $value) |
||
217 | |||
218 | private function logCallable(callable $callable, array $arguments, int $depth = 0) |
||
243 | |||
244 | private function logGenerator(Generator $generator, int $depth = 0) |
||
267 | |||
268 | private function format($parameters) |
||
272 | |||
273 | /** |
||
274 | * A method used to test whether this class is autoloaded. |
||
275 | * |
||
276 | * @return bool |
||
277 | * |
||
278 | * @see \LogicalSteps\Async\Test\DummyTest |
||
279 | */ |
||
280 | public function autoloaded() |
||
284 | |||
285 | /** |
||
286 | * Sets a logger instance on the object. |
||
287 | * |
||
288 | * @param LoggerInterface $logger |
||
289 | * |
||
290 | * @return void |
||
291 | */ |
||
292 | public function setLogger(LoggerInterface $logger) |
||
296 | |||
297 | /** |
||
298 | * @param LoopInterface $loop |
||
299 | */ |
||
300 | public function setLoop(LoopInterface $loop) |
||
305 | |||
306 | public function useAmpLoop() |
||
310 | } |
||
311 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.