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 | //[PHPCOMPRESSOR(remove,start)] |
||
55 | |||
56 | /** @var RouteCollection Generated routes collection */ |
||
57 | protected $routes; |
||
58 | |||
59 | /** @var Object[] Collection of SamsonPHP modules */ |
||
60 | protected $modules; |
||
61 | |||
62 | /** |
||
63 | * @return RouteCollection Generated routes collection |
||
64 | */ |
||
65 | public function &routes() |
||
69 | |||
70 | /** |
||
71 | * GenericRouteGenerator constructor. |
||
72 | * @param Module[] $modules |
||
73 | */ |
||
74 | public function __construct(array & $modules) |
||
79 | |||
80 | /** |
||
81 | * Load all SamsonPHP web-application routes. |
||
82 | * |
||
83 | * @return RouteCollection Collection of web-application routes |
||
84 | */ |
||
85 | public function &generate() |
||
100 | |||
101 | /** |
||
102 | * Class method signature parameters. |
||
103 | * |
||
104 | * @param mixed $object Object |
||
105 | * @param string $method Method name |
||
106 | * @return \ReflectionParameter[] Method parameters |
||
107 | */ |
||
108 | protected function getMethodParameters($object, $method) |
||
115 | |||
116 | /** |
||
117 | * Convert class method signature into route pattern with parameters. |
||
118 | * |
||
119 | * @param mixed $object Object |
||
120 | * @param string $method Method name |
||
121 | * @return string Pattern string with parameters placeholders |
||
122 | */ |
||
123 | protected function buildMethodParameters($object, $method) |
||
136 | |||
137 | /** |
||
138 | * @param $module |
||
139 | * @param $prefix |
||
140 | * @param $method |
||
141 | * @param string $action |
||
142 | * @param string $async |
||
143 | * @param string $cache |
||
144 | * @return RouteCollection |
||
145 | * @throws \samsonframework\routing\exception\IdentifierDuplication |
||
146 | */ |
||
147 | protected function getParametrizedRoutes($module, $prefix, $method, $action = '', $async = '', $cache = '') |
||
206 | |||
207 | /** |
||
208 | * Generate old-fashioned routes collection. |
||
209 | * |
||
210 | * @param Object $module |
||
211 | * @return RouteCollection |
||
212 | */ |
||
213 | protected function createGenericRoutes(&$module) |
||
291 | //[PHPCOMPRESSOR(remove,end)] |
||
292 | } |
||
293 |