Complex classes like ShoppingCart 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 ShoppingCart, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ShoppingCart extends \yz\shoppingcart\ShoppingCart |
||
24 | { |
||
25 | /** |
||
26 | * @var CartPositionInterface[] |
||
27 | * TODO make local AbstractCartPosition |
||
28 | */ |
||
29 | protected $_positions = []; |
||
30 | |||
31 | /** |
||
32 | * The cart module. |
||
33 | */ |
||
34 | public $module; |
||
35 | |||
36 | public function behaviors() |
||
44 | 1 | ||
45 | /** |
||
46 | 1 | * @return integer |
|
47 | 1 | */ |
|
48 | 1 | public function getCount(): int |
|
59 | 2 | ||
60 | public function findRelatedFor(CartPositionInterface $parent): ?CartPositionInterface |
||
70 | |||
71 | 1 | /** |
|
72 | * @return CartPositionInterface[] |
||
73 | */ |
||
74 | public function getRootPositions(): array |
||
80 | |||
81 | public function getQuantity() |
||
90 | |||
91 | public function getSubtotal() |
||
95 | |||
96 | public function getTotal() |
||
100 | |||
101 | public function getDiscount() |
||
105 | |||
106 | public function formatCurrency($sum, $currency = null) |
||
110 | |||
111 | /** |
||
112 | * Sets cart from serialized string |
||
113 | * @param string $serialized |
||
114 | */ |
||
115 | public function setSerialized($serialized) |
||
125 | |||
126 | /** |
||
127 | * Checks whether any of cart positions has error in `id` attribute. |
||
128 | * @return boolean |
||
129 | */ |
||
130 | public function hasErrors() |
||
140 | |||
141 | /** |
||
142 | * @param CartPositionInterface[] $positions |
||
143 | */ |
||
144 | public function putPositions($positions) |
||
171 | |||
172 | |||
173 | /** |
||
174 | * This cart does not support multi-currency checkout. |
||
175 | * |
||
176 | * @return string|null When the cart is empty, returns {@see getDefaultCurrency()}. |
||
177 | * When cart has at least one position, returns the first position currency. |
||
178 | * Note, that the position currency may be undefined – in this case, `null` will be returned. |
||
179 | */ |
||
180 | public function getCurrency(): ?string |
||
188 | |||
189 | /** |
||
190 | * Returns a default cart currency |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | public function getDefaultCurrency(): string |
||
198 | |||
199 | /** |
||
200 | * @return array |
||
201 | */ |
||
202 | public function getAdditionalLinks(): array |
||
224 | |||
225 | /** |
||
226 | * @var CartActionEvent[]|null |
||
227 | */ |
||
228 | private $_accumulatedEvents; |
||
229 | public function trigger($name, Event $event = null) |
||
238 | |||
239 | /** |
||
240 | * Runs $closure and accumulates all events occurred during $closure run. |
||
241 | * Events get released immediately after a success $closure run. |
||
242 | * |
||
243 | * The method can be used to prevent useless calculations that happen after |
||
244 | * bunch of similar updates on a cart. |
||
245 | * |
||
246 | * @param \Closure $closure |
||
247 | */ |
||
248 | public function accumulateEvents(\Closure $closure): void |
||
263 | } |
||
264 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: