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 | final 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 | * @param PluginJournal $journal |
||
33 | */ |
||
34 | 2 | public function __construct(Formatter $formatter, PluginJournal $journal) |
|
39 | |||
40 | /** |
||
41 | * @param RequestInterface $request |
||
42 | * @param string $clientName |
||
43 | * @param int $depth |
||
44 | */ |
||
45 | public function addRequest(RequestInterface $request, $clientName, $depth) |
||
49 | |||
50 | /** |
||
51 | * @param ResponseInterface $response |
||
52 | * @param string $clientName |
||
53 | * @param int $depth |
||
54 | */ |
||
55 | public function addResponse(ResponseInterface $response, $clientName, $depth) |
||
60 | |||
61 | /** |
||
62 | * @param Exception $exception |
||
63 | * @param string $clientName |
||
64 | * @param int $depth |
||
65 | */ |
||
66 | public function addFailure(Exception $exception, $clientName, $depth) |
||
79 | |||
80 | /** |
||
81 | * Returns the successful request-response pairs. |
||
82 | * |
||
83 | * @return int |
||
84 | */ |
||
85 | View Code Duplication | public function getSuccessfulRequests() |
|
|
|||
86 | { |
||
87 | $count = 0; |
||
88 | foreach ($this->data as $client) { |
||
89 | if (isset($client['failure'])) { |
||
90 | foreach ($client['failure'][0] as $failure) { |
||
91 | if (!$failure) { |
||
92 | ++$count; |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $count; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Returns the failed request-response pairs. |
||
103 | * |
||
104 | * @return int |
||
105 | */ |
||
106 | View Code Duplication | public function getFailedRequests() |
|
121 | |||
122 | /** |
||
123 | * Returns the total number of request made. |
||
124 | * |
||
125 | * @return int |
||
126 | */ |
||
127 | public function getTotalRequests() |
||
131 | |||
132 | /** |
||
133 | * Return a RequestStackProvider for each client. |
||
134 | * |
||
135 | * @return RequestStackProvider[] |
||
136 | */ |
||
137 | public function getClients() |
||
141 | |||
142 | /** |
||
143 | * @return PluginJournal |
||
144 | */ |
||
145 | 1 | public function getJournal() |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | public function collect(Request $request, Response $response, \Exception $exception = null) |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 1 | public function getName() |
|
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | public function serialize() |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function unserialize($data) |
||
181 | } |
||
182 |
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.