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) |
|
35 | { |
||
36 | 2 | $this->formatter = $formatter; |
|
37 | 2 | $this->journal = $journal; |
|
38 | 2 | } |
|
39 | |||
40 | /** |
||
41 | * @param RequestInterface $request |
||
42 | * @param string $clientName |
||
43 | * @param int $depth |
||
44 | */ |
||
45 | public function addRequest(RequestInterface $request, $clientName, $depth) |
||
46 | { |
||
47 | $this->data[$clientName]['request'][$depth][] = $this->formatter->formatRequest($request); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param ResponseInterface $response |
||
52 | * @param string $clientName |
||
53 | * @param int $depth |
||
54 | */ |
||
55 | public function addResponse(ResponseInterface $response, $clientName, $depth) |
||
56 | { |
||
57 | $this->data[$clientName]['response'][$depth][] = $this->formatter->formatResponse($response); |
||
58 | $this->data[$clientName]['failure'][$depth][] = false; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param Exception $exception |
||
63 | * @param string $clientName |
||
64 | * @param int $depth |
||
65 | */ |
||
66 | public function addFailure(Exception $exception, $clientName, $depth) |
||
67 | { |
||
68 | if ($exception instanceof Exception\HttpException) { |
||
69 | $formattedResponse = $this->formatter->formatResponse($exception->getResponse()); |
||
70 | } elseif ($exception instanceof Exception\TransferException) { |
||
71 | $formattedResponse = $exception->getMessage(); |
||
72 | } else { |
||
73 | $formattedResponse = sprintf('Unexpected exception of type "%s"', get_class($exception)); |
||
74 | } |
||
75 | |||
76 | $this->data[$clientName]['response'][$depth][] = $formattedResponse; |
||
77 | $this->data[$clientName]['failure'][$depth][] = true; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Returns the successful request-resonse pairs. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | View Code Duplication | public function getSucessfulRequests() |
|
|
|||
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-resonse pairs. |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | View Code Duplication | public function getFailedRequests() |
|
107 | { |
||
108 | $count = 0; |
||
109 | foreach ($this->data as $client) { |
||
110 | if (isset($client['failure'])) { |
||
111 | foreach ($client['failure'][0] as $failure) { |
||
112 | if ($failure) { |
||
113 | ++$count; |
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | return $count; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Returns the total number of request made. |
||
124 | * |
||
125 | * @return int |
||
126 | */ |
||
127 | public function getTotalRequests() |
||
128 | { |
||
129 | return $this->getSucessfulRequests() + $this->getFailedRequests(); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Return a RequestStackProvider for each client. |
||
134 | * |
||
135 | * @return RequestStackProvider[] |
||
136 | */ |
||
137 | public function getClients() |
||
138 | { |
||
139 | return RequestStackProvider::createFromCollectedData($this->data); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return PluginJournal |
||
144 | */ |
||
145 | 1 | public function getJournal() |
|
146 | { |
||
147 | 1 | return $this->journal; |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | public function collect(Request $request, Response $response, \Exception $exception = null) |
||
154 | { |
||
155 | // We do not need to collect any data from the Symfony Request and Response |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 1 | public function getName() |
|
162 | { |
||
163 | 1 | return 'httplug'; |
|
164 | } |
||
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.