1 | <?php |
||
24 | class Basket |
||
25 | { |
||
26 | protected $id; |
||
27 | |||
28 | protected $identifier; |
||
29 | protected $store; |
||
30 | |||
31 | protected $currency; |
||
32 | |||
33 | protected $requiredParams = [ |
||
34 | 'id', |
||
35 | 'name', |
||
36 | 'quantity', |
||
37 | 'price', |
||
38 | 'weight', |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Basket constructor. |
||
43 | * |
||
44 | * @param StorageInterface $store The interface for storing the cart data |
||
45 | * @param IdentifierInterface $identifier The interface for storing the identifier |
||
46 | */ |
||
47 | 14 | public function __construct(StorageInterface $store, IdentifierInterface $identifier) |
|
63 | |||
64 | /** |
||
65 | * Retrieve the basket contents. |
||
66 | * |
||
67 | * @param bool $asArray |
||
68 | * |
||
69 | * @return array An array of Item objects |
||
70 | */ |
||
71 | 9 | public function &contents($asArray = false) |
|
75 | |||
76 | /** |
||
77 | * Insert an item into the basket. |
||
78 | * |
||
79 | * @param array $item An array of item data |
||
80 | * |
||
81 | * @return string A unique item identifier |
||
82 | */ |
||
83 | 14 | public function insert(array $item) |
|
102 | |||
103 | /** |
||
104 | * Update an item. |
||
105 | * |
||
106 | * @param string $itemIdentifier The unique item identifier |
||
107 | * @param string|int|array $key The key to update, or an array of key-value pairs |
||
108 | * @param mixed $value The value to set $key to |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | 2 | public function update($itemIdentifier, $key, $value = null) |
|
121 | |||
122 | /** |
||
123 | * Remove an item from the basket. |
||
124 | * |
||
125 | * @param string $identifier Unique item identifier |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | 1 | public function remove($identifier) |
|
133 | |||
134 | /** |
||
135 | * Destroy/empty the basket. |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | 14 | public function destroy() |
|
143 | |||
144 | /** |
||
145 | * Check if the basket has a specific item. |
||
146 | * |
||
147 | * @param string $itemIdentifier The unique item identifier |
||
148 | * |
||
149 | * @return bool Yes or no? |
||
150 | */ |
||
151 | 14 | public function has($itemIdentifier) |
|
155 | |||
156 | /** |
||
157 | * Return a specific item object by identifier. |
||
158 | * |
||
159 | * @param string $itemIdentifier The unique item identifier |
||
160 | * |
||
161 | * @return Item Item object |
||
162 | */ |
||
163 | 6 | public function item($itemIdentifier) |
|
167 | |||
168 | /** |
||
169 | * Returns the first occurance of an item with a given id. |
||
170 | * |
||
171 | * @param string $id The item id |
||
172 | * |
||
173 | * @return Item Item object |
||
174 | */ |
||
175 | 1 | public function find($id) |
|
179 | |||
180 | /** |
||
181 | * The total tax value for the basket. |
||
182 | * |
||
183 | * @return float The total tax value |
||
184 | */ |
||
185 | public function tax() |
||
195 | |||
196 | /** |
||
197 | * The total weight value for the basket. |
||
198 | * |
||
199 | * @return float The total weight value |
||
200 | */ |
||
201 | public function weight() |
||
211 | |||
212 | /** |
||
213 | * The total value of the basket. |
||
214 | * |
||
215 | * @param bool $includeTax Include tax on the total? |
||
216 | * |
||
217 | * @return float The total basket value |
||
218 | */ |
||
219 | 3 | public function total($includeTax = true) |
|
229 | |||
230 | /** |
||
231 | * The total number of items in the basket. |
||
232 | * |
||
233 | * @param bool $unique Just return unique items? |
||
234 | * |
||
235 | * @return int Total number of items |
||
236 | */ |
||
237 | 1 | public function totalItems($unique = false) |
|
247 | |||
248 | /** |
||
249 | * Set the basket identifier, useful if restoring a saved basket. |
||
250 | * |
||
251 | * @param mixed The identifier |
||
252 | * |
||
253 | * @return void |
||
254 | */ |
||
255 | public function setIdentifier($identifier) |
||
259 | |||
260 | /** |
||
261 | * Create a unique item identifier. |
||
262 | * |
||
263 | * @param array $item An array of item data |
||
264 | * |
||
265 | * @return string An md5 hash of item |
||
266 | */ |
||
267 | 14 | protected function createItemIdentifier(array $item) |
|
277 | |||
278 | /** |
||
279 | * Check if a basket item has the required parameters. |
||
280 | * |
||
281 | * @param array $item An array of item data |
||
282 | * |
||
283 | * @return void |
||
284 | */ |
||
285 | 14 | protected function checkArgs(array $item) |
|
293 | } |
||
294 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: