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 |
||
21 | class ModuleManager extends BaseModuleManager |
||
22 | { |
||
23 | /** |
||
24 | * Returns an array of paths to modules. |
||
25 | * |
||
26 | * @return array An array of paths to each loaded module |
||
27 | */ |
||
28 | View Code Duplication | public function getModulesPath() |
|
37 | |||
38 | /** |
||
39 | * Returns the file path for a given resource. |
||
40 | * |
||
41 | * A Resource can be a file or a directory. |
||
42 | * |
||
43 | * The resource name must follow the following pattern: |
||
44 | * |
||
45 | * @<ModuleName>/path/to/a/file.something |
||
46 | * |
||
47 | * where ModuleName is the name of the module |
||
48 | * and the remaining part is the relative path in the module. |
||
49 | * |
||
50 | * If $dir is passed, and the first segment of the path is "Resources", |
||
51 | * this method will look for a file named: |
||
52 | * |
||
53 | * $dir/<ModuleName>/path/without/Resources |
||
54 | * |
||
55 | * before looking in the module resource folder. |
||
56 | * |
||
57 | * @param string $name A resource name to locate |
||
58 | * @param string $dir A directory where to look for the resource first |
||
59 | * @param Boolean $first Whether to return the first path or paths for all matching modules |
||
60 | * |
||
61 | * @throws \InvalidArgumentException if the file cannot be found or the name is not valid |
||
62 | * @throws \RuntimeException if the name contains invalid/unsafe |
||
63 | * @throws \RuntimeException if a custom resource is hidden by a resource in a derived module |
||
64 | * |
||
65 | * @return string|array The absolute path of the resource or an array if $first is false |
||
66 | * |
||
67 | * @api |
||
68 | */ |
||
69 | public function locateResource($name, $dir = null, $first = true) |
||
122 | |||
123 | protected function getResourcesPath($module) |
||
132 | } |
||
133 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.