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 |
||
| 33 | class ThemingDefaults extends \OC_Defaults { |
||
| 34 | |||
| 35 | /** @var IConfig */ |
||
| 36 | private $config; |
||
| 37 | /** @var IL10N */ |
||
| 38 | private $l; |
||
| 39 | /** @var IURLGenerator */ |
||
| 40 | private $urlGenerator; |
||
| 41 | /** @var IAppData */ |
||
| 42 | private $appData; |
||
| 43 | /** @var ICacheFactory */ |
||
| 44 | private $cacheFactory; |
||
| 45 | /** @var string */ |
||
| 46 | private $name; |
||
| 47 | /** @var string */ |
||
| 48 | private $url; |
||
| 49 | /** @var string */ |
||
| 50 | private $slogan; |
||
| 51 | /** @var string */ |
||
| 52 | private $color; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * ThemingDefaults constructor. |
||
| 56 | * |
||
| 57 | * @param IConfig $config |
||
| 58 | * @param IL10N $l |
||
| 59 | * @param IURLGenerator $urlGenerator |
||
| 60 | * @param \OC_Defaults $defaults |
||
| 61 | * @param IAppData $appData |
||
| 62 | * @param ICacheFactory $cacheFactory |
||
| 63 | */ |
||
| 64 | public function __construct(IConfig $config, |
||
| 83 | |||
| 84 | public function getName() { |
||
| 87 | |||
| 88 | public function getHTMLName() { |
||
| 91 | |||
| 92 | public function getTitle() { |
||
| 95 | |||
| 96 | public function getEntity() { |
||
| 99 | |||
| 100 | public function getBaseUrl() { |
||
| 103 | |||
| 104 | public function getSlogan() { |
||
| 107 | |||
| 108 | public function getShortFooter() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Color that is used for the header as well as for mail headers |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public function getColorPrimary() { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Themed logo url |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function getLogo() { |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Themed background image url |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | View Code Duplication | public function getBackground() { |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Check if Imagemagick is enabled and if SVG is supported |
||
| 172 | * otherwise we can't render custom icons |
||
| 173 | * |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function shouldReplaceIcons() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Gets the current cache buster count |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function getCacheBusterCounter() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Increases the cache buster key |
||
| 204 | */ |
||
| 205 | private function increaseCacheBuster() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Update setting in the database |
||
| 212 | * |
||
| 213 | * @param string $setting |
||
| 214 | * @param string $value |
||
| 215 | */ |
||
| 216 | public function set($setting, $value) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Revert settings to the default value |
||
| 223 | * |
||
| 224 | * @param string $setting setting which should be reverted |
||
| 225 | * @return string default value |
||
| 226 | */ |
||
| 227 | public function undo($setting) { |
||
| 251 | |||
| 252 | } |
||
| 253 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: