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:
Complex classes like URLGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use URLGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class URLGenerator implements IURLGenerator { |
||
| 49 | /** @var IConfig */ |
||
| 50 | private $config; |
||
| 51 | /** @var ICacheFactory */ |
||
| 52 | private $cacheFactory; |
||
| 53 | /** @var IRequest */ |
||
| 54 | private $request; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param IConfig $config |
||
| 58 | * @param ICacheFactory $cacheFactory |
||
| 59 | * @param IRequest $request |
||
| 60 | */ |
||
| 61 | public function __construct(IConfig $config, |
||
| 62 | ICacheFactory $cacheFactory, |
||
| 63 | IRequest $request) { |
||
| 64 | $this->config = $config; |
||
| 65 | $this->cacheFactory = $cacheFactory; |
||
| 66 | $this->request = $request; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Creates an url using a defined route |
||
| 71 | * @param string $route |
||
| 72 | * @param array $parameters args with param=>value, will be appended to the returned url |
||
| 73 | * @return string the url |
||
| 74 | * |
||
| 75 | * Returns a url to the given route. |
||
| 76 | */ |
||
| 77 | public function linkToRoute(string $route, array $parameters = array()): string { |
||
| 78 | // TODO: mock router |
||
| 79 | return \OC::$server->getRouter()->generate($route, $parameters); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Creates an absolute url using a defined route |
||
| 84 | * @param string $routeName |
||
| 85 | * @param array $arguments args with param=>value, will be appended to the returned url |
||
| 86 | * @return string the url |
||
| 87 | * |
||
| 88 | * Returns an absolute url to the given route. |
||
| 89 | */ |
||
| 90 | public function linkToRouteAbsolute(string $routeName, array $arguments = array()): string { |
||
| 91 | return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments)); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Creates an url |
||
| 96 | * @param string $app app |
||
| 97 | * @param string $file file |
||
| 98 | * @param array $args array with param=>value, will be appended to the returned url |
||
| 99 | * The value of $args will be urlencoded |
||
| 100 | * @return string the url |
||
| 101 | * |
||
| 102 | * Returns a url to the given app and file. |
||
| 103 | */ |
||
| 104 | public function linkTo(string $app, string $file, array $args = array()): string { |
||
| 105 | $frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
||
| 106 | |||
| 107 | if( $app !== '' ) { |
||
| 108 | $app_path = \OC_App::getAppPath($app); |
||
| 109 | // Check if the app is in the app folder |
||
| 110 | if ($app_path && file_exists($app_path . '/' . $file)) { |
||
|
|
|||
| 111 | if (substr($file, -3) === 'php') { |
||
| 112 | |||
| 113 | $urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $app; |
||
| 114 | if ($frontControllerActive) { |
||
| 115 | $urlLinkTo = \OC::$WEBROOT . '/apps/' . $app; |
||
| 116 | } |
||
| 117 | $urlLinkTo .= ($file !== 'index.php') ? '/' . $file : ''; |
||
| 118 | } else { |
||
| 119 | $urlLinkTo = \OC_App::getAppWebPath($app) . '/' . $file; |
||
| 120 | } |
||
| 121 | } else { |
||
| 122 | $urlLinkTo = \OC::$WEBROOT . '/' . $app . '/' . $file; |
||
| 123 | } |
||
| 124 | } else { |
||
| 125 | if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) { |
||
| 126 | $urlLinkTo = \OC::$WEBROOT . '/core/' . $file; |
||
| 127 | } else { |
||
| 128 | if ($frontControllerActive && $file === 'index.php') { |
||
| 129 | $urlLinkTo = \OC::$WEBROOT . '/'; |
||
| 130 | } else { |
||
| 131 | $urlLinkTo = \OC::$WEBROOT . '/' . $file; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | if ($args && $query = http_build_query($args, '', '&')) { |
||
| 137 | $urlLinkTo .= '?' . $query; |
||
| 138 | } |
||
| 139 | |||
| 140 | return $urlLinkTo; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Creates path to an image |
||
| 145 | * @param string $app app |
||
| 146 | * @param string $image image name |
||
| 147 | * @throws \RuntimeException If the image does not exist |
||
| 148 | * @return string the url |
||
| 149 | * |
||
| 150 | * Returns the path to the image. |
||
| 151 | */ |
||
| 152 | public function imagePath(string $app, string $image): string { |
||
| 153 | $cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-'); |
||
| 154 | $cacheKey = $app.'-'.$image; |
||
| 155 | if($key = $cache->get($cacheKey)) { |
||
| 156 | return $key; |
||
| 157 | } |
||
| 158 | |||
| 159 | // Read the selected theme from the config file |
||
| 160 | $theme = \OC_Util::getTheme(); |
||
| 161 | |||
| 162 | //if a theme has a png but not an svg always use the png |
||
| 163 | $basename = substr(basename($image),0,-4); |
||
| 164 | |||
| 165 | $appPath = \OC_App::getAppPath($app); |
||
| 166 | |||
| 167 | // Check if the app is in the app folder |
||
| 168 | $path = ''; |
||
| 169 | $themingEnabled = $this->config->getSystemValue('installed', false) && \OCP\App::isEnabled('theming') && \OC_App::isAppLoaded('theming'); |
||
| 170 | $themingImagePath = false; |
||
| 171 | if($themingEnabled) { |
||
| 172 | $themingDefaults = \OC::$server->getThemingDefaults(); |
||
| 173 | if ($themingDefaults instanceof ThemingDefaults) { |
||
| 174 | $themingImagePath = $themingDefaults->replaceImagePath($app, $image); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) { |
||
| 179 | $path = \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image"; |
||
| 180 | } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.svg") |
||
| 181 | && file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.png")) { |
||
| 182 | $path = \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$basename.png"; |
||
| 183 | } elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$image")) { |
||
| 184 | $path = \OC::$WEBROOT . "/themes/$theme/$app/img/$image"; |
||
| 185 | View Code Duplication | } elseif (!empty($app) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$basename.svg") |
|
| 186 | && file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$basename.png"))) { |
||
| 187 | $path = \OC::$WEBROOT . "/themes/$theme/$app/img/$basename.png"; |
||
| 188 | } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$image")) { |
||
| 189 | $path = \OC::$WEBROOT . "/themes/$theme/core/img/$image"; |
||
| 190 | View Code Duplication | } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg") |
|
| 191 | && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) { |
||
| 192 | $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png"; |
||
| 193 | } elseif($themingEnabled && $themingImagePath) { |
||
| 194 | $path = $themingImagePath; |
||
| 195 | } elseif ($appPath && file_exists($appPath . "/img/$image")) { |
||
| 196 | $path = \OC_App::getAppWebPath($app) . "/img/$image"; |
||
| 197 | } elseif ($appPath && !file_exists($appPath . "/img/$basename.svg") |
||
| 198 | && file_exists($appPath . "/img/$basename.png")) { |
||
| 199 | $path = \OC_App::getAppWebPath($app) . "/img/$basename.png"; |
||
| 200 | } elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/$app/img/$image")) { |
||
| 201 | $path = \OC::$WEBROOT . "/$app/img/$image"; |
||
| 202 | View Code Duplication | } elseif (!empty($app) and (!file_exists(\OC::$SERVERROOT . "/$app/img/$basename.svg") |
|
| 203 | && file_exists(\OC::$SERVERROOT . "/$app/img/$basename.png"))) { |
||
| 204 | $path = \OC::$WEBROOT . "/$app/img/$basename.png"; |
||
| 205 | } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$image")) { |
||
| 206 | $path = \OC::$WEBROOT . "/core/img/$image"; |
||
| 207 | View Code Duplication | } elseif (!file_exists(\OC::$SERVERROOT . "/core/img/$basename.svg") |
|
| 208 | && file_exists(\OC::$SERVERROOT . "/core/img/$basename.png")) { |
||
| 209 | $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png"; |
||
| 210 | } |
||
| 211 | |||
| 212 | if($path !== '') { |
||
| 213 | $cache->set($cacheKey, $path); |
||
| 214 | return $path; |
||
| 215 | } |
||
| 216 | |||
| 217 | throw new RuntimeException('image not found: image:' . $image . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT); |
||
| 218 | } |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Makes an URL absolute |
||
| 223 | * @param string $url the url in the ownCloud host |
||
| 224 | * @return string the absolute version of the url |
||
| 225 | */ |
||
| 226 | public function getAbsoluteURL(string $url): string { |
||
| 227 | $separator = $url[0] === '/' ? '' : '/'; |
||
| 228 | |||
| 229 | if (\OC::$CLI && !\defined('PHPUNIT_RUN')) { |
||
| 230 | return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/'); |
||
| 231 | } |
||
| 232 | // The ownCloud web root can already be prepended. |
||
| 233 | if(substr($url, 0, \strlen(\OC::$WEBROOT)) === \OC::$WEBROOT) { |
||
| 234 | $url = substr($url, \strlen(\OC::$WEBROOT)); |
||
| 235 | } |
||
| 236 | |||
| 237 | return $this->getBaseUrl() . $separator . $url; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $key |
||
| 242 | * @return string url to the online documentation |
||
| 243 | */ |
||
| 244 | public function linkToDocs(string $key): string { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return string base url of the current request |
||
| 251 | */ |
||
| 252 | public function getBaseUrl(): string { |
||
| 253 | return $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT; |
||
| 254 | } |
||
| 255 | } |
||
| 256 |
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: