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 | public function __construct( |
||
132 | |||
133 | 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() |
||
168 | |||
169 | /** |
||
170 | * 会員が保持する永続化されたカートと、非会員時のカートをマージする. |
||
171 | * |
||
172 | * @param Customer $Customer |
||
173 | */ |
||
174 | public function mergeFromPersistedCart(Customer $Customer) |
||
188 | |||
189 | /** |
||
190 | * @return ItemHolderInterface|Cart |
||
191 | */ |
||
192 | public function getCart() |
||
202 | |||
203 | /** |
||
204 | * @param CartItem[] $cartItems |
||
205 | * |
||
206 | * @return CartItem[] |
||
207 | */ |
||
208 | protected function mergeAllCartItems($cartItems = []) |
||
219 | |||
220 | /** |
||
221 | * @param $cartItems |
||
222 | * @param $allCartItems |
||
223 | * |
||
224 | * @return array |
||
225 | */ |
||
226 | protected function mergeCartItems($cartItems, $allCartItems) |
||
245 | |||
246 | protected function restoreCarts($cartItems) |
||
292 | |||
293 | /** |
||
294 | * カートに商品を追加します. |
||
295 | * |
||
296 | * @param $ProductClass ProductClass 商品規格 |
||
297 | * @param $quantity int 数量 |
||
298 | * |
||
299 | * @return bool 商品を追加できた場合はtrue |
||
300 | */ |
||
301 | public function addProduct($ProductClass, $quantity = 1) |
||
332 | |||
333 | public function removeProduct($ProductClass) |
||
363 | |||
364 | public function save() |
||
382 | |||
383 | /** |
||
384 | * @param string $pre_order_id |
||
385 | * |
||
386 | * @return \Eccube\Service\CartService |
||
387 | */ |
||
388 | public function setPreOrderId($pre_order_id) |
||
394 | |||
395 | /** |
||
396 | * @return string |
||
397 | */ |
||
398 | public function getPreOrderId() |
||
402 | |||
403 | /** |
||
404 | * @return \Eccube\Service\CartService |
||
405 | */ |
||
406 | public function clear() |
||
426 | |||
427 | /** |
||
428 | * @param CartItemComparator $cartItemComparator |
||
429 | */ |
||
430 | public function setCartItemComparator($cartItemComparator) |
||
434 | |||
435 | /** |
||
436 | * 指定したインデックスにあるカートを優先にする |
||
437 | * |
||
438 | * @param int $index カートのインデックス |
||
439 | */ |
||
440 | public function setPrimary($index = 0) |
||
450 | |||
451 | protected function getUser() |
||
464 | |||
465 | /** |
||
466 | * @param string $allocatedId |
||
467 | */ |
||
468 | protected function createCartKey($allocatedId, Customer $Customer = null) |
||
488 | } |
||
489 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.