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 |
||
12 | class RoutingLoader extends FileLoader |
||
13 | { |
||
14 | // Assoc beetween a controller and its route path |
||
15 | //@todo make an object for this |
||
16 | protected $actions = array( |
||
17 | 'list' => array( |
||
18 | 'path' => '/', |
||
19 | 'defaults' => array(), |
||
20 | 'requirements' => array(), |
||
21 | 'methods' => array('GET'), |
||
22 | ), |
||
23 | 'excel'=> array( |
||
24 | 'path' => '/excel/{key}', |
||
25 | 'defaults' => array('key'=>null), |
||
26 | 'requirements' => array(), |
||
27 | 'methods' => array('GET'), |
||
28 | 'controller' => 'excel', |
||
29 | ), |
||
30 | 'edit' => array( |
||
31 | 'path' => '/{pk}/edit', |
||
32 | 'defaults' => array(), |
||
33 | 'requirements' => array(), |
||
34 | 'methods' => array('GET'), |
||
35 | ), |
||
36 | 'update' => array( |
||
37 | 'path' => '/{pk}/update', |
||
38 | 'defaults' => array(), |
||
39 | 'requirements' => array(), |
||
40 | 'methods' => array('POST'), |
||
41 | 'controller' => 'edit', |
||
42 | ), |
||
43 | 'show' => array( |
||
44 | 'path' => '/{pk}/show', |
||
45 | 'defaults' => array(), |
||
46 | 'requirements' => array(), |
||
47 | 'methods' => array('GET'), |
||
48 | ), |
||
49 | 'object' => array( |
||
50 | 'path' => '/{pk}/{action}', |
||
51 | 'defaults' => array(), |
||
52 | 'requirements' => array(), |
||
53 | 'methods' => array('GET', 'POST'), |
||
54 | 'controller' => 'actions', |
||
55 | ), |
||
56 | 'batch' => array( |
||
57 | 'path' => '/batch', |
||
58 | 'defaults' => array(), |
||
59 | 'requirements' => array(), |
||
60 | 'methods' => array('POST'), |
||
61 | 'controller' => 'actions', |
||
62 | ), |
||
63 | 'new' => array( |
||
64 | 'path' => '/new', |
||
65 | 'defaults' => array(), |
||
66 | 'requirements' => array(), |
||
67 | 'methods' => array('GET'), |
||
68 | ), |
||
69 | 'create' => array( |
||
70 | 'path' => '/create', |
||
71 | 'defaults' => array(), |
||
72 | 'requirements' => array(), |
||
73 | 'methods' => array('POST'), |
||
74 | 'controller' => 'new', |
||
75 | ), |
||
76 | 'filters' => array( |
||
77 | 'path' => '/filter', |
||
78 | 'defaults' => array(), |
||
79 | 'requirements' => array(), |
||
80 | 'methods' => array('POST', 'GET'), |
||
81 | 'controller' => 'list', |
||
82 | ), |
||
83 | 'scopes' => array( |
||
84 | 'path' => '/scope/{group}/{scope}', |
||
85 | 'defaults' => array(), |
||
86 | 'requirements' => array(), |
||
87 | 'methods' => array('POST', 'GET'), |
||
88 | 'controller' => 'list', |
||
89 | ), |
||
90 | ); |
||
91 | |||
92 | /** |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $yaml = array(); |
||
96 | |||
97 | public function load($resource, $type = null) |
||
159 | |||
160 | public function supports($resource, $type = null) |
||
164 | |||
165 | /** |
||
166 | * @return string |
||
167 | */ |
||
168 | protected function getControllerFolder($resource) |
||
174 | |||
175 | /** |
||
176 | * @return string |
||
177 | */ |
||
178 | protected function getBundleNameFromResource($resource) |
||
184 | |||
185 | protected function getNamespaceFromResource($resource) |
||
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | protected function getGeneratorFilePath($resource) |
||
224 | |||
225 | /** |
||
226 | * @param string $yaml_path string with point for levels |
||
227 | */ |
||
228 | View Code Duplication | protected function getFromYaml($yaml_path, $default = null) |
|
241 | } |
||
242 |
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.