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:
1 | <?php |
||
32 | class SyncProvider extends AbstractProvider |
||
33 | { |
||
34 | /** |
||
35 | * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
||
36 | */ |
||
37 | protected $dispatcher; |
||
38 | |||
39 | 6 | View Code Duplication | public function __construct( |
|
|||
40 | $name, |
||
41 | array $options, |
||
42 | $client, |
||
43 | Cache $cache, |
||
44 | Logger $logger |
||
45 | ) { |
||
46 | 6 | $this->name = $name; |
|
47 | 6 | $this->options = $options; |
|
48 | 6 | $this->dispatcher = $client; |
|
49 | 6 | $this->cache = $cache; |
|
50 | 6 | $this->logger = $logger; |
|
51 | 6 | } |
|
52 | |||
53 | 1 | public function getProvider() |
|
57 | |||
58 | 1 | public function publish(array $message, array $options = []) |
|
59 | { |
||
60 | 1 | $message = new Message(time(), $message, []); |
|
61 | |||
62 | 1 | $this->dispatcher->dispatch( |
|
63 | 1 | Events::Message($this->name), |
|
64 | 1 | new MessageEvent($this->name, $message) |
|
65 | ); |
||
66 | |||
67 | 1 | $context = ['MessageId' => $message->getId()]; |
|
68 | 1 | $this->log(200, 'Message received and dispatched on Sync Queue', $context); |
|
69 | 1 | } |
|
70 | |||
71 | public function create() {} |
||
72 | |||
73 | public function destroy() {} |
||
74 | |||
75 | public function delete($id) {} |
||
76 | |||
77 | public function receive(array $options = []) {} |
||
78 | } |
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.