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 |
||
31 | class CustomProvider extends AbstractProvider |
||
32 | { |
||
33 | /** |
||
34 | * @type ProviderInterface |
||
35 | */ |
||
36 | private $client; |
||
37 | |||
38 | /** |
||
39 | * @param string $name |
||
40 | * @param array $options |
||
41 | * @param mixed $client |
||
42 | * @param Cache $cache |
||
43 | * @param Logger $logger |
||
44 | */ |
||
45 | 6 | View Code Duplication | public function __construct($name, array $options, $client, Cache $cache, Logger $logger) |
54 | |||
55 | /** |
||
56 | * @param ProviderInterface $client |
||
57 | * |
||
58 | * @return CustomProvider |
||
59 | */ |
||
60 | 6 | public function setClient(ProviderInterface $client) |
|
66 | |||
67 | 1 | public function getProvider() |
|
71 | |||
72 | /** |
||
73 | * Builds the configured queues |
||
74 | * |
||
75 | * If a Queue name is passed and configured, this method will build only that |
||
76 | * Queue. |
||
77 | * |
||
78 | * All Create methods are idempotent, if the resource exists, the current ARN |
||
79 | * will be returned |
||
80 | * |
||
81 | */ |
||
82 | 1 | public function create() |
|
86 | |||
87 | /** |
||
88 | * @return Boolean |
||
89 | */ |
||
90 | 1 | public function destroy() |
|
94 | |||
95 | /** |
||
96 | * {@inheritDoc} |
||
97 | * |
||
98 | * This method will either use a SNS Topic to publish a queued message or |
||
99 | * straight to SQS depending on the application configuration. |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 1 | public function publish(array $message, array $options = []) |
|
107 | |||
108 | /** |
||
109 | * {@inheritDoc} |
||
110 | */ |
||
111 | 1 | public function receive(array $options = []) |
|
115 | |||
116 | /** |
||
117 | * {@inheritDoc} |
||
118 | * |
||
119 | * @return bool |
||
120 | */ |
||
121 | 1 | public function delete($id) |
|
125 | } |
||
126 |
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.