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 |
||
| 15 | class WallpaperList implements \ArrayAccess, \IteratorAggregate, \Countable |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Wallpaper[] Wallpapers. |
||
| 20 | */ |
||
| 21 | private $wallpapers = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Download all wallpapers in list. |
||
| 25 | * |
||
| 26 | * @param string $directory Where to download wallpapers. |
||
| 27 | * |
||
| 28 | * @throws DownloadException Thrown if the download directory cannot be created. |
||
| 29 | */ |
||
| 30 | public function downloadAll($directory) |
||
| 31 | { |
||
| 32 | View Code Duplication | if (!file_exists($directory)) { |
|
|
|
|||
| 33 | if (!@mkdir($directory, null, true)) { |
||
| 34 | throw new DownloadException("The download directory cannot be created."); |
||
| 35 | } |
||
| 36 | } |
||
| 37 | |||
| 38 | $client = new Client(); |
||
| 39 | |||
| 40 | $requests = []; |
||
| 41 | foreach ($this->wallpapers as $w) { |
||
| 42 | $url = $w->getImageUrl(true); |
||
| 43 | |||
| 44 | $requests[] = $client->createRequest('GET', $url, [ |
||
| 45 | 'save_to' => $directory . '/' . basename($url) |
||
| 46 | ]); |
||
| 47 | } |
||
| 48 | |||
| 49 | $results = Pool::batch($client, $requests); |
||
| 50 | |||
| 51 | // Retry with PNG |
||
| 52 | $retryRequests = []; |
||
| 53 | foreach ($results->getFailures() as $e) { |
||
| 54 | // Delete failed files |
||
| 55 | unlink($directory . '/' . basename($e->getRequest()->getUrl())); |
||
| 56 | |||
| 57 | $urlPng = str_replace('.jpg', '.png', $e->getRequest()->getUrl()); |
||
| 58 | $statusCode = $e->getResponse()->getStatusCode(); |
||
| 59 | |||
| 60 | if ($statusCode == 404) { |
||
| 61 | $retryRequests[] = $client->createRequest('GET', $urlPng, [ |
||
| 62 | 'save_to' => $directory . '/' . basename($urlPng) |
||
| 63 | ]); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | Pool::batch($client, $retryRequests); |
||
| 68 | } |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * |
||
| 73 | * @param $offset |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | public function offsetExists($offset) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * |
||
| 84 | * @param $offset |
||
| 85 | * |
||
| 86 | * @return null|Wallpaper |
||
| 87 | */ |
||
| 88 | public function offsetGet($offset) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * |
||
| 95 | * @param $offset |
||
| 96 | * @param Wallpaper $value |
||
| 97 | * |
||
| 98 | * @throws WallhavenException |
||
| 99 | */ |
||
| 100 | public function offsetSet($offset, $value) |
||
| 101 | { |
||
| 102 | if (!$value instanceof Wallpaper) { |
||
| 103 | throw new WallhavenException("Not a Wallpaper object."); |
||
| 104 | } |
||
| 105 | |||
| 106 | if (is_null($offset)) { |
||
| 107 | $this->wallpapers[] = $value; |
||
| 108 | } else { |
||
| 109 | $this->wallpapers[$offset] = $value; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param $offset |
||
| 115 | */ |
||
| 116 | public function offsetUnset($offset) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return \ArrayIterator |
||
| 123 | */ |
||
| 124 | public function getIterator() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return int Wallpaper count. |
||
| 131 | */ |
||
| 132 | public function count() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * All all items from the given list to the current list. |
||
| 139 | * |
||
| 140 | * @param WallpaperList $wallpaperList |
||
| 141 | */ |
||
| 142 | public function addAll(WallpaperList $wallpaperList) |
||
| 148 | } |
||
| 149 |
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.