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 |
||
| 24 | class ShoppingCart extends \yz\shoppingcart\ShoppingCart |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var AbstractCartPosition[] |
||
| 28 | * TODO make local AbstractCartPosition |
||
| 29 | */ |
||
| 30 | protected $_positions = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The cart module. |
||
| 34 | */ |
||
| 35 | public $module; |
||
| 36 | |||
| 37 | public function behaviors() |
||
| 45 | |||
| 46 | 1 | /** |
|
| 47 | 1 | * @return integer |
|
| 48 | 1 | */ |
|
| 49 | public function getCount(): int |
||
| 60 | |||
| 61 | 2 | public function findRelatedFor(CartPositionInterface $parent): ?CartPositionInterface |
|
| 71 | 1 | ||
| 72 | /** |
||
| 73 | * @return CartPositionInterface[] |
||
| 74 | */ |
||
| 75 | public function getRootPositions(): array |
||
| 81 | |||
| 82 | public function getQuantity() |
||
| 91 | |||
| 92 | public function getSubtotal() |
||
| 96 | |||
| 97 | public function getTotal() |
||
| 101 | |||
| 102 | public function getDiscount() |
||
| 106 | |||
| 107 | public function formatCurrency($sum, $currency = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Sets cart from serialized string |
||
| 114 | * @param string $serialized |
||
| 115 | */ |
||
| 116 | public function setSerialized($serialized) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Checks whether any of cart positions has error in `id` attribute. |
||
| 129 | * @return boolean |
||
| 130 | */ |
||
| 131 | public function hasErrors() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param CartPositionInterface[] $positions |
||
| 144 | */ |
||
| 145 | public function putPositions($positions) |
||
| 168 | |||
| 169 | public function getCurrency(): ?string |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function getAdditionalLinks(): array |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @var CartActionEvent[]|null |
||
| 206 | */ |
||
| 207 | private $_accumulatedEvents; |
||
| 208 | public function trigger($name, Event $event = null) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Runs $closure and accumulates all events occurred during $closure run. |
||
| 220 | * Events get released immediately after a success $closure run. |
||
| 221 | * |
||
| 222 | * The method can be used to prevent useless calculations that happen after |
||
| 223 | * bunch of similar updates on a cart. |
||
| 224 | * |
||
| 225 | * @param \Closure $closure |
||
| 226 | */ |
||
| 227 | public function accumulateEvents(\Closure $closure): void |
||
| 242 | } |
||
| 243 |
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.