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 |
||
26 | trait ControllerCollectorTrait |
||
27 | { |
||
28 | |||
29 | abstract public function getWildcards(); |
||
31 | |||
32 | /** |
||
33 | * Define how controller actions names will be joined to form the route pattern. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | |||
38 | protected $controllerActionJoin = "/"; |
||
39 | |||
40 | /** |
||
41 | * Maps all the controller methods that begins with a HTTP method, and maps the rest of |
||
42 | * name as a path. The path will be the method name with slashes before every camelcased |
||
43 | * word and without the HTTP method prefix, and the controller name will be used to prefix |
||
44 | * the route pattern. e.g. ArticlesController::getCreate will generate a route to: GET articles/create |
||
45 | * |
||
46 | * @param string $controller The controller name |
||
47 | * @param string $prefix |
||
48 | * |
||
49 | * @throws \ReflectionException |
||
50 | * @return Group |
||
51 | */ |
||
52 | |||
53 | 5 | public function controller($controller, $prefix = null) |
|
60 | |||
61 | /** |
||
62 | * Maps several controllers at same time. |
||
63 | * |
||
64 | * @param string[] $controllers Controllers name. |
||
65 | * @throws \ReflectionException |
||
66 | * @return Group |
||
67 | */ |
||
68 | |||
69 | 1 | View Code Duplication | public function controllers(array $controllers) |
76 | |||
77 | /** |
||
78 | * Alias for Collector::controller but maps a controller without using the controller name as prefix. |
||
79 | * |
||
80 | * @param string $controller The controller name |
||
81 | * @throws \ReflectionException |
||
82 | * @return Group |
||
83 | */ |
||
84 | |||
85 | 2 | public function controllerWithoutPrefix($controller) |
|
91 | |||
92 | /** |
||
93 | * Alias for Collector::controllers but maps a controller without using the controller name as prefix. |
||
94 | * |
||
95 | * @param string[] $controllers |
||
96 | * @throws \ReflectionException |
||
97 | * @return Group |
||
98 | */ |
||
99 | |||
100 | 1 | View Code Duplication | public function controllersWithoutPrefix(array $controllers) |
107 | |||
108 | /** |
||
109 | * @param ReflectionClass $controller |
||
110 | * @param string[] $methods |
||
111 | * @param string $prefix |
||
112 | * |
||
113 | * @return Group |
||
114 | */ |
||
115 | |||
116 | 7 | protected function collectControllerRoutes(ReflectionClass $controller, array $methods, $prefix) |
|
145 | |||
146 | /** |
||
147 | * @param ReflectionClass $controller |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | |||
152 | 4 | protected function getControllerPrefix(ReflectionClass $controller) |
|
157 | |||
158 | /** |
||
159 | * @param \ReflectionMethod |
||
160 | * @return string |
||
161 | */ |
||
162 | |||
163 | 7 | protected function getMethodConstraints(ReflectionMethod $method) |
|
183 | |||
184 | /** |
||
185 | * @param ReflectionParameter $parameter |
||
186 | * @param string[] $types |
||
187 | * @return string |
||
188 | */ |
||
189 | |||
190 | 7 | protected function getPathConstraint(ReflectionParameter $parameter, $types) |
|
196 | |||
197 | /** |
||
198 | * @param ReflectionMethod $method |
||
199 | * @return string[] |
||
200 | */ |
||
201 | |||
202 | 7 | protected function getParamsConstraint(ReflectionMethod $method) |
|
215 | |||
216 | /** |
||
217 | * @param ReflectionClass|ReflectionMethod $reflector |
||
218 | * @return string|null |
||
219 | */ |
||
220 | |||
221 | 7 | protected function getAnnotatedStrategy($reflector) |
|
226 | |||
227 | /** |
||
228 | * Define how controller actions names will be joined to form the route pattern. |
||
229 | * Defaults to "/" so actions like "getMyAction" will be "/my/action". If changed to |
||
230 | * "-" the new pattern will be "/my-action". |
||
231 | * |
||
232 | * @param string $join |
||
233 | */ |
||
234 | |||
235 | 1 | public function setControllerActionJoin($join) |
|
239 | |||
240 | /** |
||
241 | * @return string |
||
242 | */ |
||
243 | |||
244 | public function getControllerActionJoin() |
||
248 | |||
249 | } |
||
250 |
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.