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 AbstractCartPosition[] |
||
27 | * TODO make local AbstractCartPosition |
||
28 | */ |
||
29 | protected $_positions = []; |
||
30 | |||
31 | /** |
||
32 | * The cart module. |
||
33 | */ |
||
34 | public $module; |
||
35 | |||
36 | /** |
||
37 | * @return integer |
||
38 | */ |
||
39 | 1 | public function getCount(): int |
|
50 | |||
51 | 1 | public function findRelatedFor(CartPositionInterface $parent): ?CartPositionInterface |
|
61 | 2 | ||
62 | /** |
||
63 | * @return CartPositionInterface[] |
||
64 | 1 | */ |
|
65 | public function getRootPositions(): array |
||
71 | 1 | ||
72 | public function getQuantity() |
||
81 | |||
82 | public function getSubtotal() |
||
86 | |||
87 | public function getTotal() |
||
91 | |||
92 | public function getDiscount() |
||
96 | |||
97 | public function formatCurrency($sum, $currency = null) |
||
101 | |||
102 | /** |
||
103 | * Sets cart from serialized string |
||
104 | * @param string $serialized |
||
105 | */ |
||
106 | public function setSerialized($serialized) |
||
116 | |||
117 | /** |
||
118 | * Checks whether any of cart positions has error in `id` attribute. |
||
119 | * @return boolean |
||
120 | */ |
||
121 | public function hasErrors() |
||
131 | |||
132 | /** |
||
133 | * @param CartPositionInterface[] $positions |
||
134 | */ |
||
135 | public function putPositions($positions) |
||
158 | |||
159 | public function getCurrency(): ?string |
||
167 | |||
168 | /** |
||
169 | * @return array |
||
170 | */ |
||
171 | public function getAdditionalLinks(): array |
||
193 | |||
194 | /** |
||
195 | * @var CartActionEvent[]|null |
||
196 | */ |
||
197 | private $_accumulatedEvents; |
||
198 | public function trigger($name, Event $event = null) |
||
207 | |||
208 | /** |
||
209 | * Runs $closure and accumulates all events occurred during $closure run. |
||
210 | * Events get released immediately after a success $closure run. |
||
211 | * |
||
212 | * The method can be used to prevent useless calculations that happen after |
||
213 | * bunch of similar updates on a cart. |
||
214 | * |
||
215 | * @param \Closure $closure |
||
216 | */ |
||
217 | public function accumulateEvents(\Closure $closure): void |
||
232 | } |
||
233 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.