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 |
||
| 14 | class Collection implements CollectionInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Collections's identifier. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $id = ''; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Collection's items. |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $items = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * AbstractCollection constructor. |
||
| 32 | * |
||
| 33 | * @param string|null $id |
||
| 34 | * @param array $items |
||
| 35 | */ |
||
| 36 | public function __construct($id = null, $items = []) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * If parameter is empty uses the object hash |
||
| 44 | * {@inheritdoc} |
||
| 45 | */ |
||
| 46 | public function setId($id = '') |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | public function getId() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | public function has($id) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function add(ItemInterface $item) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function replace($id, ItemInterface $item) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | public function remove($id) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | public function get($id) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function keys() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | public function count() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | public function toArray() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | public function getIterator() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | public function usort(\Closure $callback = null) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Sort items by date. |
||
| 182 | * |
||
| 183 | * @return Collection |
||
| 184 | */ |
||
| 185 | public function sortByDate() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | * |
||
| 205 | * @return Collection |
||
| 206 | */ |
||
| 207 | public function filter(\Closure $callback) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | public function map(\Closure $callback) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Implement ArrayAccess. |
||
| 222 | * |
||
| 223 | * @param mixed $offset |
||
| 224 | * |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | public function offsetExists($offset) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Implement ArrayAccess. |
||
| 234 | * |
||
| 235 | * @param mixed $offset |
||
| 236 | * |
||
| 237 | * @return null|CollectionInterface |
||
| 238 | */ |
||
| 239 | public function offsetGet($offset) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Implement ArrayAccess. |
||
| 246 | * |
||
| 247 | * @param mixed $offset |
||
| 248 | * @param mixed $value |
||
| 249 | */ |
||
| 250 | public function offsetSet($offset, $value) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Implement ArrayAccess. |
||
| 257 | * |
||
| 258 | * @param mixed $offset |
||
| 259 | */ |
||
| 260 | public function offsetUnset($offset) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Returns a string representation of this object. |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function __toString() |
||
| 274 | } |
||
| 275 |