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 Route |
||
13 | { |
||
14 | /** |
||
15 | * @var string $name a name for this route. |
||
16 | * @var string $info description of route. |
||
17 | * @var array $method the method(s) to support |
||
18 | * @var string $methodMatched the matched method. |
||
19 | * @var string $mount where to mount the path |
||
20 | * @var string $path the path rule for this route |
||
21 | * @var string $pathMatched the matched path. |
||
22 | * @var callable $handler the callback to handle this route |
||
23 | * @var null|array $arguments arguments for the callback, extracted |
||
24 | * from path |
||
25 | */ |
||
26 | private $name; |
||
27 | private $info; |
||
28 | private $method; |
||
29 | private $methodMatched; |
||
30 | private $mount; |
||
31 | private $path; |
||
32 | private $pathMatched; |
||
33 | private $handler; |
||
34 | private $arguments = []; |
||
35 | |||
36 | |||
37 | |||
38 | /** |
||
39 | * Set values for route. |
||
40 | * |
||
41 | * @param string|array $method as request method to support |
||
42 | * @param string $mount where to mount the path |
||
43 | * @param string $path for this route |
||
44 | * @param string|array|callable $handler for this path, callable or equal |
||
45 | * @param string $info description of the route |
||
46 | * |
||
47 | * @return $this |
||
48 | */ |
||
49 | public function set( |
||
71 | |||
72 | |||
73 | |||
74 | /** |
||
75 | * Check if the route matches a query and request method. |
||
76 | * |
||
77 | * @param string $query to match against |
||
78 | * @param string $method as request method |
||
79 | * |
||
80 | * @return boolean true if query matches the route |
||
81 | */ |
||
82 | public function match(string $query, string $method = null) |
||
101 | |||
102 | |||
103 | |||
104 | /** |
||
105 | * Handle the action for the route. |
||
106 | * |
||
107 | * @param string $path the matched path |
||
108 | * @param ContainerInjectableInterface $di container with services |
||
109 | * |
||
110 | * @return mixed |
||
111 | */ |
||
112 | public function handle( |
||
127 | |||
128 | |||
129 | |||
130 | /** |
||
131 | * Get the matched basename of the path, its the part without the mount |
||
132 | * point. |
||
133 | * |
||
134 | * @return string|null |
||
135 | */ |
||
136 | public function getMatchedPath() |
||
148 | |||
149 | |||
150 | |||
151 | /** |
||
152 | * Set the name of the route. |
||
153 | * |
||
154 | * @param string $name set a name for the route |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function setName($name) |
||
163 | |||
164 | |||
165 | |||
166 | /** |
||
167 | * Get information of the route. |
||
168 | * |
||
169 | * @return null|string as route information. |
||
170 | */ |
||
171 | public function getInfo() |
||
175 | |||
176 | |||
177 | |||
178 | /** |
||
179 | * Get the path for the route. |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | public function getPath() |
||
187 | |||
188 | |||
189 | |||
190 | /** |
||
191 | * Get the absolute $path by adding $mount. |
||
192 | * |
||
193 | * @return string|null as absolute path for this route. |
||
194 | */ |
||
195 | public function getAbsolutePath() |
||
207 | |||
208 | |||
209 | |||
210 | /** |
||
211 | * Get the request method for the route. |
||
212 | * |
||
213 | * @return string representing the request method supported |
||
214 | */ |
||
215 | public function getRequestMethod() : string |
||
221 | } |
||
222 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.