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 |
||
33 | class CartService |
||
34 | { |
||
35 | /** |
||
36 | * @var Cart[] |
||
37 | */ |
||
38 | protected $carts; |
||
39 | |||
40 | /** |
||
41 | * @var SessionInterface |
||
42 | */ |
||
43 | protected $session; |
||
44 | |||
45 | /** |
||
46 | * @var \Doctrine\ORM\EntityManagerInterface |
||
47 | */ |
||
48 | protected $entityManager; |
||
49 | |||
50 | /** |
||
51 | * @var ItemHolderInterface |
||
52 | * |
||
53 | * @deprecated |
||
54 | */ |
||
55 | protected $cart; |
||
56 | |||
57 | /** |
||
58 | * @var ProductClassRepository |
||
59 | */ |
||
60 | protected $productClassRepository; |
||
61 | |||
62 | /** |
||
63 | * @var CartRepository |
||
64 | */ |
||
65 | protected $cartRepository; |
||
66 | |||
67 | /** |
||
68 | * @var CartItemComparator |
||
69 | */ |
||
70 | protected $cartItemComparator; |
||
71 | |||
72 | /** |
||
73 | * @var CartItemAllocator |
||
74 | */ |
||
75 | protected $cartItemAllocator; |
||
76 | |||
77 | /** |
||
78 | * @var OrderHelper |
||
79 | */ |
||
80 | protected $orderHelper; |
||
81 | |||
82 | /** |
||
83 | * @var OrderRepository |
||
84 | */ |
||
85 | protected $orderRepository; |
||
86 | |||
87 | /** |
||
88 | * @var TokenStorageInterface |
||
89 | */ |
||
90 | protected $tokenStorage; |
||
91 | |||
92 | /** |
||
93 | * @var AuthorizationCheckerInterface |
||
94 | */ |
||
95 | protected $authorizationChecker; |
||
96 | |||
97 | /** |
||
98 | * CartService constructor. |
||
99 | * |
||
100 | * @param SessionInterface $session |
||
101 | * @param EntityManagerInterface $entityManager |
||
102 | * @param ProductClassRepository $productClassRepository |
||
103 | * @param CartItemComparator $cartItemComparator |
||
104 | * @param CartItemAllocator $cartItemAllocator |
||
105 | * @param OrderHelper $orderHelper |
||
106 | * @param TokenStorageInterface $tokenStorage |
||
107 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
108 | */ |
||
109 | 173 | public function __construct( |
|
132 | |||
133 | 132 | public function getCarts() |
|
147 | |||
148 | /** |
||
149 | * 永続化されたカートを返す |
||
150 | * |
||
151 | * @return Cart[] |
||
152 | */ |
||
153 | public function getPersistedCarts() |
||
157 | |||
158 | /** |
||
159 | * セッションにあるカートを返す |
||
160 | * |
||
161 | * @return Cart[] |
||
162 | */ |
||
163 | public function getSessionCarts() |
||
169 | 65 | ||
170 | /** |
||
171 | 65 | * 会員が保持する永続化されたカートと、非会員時のカートをマージする. |
|
172 | 4 | * |
|
173 | * @param Customer $Customer |
||
174 | */ |
||
175 | 63 | public function mergeFromPersistedCart(Customer $Customer) |
|
189 | 39 | ||
190 | /** |
||
191 | * @return ItemHolderInterface|Cart |
||
192 | 87 | */ |
|
193 | public function getCart() |
||
203 | 87 | ||
204 | 86 | /** |
|
205 | 86 | * @param CartItem[] $cartItems |
|
206 | * |
||
207 | 38 | * @return CartItem[] |
|
208 | 36 | */ |
|
209 | 36 | protected function mergeAllCartItems($cartItems = []) |
|
220 | |||
221 | 87 | /** |
|
222 | * @param $cartItems |
||
223 | 87 | * @param $allCartItems |
|
224 | 2 | * |
|
225 | 1 | * @return array |
|
226 | 1 | */ |
|
227 | 1 | protected function mergeCartItems($cartItems, $allCartItems) |
|
246 | |||
247 | protected function restoreCarts($cartItems) |
||
293 | 86 | ||
294 | /** |
||
295 | * カートに商品を追加します. |
||
296 | * |
||
297 | 86 | * @param $ProductClass ProductClass 商品規格 |
|
298 | 86 | * @param $quantity int 数量 |
|
299 | 86 | * |
|
300 | 86 | * @return bool 商品を追加できた場合はtrue |
|
301 | */ |
||
302 | 86 | public function addProduct($ProductClass, $quantity = 1) |
|
333 | 2 | ||
334 | 2 | public function removeProduct($ProductClass) |
|
364 | |||
365 | 52 | public function save() |
|
383 | 10 | ||
384 | 10 | /** |
|
385 | 8 | * @param string $pre_order_id |
|
386 | 8 | * |
|
387 | 7 | * @return \Eccube\Service\CartService |
|
388 | 7 | */ |
|
389 | public function setPreOrderId($pre_order_id) |
||
395 | 7 | ||
396 | /** |
||
397 | * @return string |
||
398 | */ |
||
399 | 10 | public function getPreOrderId() |
|
403 | |||
404 | /** |
||
405 | 1 | * @return \Eccube\Service\CartService |
|
406 | */ |
||
407 | 1 | public function clear() |
|
427 | |||
428 | 86 | /** |
|
429 | 10 | * @param CartItemComparator $cartItemComparator |
|
430 | */ |
||
431 | public function setCartItemComparator($cartItemComparator) |
||
435 | |||
436 | /** |
||
437 | 51 | * 指定したインデックスにあるカートを優先にする |
|
438 | * |
||
439 | * @param int $index カートのインデックス |
||
440 | */ |
||
441 | public function setPrimary($index = 0) |
||
451 | |||
452 | protected function getUser() |
||
465 | |||
466 | /** |
||
467 | * @param string $allocatedId |
||
468 | */ |
||
469 | protected function createCartKey($allocatedId, Customer $Customer = null) |
||
489 | } |
||
490 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.