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 |
||
| 37 | class MiddlewareDispatcher { |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array array containing all the middlewares |
||
| 41 | */ |
||
| 42 | private $middlewares; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int counter which tells us what middlware was executed once an |
||
| 46 | * exception occurs |
||
| 47 | */ |
||
| 48 | private $middlewareCounter; |
||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor |
||
| 53 | */ |
||
| 54 | public function __construct(){ |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * Adds a new middleware |
||
| 62 | * @param Middleware $middleWare the middleware which will be added |
||
| 63 | */ |
||
| 64 | public function registerMiddleware(Middleware $middleWare){ |
||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * returns an array with all middleware elements |
||
| 71 | * @return array the middlewares |
||
| 72 | */ |
||
| 73 | public function getMiddlewares(){ |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * This is being run in normal order before the controller is being |
||
| 80 | * called which allows several modifications and checks |
||
| 81 | * |
||
| 82 | * @param Controller $controller the controller that is being called |
||
| 83 | * @param string $methodName the name of the method that will be called on |
||
| 84 | * the controller |
||
| 85 | */ |
||
| 86 | public function beforeController(Controller $controller, $methodName){ |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * This is being run when either the beforeController method or the |
||
| 100 | * controller method itself is throwing an exception. The middleware is asked |
||
| 101 | * in reverse order to handle the exception and to return a response. |
||
| 102 | * If the response is null, it is assumed that the exception could not be |
||
| 103 | * handled and the error will be thrown again |
||
| 104 | * |
||
| 105 | * @param Controller $controller the controller that is being called |
||
| 106 | * @param string $methodName the name of the method that will be called on |
||
| 107 | * the controller |
||
| 108 | * @param \Exception $exception the thrown exception |
||
| 109 | * @return Response a Response object if the middleware can handle the |
||
| 110 | * exception |
||
| 111 | * @throws \Exception the passed in exception if it cant handle it |
||
| 112 | */ |
||
| 113 | public function afterException(Controller $controller, $methodName, \Exception $exception){ |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * This is being run after a successful controllermethod call and allows |
||
| 128 | * the manipulation of a Response object. The middleware is run in reverse order |
||
| 129 | * |
||
| 130 | * @param Controller $controller the controller that is being called |
||
| 131 | * @param string $methodName the name of the method that will be called on |
||
| 132 | * the controller |
||
| 133 | * @param Response $response the generated response from the controller |
||
| 134 | * @return Response a Response object |
||
| 135 | */ |
||
| 136 | View Code Duplication | public function afterController(Controller $controller, $methodName, Response $response){ |
|
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * This is being run after the response object has been rendered and |
||
| 147 | * allows the manipulation of the output. The middleware is run in reverse order |
||
| 148 | * |
||
| 149 | * @param Controller $controller the controller that is being called |
||
| 150 | * @param string $methodName the name of the method that will be called on |
||
| 151 | * the controller |
||
| 152 | * @param string $output the generated output from a response |
||
| 153 | * @return string the output that should be printed |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function beforeOutput(Controller $controller, $methodName, $output){ |
|
| 162 | |||
| 163 | } |
||
| 164 |
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.