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 |
||
27 | class Bus implements BusInterface |
||
28 | { |
||
29 | /** |
||
30 | * @var SortedArrayHashMap |
||
31 | */ |
||
32 | protected $middlewares; |
||
33 | |||
34 | /** |
||
35 | * Bus constructor. |
||
36 | * |
||
37 | * @param MiddlewareInterface[] $middlewares |
||
38 | */ |
||
39 | public function __construct(array $middlewares = array()) |
||
50 | |||
51 | /** |
||
52 | * Adds a middleware to the middleware list. The higher priority value, the earlier a middleware |
||
53 | * will be triggered in the chain (defaults to 0). |
||
54 | * |
||
55 | * @param MiddlewareInterface $middleware |
||
56 | * @param int $priority |
||
57 | */ |
||
58 | public function addMiddleware(MiddlewareInterface $middleware, $priority = 0) |
||
70 | |||
71 | /** |
||
72 | * Add a middleware before a given middleware. |
||
73 | * |
||
74 | * @param MiddlewareInterface $middleware |
||
75 | * @param MiddlewareInterface $target |
||
76 | * |
||
77 | * @throws \InvalidArgumentException |
||
78 | */ |
||
79 | View Code Duplication | public function addMiddlewareBefore(MiddlewareInterface $middleware, MiddlewareInterface $target) |
|
93 | |||
94 | /** |
||
95 | * Add a middleware before a given middleware. |
||
96 | * |
||
97 | * @param MiddlewareInterface $middleware |
||
98 | * @param MiddlewareInterface $target |
||
99 | * |
||
100 | * @throws \InvalidArgumentException |
||
101 | */ |
||
102 | View Code Duplication | public function addMiddlewareAfter(MiddlewareInterface $middleware, MiddlewareInterface $target) |
|
116 | |||
117 | /** |
||
118 | * @param MiddlewareInterface $middleware |
||
119 | * |
||
120 | * @return int|null |
||
121 | */ |
||
122 | protected function middlewarePriority(MiddlewareInterface $middleware) |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function dispatch(MessageInterface $message) |
||
144 | |||
145 | /** |
||
146 | * @return Delegate |
||
147 | */ |
||
148 | private function chainedExecution() |
||
172 | } |
||
173 |
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.