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 Queue extends ObjectAbstract implements QueueInterface |
||
38 | { |
||
39 | /** |
||
40 | * @var \SplQueue |
||
41 | * @access protected |
||
42 | */ |
||
43 | protected $queue; |
||
44 | |||
45 | /** |
||
46 | * Constructor |
||
47 | * |
||
48 | * @param array $middlewares |
||
49 | * @access public |
||
50 | */ |
||
51 | public function __construct(array $middlewares = []) |
||
59 | |||
60 | /** |
||
61 | * Compatible with middlewares of the signature |
||
62 | * |
||
63 | * ```php |
||
64 | * fn($request, $response, callable $next) |
||
65 | * ``` |
||
66 | * |
||
67 | * @param RequestInterface $request |
||
68 | * @param ResponseInterface $response |
||
69 | * @param DelegateInterface $next |
||
70 | * @return ResponseInterface |
||
71 | * @access public |
||
72 | */ |
||
73 | public function __invoke( |
||
80 | |||
81 | /** |
||
82 | * {@inheritDoc} |
||
83 | */ |
||
84 | public function push($middleware, $condition = null) |
||
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | */ |
||
94 | public function process( |
||
105 | |||
106 | /** |
||
107 | * {@inheritDoc} |
||
108 | */ |
||
109 | public function next( |
||
128 | |||
129 | /** |
||
130 | * Fill the queue with middlewares |
||
131 | * |
||
132 | * @param array $middlewares |
||
133 | * @access protected |
||
134 | */ |
||
135 | protected function fillTheQueue(array $middlewares) |
||
148 | |||
149 | /** |
||
150 | * Process/run this middleware |
||
151 | * |
||
152 | * @param MiddlewareInterface|callable $middleware |
||
153 | * @param RequestInterface $request |
||
154 | * @param ResponseInterface $response |
||
155 | * @return ResponseInterface |
||
156 | * @throws LogicException if invalid middleware type |
||
157 | * @access protected |
||
158 | */ |
||
159 | View Code Duplication | protected function runMiddleware( |
|
180 | |||
181 | /** |
||
182 | * Evaluate the condition |
||
183 | * |
||
184 | * support both a callable returns bool value or an object instance of |
||
185 | * ConditionInterface. |
||
186 | * |
||
187 | * @param ConditionInterface|callable $condition |
||
188 | * @param RequestInterface $request |
||
189 | * @param ResponseInterface $response |
||
190 | * @return bool |
||
191 | * @throws LogicException if condition is invalid |
||
192 | * @access protected |
||
193 | */ |
||
194 | View Code Duplication | protected function evalCondition( |
|
215 | } |
||
216 |
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.