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 |
||
17 | class YamlLoader extends FileLoader |
||
18 | { |
||
19 | private static $availableKeys = [ |
||
20 | 'resource', |
||
21 | 'type', |
||
22 | 'prefix', |
||
23 | 'method', |
||
24 | 'controller', |
||
25 | 'context', |
||
26 | 'default_context', |
||
27 | 'inherit', |
||
28 | ]; |
||
29 | |||
30 | /** @var Parser */ |
||
31 | private $parser; |
||
32 | |||
33 | /** |
||
34 | * Loads a resource. |
||
35 | * |
||
36 | * @param mixed $resource The resource |
||
37 | * @param string|null $type The resource type or null if unknown |
||
38 | * |
||
39 | * @return MethodCollection |
||
40 | * @throws \Exception If something went wrong |
||
41 | */ |
||
42 | 8 | public function load($resource, $type = null) |
|
89 | |||
90 | /** |
||
91 | * Validates the route configuration. |
||
92 | * |
||
93 | * @param array $config A resource config |
||
94 | * @param string $name The config key |
||
95 | * @param string $path The loaded file path |
||
96 | * |
||
97 | * @return array |
||
98 | * @throws \InvalidArgumentException If one of the provided config keys is not supported, |
||
99 | * something is missing or the combination is nonsense |
||
100 | */ |
||
101 | 8 | protected function validate($config, $name, $path) |
|
140 | |||
141 | /** |
||
142 | * Parses an import and adds the routes in the resource to the RouteCollection. |
||
143 | * |
||
144 | * @param MethodCollection $collection A RouteCollection instance |
||
145 | * @param array $config Route definition |
||
146 | * @param string $path Full path of the YAML file being processed |
||
147 | * @param string $file Loaded file name |
||
148 | */ |
||
149 | 8 | protected function parseImport(MethodCollection $collection, array $config, $path, $file) |
|
167 | |||
168 | /** |
||
169 | * Parses a route and adds it to the RouteCollection. |
||
170 | * |
||
171 | * @param MethodCollection $collection A RouteCollection instance |
||
172 | * @param string $name Route name |
||
173 | * @param array $config Route definition |
||
174 | * @param string $path Full path of the YAML file being processed |
||
175 | */ |
||
176 | 8 | protected function parseRoute(MethodCollection $collection, $name, array $config, $path) |
|
191 | |||
192 | /** |
||
193 | * Returns whether this class supports the given resource. |
||
194 | * |
||
195 | * @param mixed $resource A resource |
||
196 | * @param string|null $type The resource type or null if unknown |
||
197 | * |
||
198 | * @return bool True if this class supports the given resource, false otherwise |
||
199 | */ |
||
200 | 8 | public function supports($resource, $type = null) |
|
206 | } |
||
207 |
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.