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 |
||
18 | class DebugPluginCollector extends DataCollector |
||
19 | { |
||
20 | /** |
||
21 | * @var Formatter |
||
22 | */ |
||
23 | private $formatter; |
||
24 | |||
25 | /** |
||
26 | * @var PluginJournal |
||
27 | */ |
||
28 | private $journal; |
||
29 | |||
30 | /** |
||
31 | * @param Formatter $formatter |
||
32 | */ |
||
33 | 1 | public function __construct(Formatter $formatter, PluginJournal $journal) |
|
38 | |||
39 | /** |
||
40 | * @param RequestInterface $request |
||
41 | */ |
||
42 | public function addRequest(RequestInterface $request, $clientName, $depth) |
||
46 | |||
47 | /** |
||
48 | * @param ResponseInterface $response |
||
49 | */ |
||
50 | public function addResponse(ResponseInterface $response, $clientName, $depth) |
||
55 | |||
56 | /** |
||
57 | * @param Exception $exception |
||
58 | */ |
||
59 | public function addFailure(Exception $exception, $clientName, $depth) |
||
72 | |||
73 | /** |
||
74 | * Returns the successful request-resonse pairs. |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | View Code Duplication | public function getSucessfulRequests() |
|
93 | |||
94 | /** |
||
95 | * Returns the failed request-resonse pairs. |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | View Code Duplication | public function getFailedRequests() |
|
114 | |||
115 | /** |
||
116 | * Returns the total number of request made. |
||
117 | * |
||
118 | * @return int |
||
119 | */ |
||
120 | public function getTotalRequests() |
||
124 | |||
125 | /** |
||
126 | * Return a RequestStackProvider for each client. |
||
127 | * |
||
128 | * @return RequestStackProvider[] |
||
129 | */ |
||
130 | public function getClients() |
||
134 | |||
135 | /** |
||
136 | * @return PluginJournal |
||
137 | */ |
||
138 | public function getJournal() |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function collect(Request $request, Response $response, \Exception $exception = null) |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function getName() |
||
158 | |||
159 | public function serialize() |
||
163 | |||
164 | public function unserialize($data) |
||
168 | } |
||
169 |
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.