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 |
||
28 | abstract class RpcController implements ContainerAwareInterface |
||
29 | { |
||
30 | /** @var ContainerInterface */ |
||
31 | private $container; |
||
32 | /** @var EventDispatcherInterface */ |
||
33 | private $dispatcher; |
||
34 | |||
35 | /** |
||
36 | * Sets the container. |
||
37 | * |
||
38 | * @param ContainerInterface|null $container A ContainerInterface instance or null |
||
39 | */ |
||
40 | 7 | public function setContainer(ContainerInterface $container = null) |
|
45 | |||
46 | /** |
||
47 | * @param RequestInterface $request |
||
48 | * @param string $endpoint |
||
49 | * |
||
50 | * @return RpcResponseInterface |
||
51 | * @throws \Exception |
||
52 | */ |
||
53 | 7 | protected function getResponse(RequestInterface $request, $endpoint) |
|
65 | |||
66 | /** |
||
67 | * @param RequestInterface $request |
||
68 | * |
||
69 | * @return RpcResponseInterface |
||
70 | * |
||
71 | * @throws \RuntimeException |
||
72 | * @throws \InvalidArgumentException |
||
73 | * @throws \LogicException |
||
74 | * @throws MethodNotFoundException |
||
75 | */ |
||
76 | 7 | protected function handleSingleRequest(RequestInterface $request) |
|
118 | |||
119 | /** |
||
120 | * @return KernelInterface |
||
121 | */ |
||
122 | 7 | private function getKernel() |
|
126 | |||
127 | /** |
||
128 | * @param $name |
||
129 | * |
||
130 | * @return object|null |
||
131 | * @throws ServiceNotFoundException |
||
132 | */ |
||
133 | 7 | protected function get($name) |
|
138 | |||
139 | /** |
||
140 | * Filters a response object. |
||
141 | * |
||
142 | * @param RpcResponseInterface $response A Response instance |
||
143 | * @param RequestInterface $request An error message in case the response is not a Response object |
||
144 | * |
||
145 | * @return RpcResponseInterface The filtered Response instance |
||
146 | */ |
||
147 | 3 | protected function filterResponse(RpcResponseInterface $response, RequestInterface $request) |
|
155 | |||
156 | /** |
||
157 | * Publishes the finish request event, then pop the request from the stack. |
||
158 | * |
||
159 | * Note that the order of the operations is important here, otherwise |
||
160 | * operations such as {@link RequestStack::getParentRequest()} can lead to |
||
161 | * weird results. |
||
162 | * |
||
163 | * @param RequestInterface $request |
||
164 | */ |
||
165 | 7 | protected function finishRequest(RequestInterface $request) |
|
172 | |||
173 | /** |
||
174 | * @return ControllerResolverInterface |
||
175 | */ |
||
176 | abstract protected function getResolver(); |
||
177 | |||
178 | /** |
||
179 | * @param $var |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | View Code Duplication | protected function varToString($var) |
|
211 | |||
212 | /** |
||
213 | * Handles an exception by trying to convert it to a Response. |
||
214 | * |
||
215 | * @param \Exception $e An \Exception instance |
||
216 | * @param RequestInterface $request A Request instance |
||
217 | * |
||
218 | * @return RpcResponseInterface A Response instance |
||
219 | * |
||
220 | * @throws \Exception |
||
221 | */ |
||
222 | 4 | protected function handleException(\Exception $e, RequestInterface $request) |
|
240 | } |
||
241 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: