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 |
||
| 29 | abstract class ResourceLocator { |
||
| 30 | protected $theme; |
||
| 31 | |||
| 32 | protected $mapping; |
||
| 33 | protected $serverroot; |
||
| 34 | protected $thirdpartyroot; |
||
| 35 | protected $webroot; |
||
| 36 | |||
| 37 | protected $resources = array(); |
||
| 38 | |||
| 39 | /** @var \OCP\ILogger */ |
||
| 40 | protected $logger; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param \OCP\ILogger $logger |
||
| 44 | * @param string $theme |
||
| 45 | * @param array $core_map |
||
| 46 | * @param array $party_map |
||
| 47 | */ |
||
| 48 | public function __construct(\OCP\ILogger $logger, $theme, $core_map, $party_map) { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param string $resource |
||
| 59 | */ |
||
| 60 | abstract public function doFind($resource); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param string $resource |
||
| 64 | */ |
||
| 65 | abstract public function doFindTheme($resource); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Finds the resources and adds them to the list |
||
| 69 | * |
||
| 70 | * @param array $resources |
||
| 71 | */ |
||
| 72 | public function find($resources) { |
||
| 73 | foreach ($resources as $resource) { |
||
| 74 | try { |
||
| 75 | $this->doFind($resource); |
||
| 76 | } catch (ResourceNotFoundException $e) { |
||
| 77 | $resourceApp = substr($resource, 0, strpos($resource, '/')); |
||
| 78 | $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | if (!empty($this->theme)) { |
||
| 82 | foreach ($resources as $resource) { |
||
| 83 | try { |
||
| 84 | $this->doFindTheme($resource); |
||
| 85 | } catch (ResourceNotFoundException $e) { |
||
| 86 | $resourceApp = substr($resource, 0, strpos($resource, '/')); |
||
| 87 | $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * append the $file resource if exist at $root |
||
| 95 | * |
||
| 96 | * @param string $root path to check |
||
| 97 | * @param string $file the filename |
||
| 98 | * @param string|null $webRoot base for path, default map $root to $webRoot |
||
| 99 | * @return bool True if the resource was found, false otherwise |
||
| 100 | */ |
||
| 101 | protected function appendIfExist($root, $file, $webRoot = null) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Attempt to find the webRoot |
||
| 111 | * |
||
| 112 | * traverse the potential web roots upwards in the path |
||
| 113 | * |
||
| 114 | * example: |
||
| 115 | * - root: /srv/www/apps/myapp |
||
| 116 | * - available mappings: ['/srv/www'] |
||
| 117 | * |
||
| 118 | * First we check if a mapping for /srv/www/apps/myapp is available, |
||
| 119 | * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a |
||
| 120 | * valid web root |
||
| 121 | * |
||
| 122 | * @param string $root |
||
| 123 | * @return string|null The web root or null on failure |
||
| 124 | */ |
||
| 125 | protected function findWebRoot($root) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * append the $file resource at $root |
||
| 155 | * |
||
| 156 | * @param string $root path to check |
||
| 157 | * @param string $file the filename |
||
| 158 | * @param string|null $webRoot base for path, default map $root to $webRoot |
||
| 159 | * @param bool $throw Throw an exception, when the route does not exist |
||
| 160 | * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing |
||
| 161 | */ |
||
| 162 | protected function append($root, $file, $webRoot = null, $throw = true) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the list of all resources that should be loaded |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | public function getResources() { |
||
| 199 | } |
||
| 200 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: