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 |
||
| 45 | class CORSMiddleware extends Middleware { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var IRequest |
||
| 49 | */ |
||
| 50 | private $request; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var ControllerMethodReflector |
||
| 54 | */ |
||
| 55 | private $reflector; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var IUserSession |
||
| 59 | */ |
||
| 60 | private $session; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var IConfig |
||
| 64 | */ |
||
| 65 | private $config; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param IRequest $request |
||
| 69 | * @param ControllerMethodReflector $reflector |
||
| 70 | * @param IUserSession $session |
||
| 71 | * @param IConfig $config |
||
| 72 | */ |
||
| 73 | View Code Duplication | public function __construct(IRequest $request, |
|
| 82 | |||
| 83 | /** |
||
| 84 | * This is being run after a successful controllermethod call and allows |
||
| 85 | * the manipulation of a Response object. The middleware is run in reverse order |
||
| 86 | * |
||
| 87 | * @param Controller $controller the controller that is being called |
||
| 88 | * @param string $methodName the name of the method that will be called on |
||
| 89 | * the controller |
||
| 90 | * @param Response $response the generated response from the controller |
||
| 91 | * @return Response a Response object |
||
| 92 | * @throws SecurityException |
||
| 93 | */ |
||
| 94 | public function afterController($controller, $methodName, Response $response){ |
||
| 124 | |||
| 125 | /** |
||
| 126 | * If an SecurityException is being caught return a JSON error response |
||
| 127 | * |
||
| 128 | * @param Controller $controller the controller that is being called |
||
| 129 | * @param string $methodName the name of the method that will be called on |
||
| 130 | * the controller |
||
| 131 | * @param \Exception $exception the thrown exception |
||
| 132 | * @throws \Exception the passed in exception if it can't handle it |
||
| 133 | * @return Response a Response object or null in case that the exception could not be handled |
||
| 134 | */ |
||
| 135 | public function afterException($controller, $methodName, \Exception $exception){ |
||
| 148 | |||
| 149 | } |
||
| 150 |