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 | /** @var EventDispatcherInterface */ |
||
| 29 | private $dispatcher; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Sets the container. |
||
| 33 | * |
||
| 34 | * @param ContainerInterface|null $container A ContainerInterface instance or null |
||
| 35 | * |
||
| 36 | * @throws ServiceNotFoundException |
||
| 37 | * @throws ServiceCircularReferenceException |
||
| 38 | */ |
||
| 39 | 7 | public function setContainer(ContainerInterface $container = null) |
|
| 44 | |||
| 45 | /** |
||
| 46 | * @param RequestInterface $request |
||
| 47 | * @param string $endpoint |
||
| 48 | * |
||
| 49 | * @return RpcResponseInterface |
||
| 50 | * @throws \Exception |
||
| 51 | */ |
||
| 52 | 7 | protected function getResponse(RequestInterface $request, $endpoint) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @param RequestInterface $request |
||
| 67 | * |
||
| 68 | * @return RpcResponseInterface |
||
| 69 | * |
||
| 70 | * @throws \RuntimeException |
||
| 71 | * @throws \InvalidArgumentException |
||
| 72 | * @throws \LogicException |
||
| 73 | * @throws MethodNotFoundException |
||
| 74 | */ |
||
| 75 | 7 | protected function handleSingleRequest(RequestInterface $request) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return KernelInterface |
||
| 120 | * @throws \RuntimeException |
||
| 121 | */ |
||
| 122 | 7 | private function getKernel() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param $name |
||
| 139 | * |
||
| 140 | * @return object|null |
||
| 141 | * @throws ServiceNotFoundException |
||
| 142 | * @throws ServiceCircularReferenceException |
||
| 143 | */ |
||
| 144 | 7 | protected function get($name) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Filters a response object. |
||
| 151 | * |
||
| 152 | * @param RpcResponseInterface $response A Response instance |
||
| 153 | * @param RequestInterface $request An error message in case the response is not a Response object |
||
| 154 | * |
||
| 155 | * @return RpcResponseInterface The filtered Response instance |
||
| 156 | * @throws \RuntimeException |
||
| 157 | */ |
||
| 158 | 3 | protected function filterResponse(RpcResponseInterface $response, RequestInterface $request) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Publishes the finish request event, then pop the request from the stack. |
||
| 169 | * |
||
| 170 | * Note that the order of the operations is important here, otherwise |
||
| 171 | * operations such as {@link RequestStack::getParentRequest()} can lead to |
||
| 172 | * weird results. |
||
| 173 | * |
||
| 174 | * @param RequestInterface $request |
||
| 175 | * |
||
| 176 | * @throws \RuntimeException |
||
| 177 | */ |
||
| 178 | 7 | protected function finishRequest(RequestInterface $request) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @return ControllerResolverInterface |
||
| 188 | */ |
||
| 189 | abstract protected function getResolver(); |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param $var |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | View Code Duplication | protected function varToString($var) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Handles an exception by trying to convert it to a Response. |
||
| 227 | * |
||
| 228 | * @param \Exception $e An \Exception instance |
||
| 229 | * @param RequestInterface $request A Request instance |
||
| 230 | * |
||
| 231 | * @return RpcResponseInterface A Response instance |
||
| 232 | * |
||
| 233 | * @throws \Exception |
||
| 234 | */ |
||
| 235 | 4 | protected function handleException(\Exception $e, RequestInterface $request) |
|
| 253 | } |
||
| 254 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: