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 namespace Garden; |
||
3 | abstract class Route { |
||
4 | /// Constants /// |
||
5 | |||
6 | const MAP_QUERY = 'query'; // map to the querystring. |
||
7 | const MAP_INPUT = 'input'; // map to the input (post). |
||
8 | const MAP_DATA = 'data'; // map to the querystring or input depending on the method. |
||
9 | |||
10 | /// Properties /// |
||
11 | |||
12 | /** |
||
13 | * @var array[string] An array of allowed http methods for this route. |
||
14 | */ |
||
15 | protected $methods; |
||
16 | |||
17 | protected $pattern; |
||
18 | |||
19 | /** |
||
20 | * @var array An array of parameter conditions. |
||
21 | */ |
||
22 | protected $conditions; |
||
23 | |||
24 | /** |
||
25 | * @var array An array of global parameter conditions. |
||
26 | */ |
||
27 | protected static $globalConditions; |
||
28 | |||
29 | /** |
||
30 | * @var array An array of parameter mappings. |
||
31 | */ |
||
32 | protected $mappings; |
||
33 | |||
34 | /** |
||
35 | * @var bool Whether or not to match the path with the file extension. |
||
36 | */ |
||
37 | protected $matchFullPath; |
||
38 | |||
39 | /** |
||
40 | * @var array An array of global parameter mappings. |
||
41 | */ |
||
42 | protected static $globalMappings = [ |
||
43 | 'data' => Route::MAP_DATA, |
||
44 | 'query' => Route::MAP_QUERY, |
||
45 | 'input' => Route::MAP_INPUT |
||
46 | ]; |
||
47 | |||
48 | /// Methods /// |
||
49 | |||
50 | /** |
||
51 | * Create and return a new route. |
||
52 | * |
||
53 | * @param string $pattern The pattern for the route. |
||
54 | * @param callable|string $callback Either a callback to map the route to or a string representing |
||
55 | * a format for {@link sprintf()}. |
||
56 | * @return \Garden\Route Returns the new route. |
||
57 | */ |
||
58 | 44 | public static function create($pattern, $callback) { |
|
66 | |||
67 | /** |
||
68 | * Dispatch the route. |
||
69 | * |
||
70 | * @param Request $request The current request we are dispatching against. |
||
71 | * @param array &$args The args to pass to the dispatch. |
||
72 | * These are the arguments returned from {@link Route::matches()}. |
||
73 | */ |
||
74 | abstract public function dispatch(Request $request, array &$args); |
||
75 | |||
76 | /** |
||
77 | * Gets or sets the route's conditions. |
||
78 | * |
||
79 | * @param array|null $conditions An array of conditions to set. |
||
80 | * @return Route|array |
||
81 | */ |
||
82 | 35 | View Code Duplication | public function conditions($conditions = null) { |
99 | |||
100 | /** |
||
101 | * Gets or sets the allowed http methods for this route. |
||
102 | * |
||
103 | * @param array|string|null $methods Set a new set of allowed methods or pass null to get the current methods. |
||
104 | * @return Route|array Returns the current methods or `$this` for fluent calls. |
||
105 | */ |
||
106 | 1 | public function methods($methods = null) { |
|
114 | |||
115 | /** |
||
116 | * Gets/sets the global conditions. |
||
117 | * |
||
118 | * @param array|null $conditions An array of conditions to set. |
||
119 | * @return array The current global conditions. |
||
120 | */ |
||
121 | 34 | View Code Duplication | public static function globalConditions($conditions = null) { |
137 | |||
138 | /** |
||
139 | * Gets or sets the mappings array that maps parameter names to mappings. |
||
140 | * |
||
141 | * @param array|null $mappings An array of mappings to set. |
||
142 | * @return Route|array Returns the current mappings or `$this` for fluent calls. |
||
143 | */ |
||
144 | View Code Duplication | public function mappings($mappings = null) { |
|
161 | |||
162 | /** |
||
163 | * Gets or sets the global mappings array that maps parameter names to mappings. |
||
164 | * |
||
165 | * @param array|null $mappings An array of mappings to set. |
||
166 | * @return array Returns the current global mappings. |
||
167 | */ |
||
168 | View Code Duplication | public static function globalMappings($mappings = null) { |
|
184 | |||
185 | /** |
||
186 | * Determine whether or not a parameter is mapped to special request data. |
||
187 | * |
||
188 | * @param string $name The name of the parameter to check. |
||
189 | * @return bool Returns true if the parameter is mapped, false otherwise. |
||
190 | */ |
||
191 | 10 | protected function isMapped($name) { |
|
195 | |||
196 | /** |
||
197 | * Get the mapped data for a parameter. |
||
198 | * |
||
199 | * @param string $name The name of the parameter. |
||
200 | * @param Request $request The {@link Request} to get the data from. |
||
201 | * @return array|null Returns the mapped data or null if there is no data. |
||
202 | */ |
||
203 | 2 | protected function mappedData($name, Request $request) { |
|
229 | |||
230 | /** |
||
231 | * Try matching a route to a request. |
||
232 | * |
||
233 | * @param Request $request The request to match the route with. |
||
234 | * @param Application $app The application instantiating the route. |
||
235 | * @return array|null Whether or not the route matches the request. |
||
236 | * If the route matches an array of args is returned, otherwise the function returns null. |
||
237 | */ |
||
238 | abstract public function matches(Request $request, Application $app); |
||
239 | |||
240 | /** |
||
241 | * Tests whether or not a route matches the allowed methods for this route. |
||
242 | * |
||
243 | * @param Request $request The request to test. |
||
244 | * @return bool Returns `true` if the route allows the method, otherwise `false`. |
||
245 | */ |
||
246 | 44 | protected function matchesMethods(Request $request) { |
|
252 | |||
253 | /** |
||
254 | * Gets or sets the route pattern. |
||
255 | * |
||
256 | * @param string|null $pattern The route pattern. |
||
257 | * @return string Returns the pattern. |
||
258 | */ |
||
259 | 44 | public function pattern($pattern = null) { |
|
265 | |||
266 | /** |
||
267 | * Get whether or not to match the path with the extension. |
||
268 | * |
||
269 | * @return boolean Returns **true** if the path should be matched with the file extension or **false** otherwise. |
||
270 | */ |
||
271 | 44 | public function getMatchFullPath() { |
|
274 | |||
275 | /** |
||
276 | * Set whether or not to match the path with the extension. |
||
277 | * |
||
278 | * @param boolean $matchFullPath The new value for the property. |
||
279 | * @return Route Returns `$this` for fluent calls. |
||
280 | */ |
||
281 | 1 | public function setMatchFullPath($matchFullPath) { |
|
285 | } |
||
286 |