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 |
||
| 30 | abstract class ResourceLocator { |
||
| 31 | /** |
||
| 32 | * @var Theme |
||
| 33 | */ |
||
| 34 | protected $theme; |
||
| 35 | |||
| 36 | protected $mapping; |
||
| 37 | protected $serverroot; |
||
| 38 | protected $thirdpartyroot; |
||
| 39 | protected $webroot; |
||
| 40 | |||
| 41 | protected $resources = []; |
||
| 42 | |||
| 43 | /** @var \OCP\ILogger */ |
||
| 44 | protected $logger; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param \OCP\ILogger $logger |
||
| 48 | * @param Theme $theme |
||
| 49 | * @param array $core_map |
||
| 50 | * @param array $party_map |
||
| 51 | */ |
||
| 52 | public function __construct(\OCP\ILogger $logger, $theme, $core_map, $party_map) { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $resource |
||
| 63 | */ |
||
| 64 | abstract public function doFind($resource); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $resource |
||
| 68 | */ |
||
| 69 | abstract public function doFindTheme($resource); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Finds the resources and adds them to the list |
||
| 73 | * |
||
| 74 | * @param array $resources |
||
| 75 | */ |
||
| 76 | public function find($resources) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * append the $file resource once if exist at $root |
||
| 99 | * |
||
| 100 | * @param string $root path to check |
||
| 101 | * @param string $file the filename |
||
| 102 | * @param string|null $webRoot base for path, default map $root to $webRoot |
||
| 103 | * @return bool True if the resource was found, false otherwise |
||
| 104 | */ |
||
| 105 | protected function appendOnceIfExist($root, $file, $webRoot = null) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * append the $file resource at $root |
||
| 118 | * |
||
| 119 | * @param string $root path to check |
||
| 120 | * @param string $file the filename |
||
| 121 | * @param string|null $webRoot base for path, default map $root to $webRoot |
||
| 122 | * @param bool $throw Throw an exception, when the route does not exist |
||
| 123 | * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing |
||
| 124 | */ |
||
| 125 | protected function append($root, $file, $webRoot = null, $throw = true) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * build a path by given parts concatenated with a '/' (DIRECTORY_SEPARATOR) |
||
| 140 | * |
||
| 141 | * @param string[] $parts path parts to concatenate |
||
| 142 | * @return string $parts concatenated |
||
| 143 | */ |
||
| 144 | private function buildPath($parts){ |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns the list of all resources that should be loaded |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | public function getResources() { |
||
| 155 | } |
||
| 156 |
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.