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 Wallpaper 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 Wallpaper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Wallpaper |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var Client |
||
| 23 | */ |
||
| 24 | private $client; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var bool Cache enabled. |
||
| 28 | */ |
||
| 29 | private $cacheEnabled; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Dom Cached DOM. |
||
| 33 | */ |
||
| 34 | private $dom; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int Wallpaper ID. |
||
| 38 | */ |
||
| 39 | private $id; |
||
| 40 | |||
| 41 | private $tags; |
||
| 42 | private $purity; |
||
| 43 | private $resolution; |
||
| 44 | private $size; |
||
| 45 | private $category; |
||
| 46 | private $views; |
||
| 47 | private $favorites; |
||
| 48 | private $featuredBy; |
||
| 49 | private $featuredDate; |
||
| 50 | private $uploadedBy; |
||
| 51 | private $uploadedDate; |
||
| 52 | |||
| 53 | private $imageUrl; |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * @param int $id Wallpaper's ID. |
||
| 58 | * @param Client $client HTTP Client. |
||
| 59 | */ |
||
| 60 | public function __construct($id, Client $client) |
||
| 61 | { |
||
| 62 | $this->id = $id; |
||
| 63 | $this->client = $client; |
||
| 64 | $this->cacheEnabled = true; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return int Wallpaper ID. |
||
| 69 | */ |
||
| 70 | public function getId() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param array $properties Properties. |
||
| 77 | */ |
||
| 78 | public function setProperties(array $properties) |
||
| 79 | { |
||
| 80 | foreach ($properties as $key => $value) { |
||
| 81 | $this->$key = $value; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Enable or disable caching of wallpaper information. It's recommended to leave this enabled (default) unless |
||
| 87 | * you really need real-time information. If you disable caching, performance will be severely degraded. |
||
| 88 | * |
||
| 89 | * @param bool $enabled Whether caching should be enabled. |
||
| 90 | */ |
||
| 91 | public function setCacheEnabled($enabled) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get wallpaper tags. |
||
| 98 | * |
||
| 99 | * @return string[] Tags. |
||
| 100 | */ |
||
| 101 | public function getTags() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return Dom |
||
| 120 | * @throws LoginException Thrown if access to the wallpaper was denied. |
||
| 121 | * @throws NotFoundException Thrown if the wallpaper was not found. |
||
| 122 | */ |
||
| 123 | private function getDom() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return int Purity. |
||
| 152 | */ |
||
| 153 | public function getPurity() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return string Resolution. |
||
| 174 | */ |
||
| 175 | public function getResolution() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $contents |
||
| 187 | * |
||
| 188 | * @return \PHPHtmlParser\Dom\AbstractNode |
||
| 189 | * @throws ParseException |
||
| 190 | */ |
||
| 191 | private function getSibling($contents) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return string Size of the image. |
||
| 208 | */ |
||
| 209 | public function getSize() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return int Category. |
||
| 220 | */ |
||
| 221 | public function getCategory() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return int Number of views. |
||
| 232 | */ |
||
| 233 | public function getViews() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return int Number of favorites. |
||
| 244 | */ |
||
| 245 | public function getFavorites() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return User User that featured the wallpaper. |
||
| 262 | * @throws ParseException |
||
| 263 | */ |
||
| 264 | View Code Duplication | public function getFeaturedBy() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @return DateTime Date and time when the wallpaper was featured. |
||
| 281 | * @throws ParseException |
||
| 282 | */ |
||
| 283 | View Code Duplication | public function getFeaturedDate() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @return User User that uploaded the wallpaper. |
||
| 300 | */ |
||
| 301 | public function getUploadedBy() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return DateTime Date and time when the wallpaper was uploaded. |
||
| 317 | */ |
||
| 318 | public function getUploadedDate() |
||
| 328 | |||
| 329 | public function __toString() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return string Thumbnail URL. |
||
| 336 | */ |
||
| 337 | public function getThumbnailUrl() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Download the wallpaper. |
||
| 344 | * |
||
| 345 | * @param string $directory Where to download the wallpaper. |
||
| 346 | * |
||
| 347 | * @throws DownloadException Thrown if the download directory cannot be created. |
||
| 348 | */ |
||
| 349 | public function download($directory) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param bool $assumeJpg Assume the wallpaper is JPG. May speed up the method at the cost of potentially wrong URL. |
||
| 366 | * |
||
| 367 | * @return string URL. |
||
| 368 | * @throws LoginException |
||
| 369 | * @throws NotFoundException |
||
| 370 | */ |
||
| 371 | public function getImageUrl($assumeJpg = false) |
||
| 385 | } |
||
| 386 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.