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 |
||
| 11 | class YamlLoader extends FileLoader |
||
| 12 | { |
||
| 13 | private static $availableKeys = [ |
||
| 14 | 'resource', |
||
| 15 | 'type', |
||
| 16 | 'prefix', |
||
| 17 | 'method', |
||
| 18 | 'controller', |
||
| 19 | 'context', |
||
| 20 | 'default_context', |
||
| 21 | 'inherit', |
||
| 22 | ]; |
||
| 23 | |||
| 24 | /** @var Parser */ |
||
| 25 | private $parser; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Loads a resource. |
||
| 29 | * |
||
| 30 | * @param mixed $resource The resource |
||
| 31 | * @param string|null $type The resource type or null if unknown |
||
| 32 | * |
||
| 33 | * @return MethodCollection |
||
| 34 | * @throws \Exception If something went wrong |
||
| 35 | */ |
||
| 36 | 2 | public function load($resource, $type = null) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Returns whether this class supports the given resource. |
||
| 86 | * |
||
| 87 | * @param mixed $resource A resource |
||
| 88 | * @param string|null $type The resource type or null if unknown |
||
| 89 | * |
||
| 90 | * @return bool True if this class supports the given resource, false otherwise |
||
| 91 | */ |
||
| 92 | 2 | public function supports($resource, $type = null) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Validates the route configuration. |
||
| 101 | * |
||
| 102 | * @param array $config A resource config |
||
| 103 | * @param string $name The config key |
||
| 104 | * @param string $path The loaded file path |
||
| 105 | * |
||
| 106 | * @return array |
||
| 107 | * @throws \InvalidArgumentException If one of the provided config keys is not supported, |
||
| 108 | * something is missing or the combination is nonsense |
||
| 109 | */ |
||
| 110 | 2 | protected function validate($config, $name, $path) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Parses an import and adds the routes in the resource to the RouteCollection. |
||
| 152 | * |
||
| 153 | * @param MethodCollection $collection A RouteCollection instance |
||
| 154 | * @param array $config Route definition |
||
| 155 | * @param string $path Full path of the YAML file being processed |
||
| 156 | * @param string $file Loaded file name |
||
| 157 | */ |
||
| 158 | 2 | protected function parseImport(MethodCollection $collection, array $config, $path, $file) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Parses a route and adds it to the RouteCollection. |
||
| 179 | * |
||
| 180 | * @param MethodCollection $collection A RouteCollection instance |
||
| 181 | * @param string $name Route name |
||
| 182 | * @param array $config Route definition |
||
| 183 | * @param string $path Full path of the YAML file being processed |
||
| 184 | */ |
||
| 185 | 2 | protected function parseRoute(MethodCollection $collection, $name, array $config, $path) |
|
| 200 | } |
||
| 201 |
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.