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 |
||
24 | abstract class RpcController implements ContainerAwareInterface |
||
25 | { |
||
26 | /** @var ContainerInterface */ |
||
27 | private $container; |
||
28 | |||
29 | /** |
||
30 | * Sets the container. |
||
31 | * |
||
32 | * @param ContainerInterface|null $container A ContainerInterface instance or null |
||
33 | * |
||
34 | * @throws ServiceNotFoundException |
||
35 | * @throws ServiceCircularReferenceException |
||
36 | */ |
||
37 | 9 | public function setContainer(ContainerInterface $container = null) |
|
41 | |||
42 | /** |
||
43 | * @param RpcRequestInterface $request |
||
44 | * @param string $endpoint |
||
45 | * |
||
46 | * @return RpcResponseInterface |
||
47 | * @throws \Exception |
||
48 | */ |
||
49 | 9 | protected function getResponse(RpcRequestInterface $request, $endpoint) |
|
61 | |||
62 | /** |
||
63 | * @param RpcRequestInterface $request |
||
64 | * |
||
65 | * @return RpcResponseInterface |
||
66 | * |
||
67 | * @throws \RuntimeException |
||
68 | * @throws \InvalidArgumentException |
||
69 | * @throws \LogicException |
||
70 | * @throws MethodNotFoundException |
||
71 | */ |
||
72 | 9 | protected function handleSingleRequest(RpcRequestInterface $request) |
|
114 | |||
115 | /** |
||
116 | * @param $name |
||
117 | * |
||
118 | * @return object|null |
||
119 | * @throws ServiceNotFoundException |
||
120 | * @throws ServiceCircularReferenceException |
||
121 | */ |
||
122 | 9 | protected function get($name) |
|
126 | |||
127 | /** |
||
128 | * Filters a response object. |
||
129 | * |
||
130 | * @param RpcResponseInterface $response A Response instance |
||
131 | * @param RpcRequestInterface $request An error message in case the response is not a Response object |
||
132 | * |
||
133 | * @return RpcResponseInterface The filtered Response instance |
||
134 | * @throws \RuntimeException |
||
135 | */ |
||
136 | 4 | protected function filterResponse(RpcResponseInterface $response, RpcRequestInterface $request) |
|
144 | |||
145 | /** |
||
146 | * Publishes the finish request event, then pop the request from the stack. |
||
147 | * |
||
148 | * Note that the order of the operations is important here, otherwise |
||
149 | * operations such as {@link RequestStack::getParentRequest()} can lead to |
||
150 | * weird results. |
||
151 | * |
||
152 | * @param RpcRequestInterface $request |
||
153 | * |
||
154 | * @throws \RuntimeException |
||
155 | */ |
||
156 | 9 | protected function finishRequest(RpcRequestInterface $request) |
|
163 | |||
164 | /** |
||
165 | * @return ControllerResolverInterface |
||
166 | */ |
||
167 | abstract protected function getResolver(); |
||
168 | |||
169 | /** |
||
170 | * @param $var |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | View Code Duplication | protected function varToString($var) |
|
202 | |||
203 | /** |
||
204 | * Handles an exception by trying to convert it to a Response. |
||
205 | * |
||
206 | * @param \Exception $e An \Exception instance |
||
207 | * @param RpcRequestInterface $request A Request instance |
||
208 | * |
||
209 | * @return RpcResponseInterface A Response instance |
||
210 | * |
||
211 | * @throws \Exception |
||
212 | */ |
||
213 | 5 | protected function handleException(\Exception $e, RpcRequestInterface $request) |
|
231 | |||
232 | /** |
||
233 | * @return EventDispatcherInterface |
||
234 | */ |
||
235 | 9 | protected function getDispatcher() |
|
239 | |||
240 | /** |
||
241 | * @return KernelInterface |
||
242 | * @throws \RuntimeException |
||
243 | */ |
||
244 | 9 | private function getKernel() |
|
258 | } |
||
259 |
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.