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 |
||
18 | class GenericRouteGenerator |
||
19 | { |
||
20 | /** Default controller name */ |
||
21 | const CTR_BASE = '__base'; |
||
22 | const CTR_CACHE_BASE = '__cache_base'; |
||
23 | |||
24 | /** Universal controller name */ |
||
25 | const CTR_UNI = '__handler'; |
||
26 | const CTR_CACHE_UNI = '__cache_handler'; |
||
27 | |||
28 | /** Post controller name */ |
||
29 | const CTR_POST = '__post'; |
||
30 | const CTR_CACHE_POST = '__cache_post'; |
||
31 | |||
32 | /** Put controller name */ |
||
33 | const CTR_PUT = '__put'; |
||
34 | const CTR_CACHE_PUT = '__cache_put'; |
||
35 | |||
36 | /** Delete controller name */ |
||
37 | const CTR_DELETE = '__delete'; |
||
38 | const CTR_CACHE_DELETE = '__cache_delete'; |
||
39 | |||
40 | /** Delete controller name */ |
||
41 | const CTR_UPDATE = '__update'; |
||
42 | const CTR_CACHE_UPDATE = '__cache_update'; |
||
43 | |||
44 | /** Controllers naming conventions */ |
||
45 | |||
46 | /** Procedural controller prefix */ |
||
47 | const PROC_PREFIX = '_'; |
||
48 | /** OOP controller prefix */ |
||
49 | const OBJ_PREFIX = '__'; |
||
50 | /** AJAX controller prefix */ |
||
51 | const ASYNC_PREFIX = 'async_'; |
||
52 | /** CACHE controller prefix */ |
||
53 | const CACHE_PREFIX = 'cache_'; |
||
54 | |||
55 | /** @var RouteCollection Generated routes collection */ |
||
56 | protected $routes; |
||
57 | |||
58 | /** @var Object[] Collection of SamsonPHP modules */ |
||
59 | protected $modules; |
||
60 | |||
61 | /** |
||
62 | * @return RouteCollection Generated routes collection |
||
63 | */ |
||
64 | public function &routes() |
||
68 | |||
69 | /** |
||
70 | * GenericRouteGenerator constructor. |
||
71 | * @param Module[] $modules |
||
72 | */ |
||
73 | public function __construct(array & $modules) |
||
78 | |||
79 | /** |
||
80 | * Load all SamsonPHP web-application routes. |
||
81 | * |
||
82 | * @return RouteCollection Collection of web-application routes |
||
83 | */ |
||
84 | public function &generate() |
||
101 | |||
102 | /** |
||
103 | * Class method signature parameters. |
||
104 | * |
||
105 | * @param object $object Object |
||
106 | * @param string $method Method name |
||
107 | * @return array Method parameters |
||
108 | */ |
||
109 | protected function getMethodParameters($object, $method) |
||
116 | |||
117 | /** |
||
118 | * Convert class method signature into route pattern with parameters. |
||
119 | * |
||
120 | * @param object $object Object |
||
121 | * @param string $method Method name |
||
122 | * @return string Pattern string with parameters placeholders |
||
123 | */ |
||
124 | protected function buildMethodParameters($object, $method) |
||
137 | |||
138 | /** |
||
139 | * @param $module |
||
140 | * @param $prefix |
||
141 | * @param $method |
||
142 | * @param $action |
||
143 | * @param string $async |
||
144 | * @return RouteCollection |
||
145 | * @throws \samsonframework\routing\exception\IdentifierDuplication |
||
146 | */ |
||
147 | protected function getParametrizedRoutes($module, $prefix, $method, $action = '', $async = '') |
||
206 | |||
207 | /** |
||
208 | * Generate old-fashioned routes collection. |
||
209 | * |
||
210 | * @param Object $module |
||
211 | * @return array |
||
212 | */ |
||
213 | protected function createGenericRoutes(&$module) |
||
279 | } |
||
280 |