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:
Complex classes like CartService 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 CartService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class CartService |
||
43 | { |
||
44 | /** |
||
45 | * @var Session |
||
46 | * @Inject("session") |
||
47 | */ |
||
48 | protected $session; |
||
49 | |||
50 | /** |
||
51 | * @var EntityManager |
||
52 | * @Inject("orm.em") |
||
53 | */ |
||
54 | protected $em; |
||
55 | |||
56 | /** |
||
57 | * @var ItemHolderInterface |
||
58 | * @deprecated |
||
59 | */ |
||
60 | protected $cart; |
||
61 | 86 | ||
62 | /** |
||
63 | 86 | * @var ProductClassRepository |
|
64 | 86 | * @Inject(ProductClassRepository::class) |
|
65 | 86 | */ |
|
66 | protected $productClassRepository; |
||
67 | |||
68 | 86 | /** |
|
69 | * @var CartItemComparator |
||
70 | * @Inject(CartItemComparator::class) |
||
71 | 86 | */ |
|
72 | protected $cartItemComparator; |
||
73 | |||
74 | 86 | /** |
|
75 | * @var CartItemAllocator |
||
76 | * @Inject(CartItemAllocator::class) |
||
77 | */ |
||
78 | protected $cartItemAllocator; |
||
79 | |||
80 | /** |
||
81 | * @var Cart[] |
||
82 | 34 | */ |
|
83 | protected $carts; |
||
84 | 34 | ||
85 | 19 | public function getCarts() |
|
93 | 34 | ||
94 | 34 | /** |
|
95 | * @return ItemHolderInterface|Cart |
||
96 | */ |
||
97 | 34 | public function getCart() |
|
108 | 34 | ||
109 | 34 | protected function loadItems() |
|
120 | 2 | ||
121 | /** |
||
122 | 2 | * @param CartItem[] $cartItems |
|
123 | 1 | * @return CartItem[] |
|
124 | 1 | */ |
|
125 | 1 | protected function mergeAllCartItems($cartItems = []) |
|
136 | 2 | ||
137 | /** |
||
138 | * @param $cartItems |
||
139 | 31 | * @param $allCartItems |
|
140 | * @return array |
||
141 | 31 | */ |
|
142 | protected function mergeCartitems($cartItems, $allCartItems) |
||
160 | |||
161 | 22 | protected function restoreCarts($cartItems) |
|
181 | |||
182 | 12 | /** |
|
183 | * カートに商品を追加します. |
||
184 | * @param $ProductClass ProductClass 商品規格 |
||
185 | * @param $quantity int 数量 |
||
186 | * @return bool 商品を追加できた場合はtrue |
||
187 | */ |
||
188 | 22 | public function addProduct($ProductClass, $quantity = 1) |
|
220 | |||
221 | public function removeProduct($ProductClass) |
||
250 | |||
251 | public function save() |
||
255 | |||
256 | public function unlock() |
||
262 | |||
263 | public function lock() |
||
269 | |||
270 | /** |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function isLocked() |
||
277 | |||
278 | /** |
||
279 | * @param string $pre_order_id |
||
280 | * @return \Eccube\Service\CartService |
||
281 | */ |
||
282 | public function setPreOrderId($pre_order_id) |
||
288 | |||
289 | /** |
||
290 | * @return string |
||
291 | */ |
||
292 | public function getPreOrderId() |
||
296 | |||
297 | /** |
||
298 | * @return \Eccube\Service\CartService |
||
299 | */ |
||
300 | public function clear() |
||
315 | |||
316 | /** |
||
317 | * @param CartItemComparator $cartItemComparator |
||
318 | */ |
||
319 | public function setCartItemComparator($cartItemComparator) |
||
323 | |||
324 | /** |
||
325 | * 指定したインデックスにあるカートを優先にする |
||
326 | * @param int $index カートのインデックス |
||
327 | */ |
||
328 | public function setPrimary($index = 0) |
||
338 | } |
||
339 |