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 |
||
| 30 | class Runtime implements StorageInterface |
||
| 31 | { |
||
| 32 | /** @var string */ |
||
| 33 | protected string $identifier; |
||
|
|
|||
| 34 | |||
| 35 | /** @var array */ |
||
| 36 | protected static array $cart = []; |
||
| 37 | |||
| 38 | /** @var string */ |
||
| 39 | protected string $id = 'basket'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Add or update an item in the cart. |
||
| 43 | * |
||
| 44 | * @param ItemInterface $item The item to insert or update |
||
| 45 | */ |
||
| 46 | 18 | public function insertUpdate(ItemInterface $item) |
|
| 47 | { |
||
| 48 | 18 | static::$cart[$this->id][$item->identifier] = $item; |
|
| 49 | 18 | } |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Retrieve the cart data. |
||
| 53 | * |
||
| 54 | * @param bool $asArray |
||
| 55 | * |
||
| 56 | * @return array |
||
| 57 | */ |
||
| 58 | 14 | public function &data($asArray = false): array |
|
| 59 | { |
||
| 60 | 14 | $cart = &static::$cart[$this->id]; |
|
| 61 | |||
| 62 | 14 | if (! $asArray) { |
|
| 63 | 13 | return $cart; |
|
| 64 | } |
||
| 65 | |||
| 66 | 1 | $data = $cart; |
|
| 67 | |||
| 68 | 1 | foreach ($data as &$item) { |
|
| 69 | 1 | $item = $item->toArray(); |
|
| 70 | } |
||
| 71 | |||
| 72 | 1 | return $data; |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Check if the item exists in the cart. |
||
| 77 | * |
||
| 78 | * @param string $identifier |
||
| 79 | * |
||
| 80 | * @return bool |
||
| 81 | * |
||
| 82 | * @internal param mixed $id |
||
| 83 | */ |
||
| 84 | 18 | public function has(string $identifier): bool |
|
| 85 | { |
||
| 86 | 18 | foreach (static::$cart[$this->id] as $item) { |
|
| 87 | 2 | if ($item->identifier == $identifier) { |
|
| 88 | 1 | return true; |
|
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | 18 | return false; |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get a single cart item by id. |
||
| 97 | * |
||
| 98 | * @param string $identifier |
||
| 99 | * |
||
| 100 | * @return bool|Item |
||
| 101 | * |
||
| 102 | * @internal param mixed $id The item id |
||
| 103 | */ |
||
| 104 | 5 | public function item(string $identifier) |
|
| 105 | { |
||
| 106 | 5 | foreach (static::$cart[$this->id] as $item) { |
|
| 107 | 5 | if ($item->identifier == $identifier) { |
|
| 108 | 5 | return $item; |
|
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | return false; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns the first occurance of an item with a given id. |
||
| 117 | * |
||
| 118 | * @param string $id The item id |
||
| 119 | * |
||
| 120 | * @return bool|Item |
||
| 121 | */ |
||
| 122 | 1 | public function find(string $id) |
|
| 123 | { |
||
| 124 | 1 | foreach (static::$cart[$this->id] as $item) { |
|
| 125 | 1 | if ($item->id == $id) { |
|
| 126 | 1 | return $item; |
|
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Remove an item from the cart. |
||
| 135 | * |
||
| 136 | * @param string $id |
||
| 137 | */ |
||
| 138 | 1 | public function remove(string $id) |
|
| 139 | { |
||
| 140 | 1 | unset(static::$cart[$this->id][$id]); |
|
| 141 | 1 | } |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Destroy the cart. |
||
| 145 | */ |
||
| 146 | 18 | public function destroy() |
|
| 147 | { |
||
| 148 | 18 | static::$cart[$this->id] = []; |
|
| 149 | 18 | } |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Set the cart identifier. |
||
| 153 | * |
||
| 154 | * @param string $identifier |
||
| 155 | * |
||
| 156 | * @internal param string $identifier |
||
| 157 | */ |
||
| 158 | 18 | public function setIdentifier(string $identifier) |
|
| 159 | { |
||
| 160 | 18 | $this->id = $identifier; |
|
| 161 | |||
| 162 | 18 | if (! array_key_exists($this->id, static::$cart)) { |
|
| 163 | 1 | static::$cart[$this->id] = []; |
|
| 164 | } |
||
| 165 | 18 | } |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Return the current cart identifier. |
||
| 169 | * |
||
| 170 | * @return string The identifier |
||
| 171 | */ |
||
| 172 | public function getIdentifier(): string |
||
| 173 | { |
||
| 174 | return $this->id; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Restore the cart. |
||
| 179 | */ |
||
| 180 | 18 | public function restore() |
|
| 181 | { |
||
| 182 | 18 | if (isset($_SESSION['cart'])) { |
|
| 183 | static::$cart = unserialize($_SESSION['cart']); |
||
| 184 | } |
||
| 185 | 18 | } |
|
| 186 | } |
||
| 187 |