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 |
||
32 | class CartService |
||
33 | { |
||
34 | /** |
||
35 | * @var Cart[] |
||
36 | */ |
||
37 | protected $carts; |
||
38 | |||
39 | /** |
||
40 | * @var SessionInterface |
||
41 | */ |
||
42 | protected $session; |
||
43 | |||
44 | /** |
||
45 | * @var \Doctrine\ORM\EntityManagerInterface |
||
46 | */ |
||
47 | protected $entityManager; |
||
48 | |||
49 | /** |
||
50 | * @var ItemHolderInterface |
||
51 | * |
||
52 | * @deprecated |
||
53 | */ |
||
54 | protected $cart; |
||
55 | |||
56 | /** |
||
57 | * @var ProductClassRepository |
||
58 | */ |
||
59 | protected $productClassRepository; |
||
60 | |||
61 | /** |
||
62 | * @var CartRepository |
||
63 | */ |
||
64 | protected $cartRepository; |
||
65 | |||
66 | /** |
||
67 | * @var CartItemComparator |
||
68 | */ |
||
69 | protected $cartItemComparator; |
||
70 | |||
71 | /** |
||
72 | * @var CartItemAllocator |
||
73 | */ |
||
74 | protected $cartItemAllocator; |
||
75 | |||
76 | /** |
||
77 | * @var OrderHelper |
||
78 | */ |
||
79 | protected $orderHelper; |
||
80 | |||
81 | /** |
||
82 | * @var OrderRepository |
||
83 | */ |
||
84 | protected $orderRepository; |
||
85 | |||
86 | /** |
||
87 | * @var TokenStorageInterface |
||
88 | */ |
||
89 | protected $tokenStorage; |
||
90 | |||
91 | /** |
||
92 | * CartService constructor. |
||
93 | * |
||
94 | * @param SessionInterface $session |
||
95 | * @param EntityManagerInterface $entityManager |
||
96 | * @param ProductClassRepository $productClassRepository |
||
97 | * @param CartRepository $cartRepository |
||
98 | * @param CartItemComparator $cartItemComparator |
||
99 | * @param CartItemAllocator $cartItemAllocator |
||
100 | * @param OrderHelper $orderHelper |
||
101 | * @param OrderRepository $orderRepository |
||
102 | * @param TokenStorageInterface $tokenStorage |
||
103 | */ |
||
104 | public function __construct( |
||
125 | |||
126 | public function getCarts() |
||
140 | |||
141 | /** |
||
142 | * 永続化されたカートを返す |
||
143 | * |
||
144 | * @return Cart[] |
||
145 | */ |
||
146 | public function getPersistedCarts() |
||
150 | |||
151 | /** |
||
152 | * セッションにあるカートを返す |
||
153 | * |
||
154 | * @return Cart[] |
||
155 | */ |
||
156 | public function getSessionCarts() |
||
162 | |||
163 | /** |
||
164 | * 会員が保持する永続化されたカートと、非会員時のカートをマージする. |
||
165 | */ |
||
166 | public function mergeFromPersistedCart() |
||
180 | |||
181 | /** |
||
182 | * @return ItemHolderInterface|Cart |
||
183 | */ |
||
184 | public function getCart() |
||
207 | |||
208 | /** |
||
209 | * @param CartItem[] $cartItems |
||
210 | * |
||
211 | * @return CartItem[] |
||
212 | */ |
||
213 | protected function mergeAllCartItems($cartItems = []) |
||
224 | |||
225 | /** |
||
226 | * @param $cartItems |
||
227 | * @param $allCartItems |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | protected function mergeCartItems($cartItems, $allCartItems) |
||
250 | |||
251 | protected function restoreCarts($cartItems) |
||
295 | |||
296 | /** |
||
297 | * カートに商品を追加します. |
||
298 | * |
||
299 | * @param $ProductClass ProductClass 商品規格 |
||
300 | * @param $quantity int 数量 |
||
301 | * |
||
302 | * @return bool 商品を追加できた場合はtrue |
||
303 | */ |
||
304 | public function addProduct($ProductClass, $quantity = 1) |
||
335 | |||
336 | public function removeProduct($ProductClass) |
||
366 | |||
367 | public function save() |
||
385 | |||
386 | /** |
||
387 | * @param string $pre_order_id |
||
388 | * |
||
389 | * @return \Eccube\Service\CartService |
||
390 | */ |
||
391 | public function setPreOrderId($pre_order_id) |
||
397 | |||
398 | /** |
||
399 | * @return string |
||
400 | */ |
||
401 | public function getPreOrderId() |
||
405 | |||
406 | /** |
||
407 | * @return \Eccube\Service\CartService |
||
408 | */ |
||
409 | public function clear() |
||
435 | |||
436 | /** |
||
437 | * @param CartItemComparator $cartItemComparator |
||
438 | */ |
||
439 | public function setCartItemComparator($cartItemComparator) |
||
443 | |||
444 | /** |
||
445 | * カートキーで指定したインデックスにあるカートを優先にする |
||
446 | * |
||
447 | * @param string $cartKey カートキー |
||
448 | */ |
||
449 | public function setPrimary($cartKey) |
||
467 | |||
468 | protected function getUser() |
||
481 | |||
482 | /** |
||
483 | * @param string $allocatedId |
||
484 | * |
||
485 | * @return string |
||
486 | */ |
||
487 | protected function createCartKey($allocatedId, Customer $Customer = null) |
||
507 | } |
||
508 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.