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 ShippingRepository 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 ShippingRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class ShippingRepository extends AbstractRepository |
||
28 | { |
||
29 | 108 | public function __construct(RegistryInterface $registry) |
|
33 | |||
34 | /** |
||
35 | * Build query |
||
36 | * |
||
37 | * @param array $searchData |
||
38 | * |
||
39 | * @return QueryBuilder |
||
40 | */ |
||
41 | public function getQueryBuilderBySearchDataForAdmin($searchData) |
||
42 | { |
||
43 | $qb = $this->createQueryBuilder('s'); |
||
44 | |||
45 | $qb->leftJoin('s.OrderItems', 'si') |
||
46 | ->leftJoin('si.Order', 'o'); |
||
47 | // order_id_start |
||
48 | View Code Duplication | if (isset($searchData['shipping_id_start']) && StringUtil::isNotBlank($searchData['shipping_id_start'])) { |
|
49 | $qb |
||
50 | ->andWhere('s.id >= :shipping_id_start') |
||
51 | ->setParameter('shipping_id_start', $searchData['shipping_id_start']); |
||
52 | } |
||
53 | // multi |
||
54 | View Code Duplication | if (isset($searchData['multi']) && StringUtil::isNotBlank($searchData['multi'])) { |
|
55 | $multi = preg_match('/^\d{0,10}$/', $searchData['multi']) ? $searchData['multi'] : null; |
||
56 | $qb |
||
57 | ->andWhere('s.id = :multi OR s.name01 LIKE :likemulti OR s.name02 LIKE :likemulti OR '. |
||
|
|||
58 | 's.kana01 LIKE :likemulti OR s.kana02 LIKE :likemulti OR s.company_name LIKE :likemulti') |
||
59 | ->setParameter('multi', $multi) |
||
60 | ->setParameter('likemulti', '%'.$searchData['multi'].'%'); |
||
61 | } |
||
62 | |||
63 | // shipping_id_end |
||
64 | View Code Duplication | if (isset($searchData['shipping_id_end']) && StringUtil::isNotBlank($searchData['shipping_id_end'])) { |
|
65 | $qb |
||
66 | ->andWhere('s.id <= :shipping_id_end') |
||
67 | ->setParameter('shipping_id_end', $searchData['shipping_id_end']); |
||
68 | } |
||
69 | |||
70 | // order_id |
||
71 | View Code Duplication | if (isset($searchData['order_id']) && StringUtil::isNotBlank($searchData['order_id'])) { |
|
72 | $qb |
||
73 | ->andWhere('o.id = :order_id') |
||
74 | ->setParameter('order_id', $searchData['order_id']); |
||
75 | } |
||
76 | |||
77 | // order_no |
||
78 | if (isset($searchData['order_no']) && StringUtil::isNotBlank($searchData['order_no'])) { |
||
79 | $qb |
||
80 | ->andWhere('o.order_no LIKE :order_no') |
||
81 | ->setParameter('order_no', "%{$searchData['order_no']}%"); |
||
82 | } |
||
83 | |||
84 | // order status |
||
85 | if (isset($searchData['order_status']) && count($searchData['order_status'])) { |
||
86 | $s = $searchData['order_status']; |
||
87 | $qb |
||
88 | ->andWhere($qb->expr()->in('o.OrderStatus', ':order_status')) |
||
89 | ->setParameter('order_status', $searchData['order_status']); |
||
90 | } |
||
91 | // name |
||
92 | View Code Duplication | if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) { |
|
93 | $qb |
||
94 | ->andWhere('CONCAT(s.name01, s.name02) LIKE :name') |
||
95 | ->setParameter('name', '%'.$searchData['name'].'%'); |
||
96 | } |
||
97 | |||
98 | // kana |
||
99 | View Code Duplication | if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) { |
|
100 | $qb |
||
101 | ->andWhere('CONCAT(s.kana01, s.kana02) LIKE :kana') |
||
102 | ->setParameter('kana', '%'.$searchData['kana'].'%'); |
||
103 | } |
||
104 | |||
105 | // order_name |
||
106 | View Code Duplication | if (isset($searchData['order_name']) && StringUtil::isNotBlank($searchData['order_name'])) { |
|
107 | $qb |
||
108 | ->andWhere('CONCAT(o.name01, o.name02) LIKE :order_name') |
||
109 | ->setParameter('order_name', '%'.$searchData['order_name'].'%'); |
||
110 | } |
||
111 | |||
112 | // order_kana |
||
113 | View Code Duplication | if (isset($searchData['order_kana']) && StringUtil::isNotBlank($searchData['order_kana'])) { |
|
114 | $qb |
||
115 | ->andWhere('CONCAT(o.kana01, s.kana02) LIKE :order_kana') |
||
116 | ->setParameter('order_kana', '%'.$searchData['order_kana'].'%'); |
||
117 | } |
||
118 | |||
119 | // order_email |
||
120 | View Code Duplication | if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) { |
|
121 | $qb |
||
122 | ->andWhere('o.email like :email') |
||
123 | ->setParameter('email', '%'.$searchData['email'].'%'); |
||
124 | } |
||
125 | |||
126 | // tel |
||
127 | View Code Duplication | if (isset($searchData['phone_number']) && StringUtil::isNotBlank($searchData['phone_number'])) { |
|
128 | $tel = preg_replace('/[^0-9]/ ', '', $searchData['phone_number']); |
||
129 | $qb |
||
130 | ->andWhere('s.phone_number LIKE :phone_number') |
||
131 | ->setParameter('phone_number', '%'.$tel.'%'); |
||
132 | } |
||
133 | |||
134 | // payment |
||
135 | View Code Duplication | if (!empty($searchData['payment']) && count($searchData['payment'])) { |
|
136 | $payments = []; |
||
137 | foreach ($searchData['payment'] as $payment) { |
||
138 | $payments[] = $payment->getId(); |
||
139 | } |
||
140 | $qb |
||
141 | ->leftJoin('o.Payment', 'p') |
||
142 | ->andWhere($qb->expr()->in('p.id', ':payments')) |
||
143 | ->setParameter('payments', $payments); |
||
144 | } |
||
145 | |||
146 | // oreder_date |
||
147 | View Code Duplication | if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) { |
|
148 | $date = $searchData['order_date_start']; |
||
149 | $qb |
||
150 | ->andWhere('o.order_date >= :order_date_start') |
||
151 | ->setParameter('order_date_start', $date); |
||
152 | } |
||
153 | View Code Duplication | if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) { |
|
154 | $date = clone $searchData['order_date_end']; |
||
155 | $date = $date |
||
156 | ->modify('+1 days'); |
||
157 | $qb |
||
158 | ->andWhere('o.order_date < :order_date_end') |
||
159 | ->setParameter('order_date_end', $date); |
||
160 | } |
||
161 | |||
162 | // shipping_delivery_date |
||
163 | View Code Duplication | if (!empty($searchData['shipping_delivery_date_start']) && $searchData['shipping_delivery_date_start']) { |
|
164 | $date = $searchData['shipping_delivery_date_start']; |
||
165 | $qb |
||
166 | ->andWhere('s.shipping_delivery_date >= :shipping_delivery_date_start') |
||
167 | ->setParameter('shipping_delivery_date_start', $date); |
||
168 | } |
||
169 | View Code Duplication | if (!empty($searchData['shipping_delivery_date_end']) && $searchData['shipping_delivery_date_end']) { |
|
170 | $date = clone $searchData['shipping_delivery_date_end']; |
||
171 | $date = $date |
||
172 | ->modify('+1 days'); |
||
173 | $qb |
||
174 | ->andWhere('s.shipping_delivery_date < :shipping_delivery_date_end') |
||
175 | ->setParameter('shipping_delivery_date_end', $date); |
||
176 | } |
||
177 | |||
178 | // shipping_date |
||
179 | View Code Duplication | if (!empty($searchData['shipping_date_start']) && $searchData['shipping_date_start']) { |
|
180 | $date = $searchData['shipping_date_start']; |
||
181 | $qb |
||
182 | ->andWhere('s.shipping_date >= :shipping_date_start') |
||
183 | ->setParameter('shipping_date_start', $date); |
||
184 | } |
||
185 | View Code Duplication | if (!empty($searchData['shipping_date_end']) && $searchData['shipping_date_end']) { |
|
186 | $date = clone $searchData['shipping_date_end']; |
||
187 | $date = $date |
||
188 | ->modify('+1 days'); |
||
189 | $qb |
||
190 | ->andWhere('s.shipping_date < :shipping_date_end') |
||
191 | ->setParameter('shipping_date_end', $date); |
||
192 | } |
||
193 | |||
194 | // update_date |
||
195 | View Code Duplication | if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) { |
|
196 | $date = $searchData['update_date_start']; |
||
197 | $qb |
||
198 | ->andWhere('s.update_date >= :update_date_start') |
||
199 | ->setParameter('update_date_start', $date); |
||
200 | } |
||
201 | View Code Duplication | if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) { |
|
202 | $date = clone $searchData['update_date_end']; |
||
203 | $date = $date |
||
204 | ->modify('+1 days'); |
||
205 | $qb |
||
206 | ->andWhere('s.update_date < :update_date_end') |
||
207 | ->setParameter('update_date_end', $date); |
||
208 | } |
||
209 | |||
210 | // payment_total |
||
211 | View Code Duplication | if (isset($searchData['payment_total_start']) && StringUtil::isNotBlank($searchData['payment_total_start'])) { |
|
212 | $qb |
||
213 | ->andWhere('o.payment_total >= :payment_total_start') |
||
214 | ->setParameter('payment_total_start', $searchData['payment_total_start']); |
||
215 | } |
||
216 | View Code Duplication | if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) { |
|
217 | $qb |
||
218 | ->andWhere('o.payment_total <= :payment_total_end') |
||
219 | ->setParameter('payment_total_end', $searchData['payment_total_end']); |
||
220 | } |
||
221 | |||
222 | // buy_product_name |
||
223 | View Code Duplication | if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) { |
|
224 | $qb |
||
225 | ->andWhere('si.product_name LIKE :buy_product_name') |
||
226 | ->setParameter('buy_product_name', '%'.$searchData['buy_product_name'].'%'); |
||
227 | } |
||
228 | |||
229 | // Order By |
||
230 | $qb->orderBy('s.update_date', 'DESC'); |
||
231 | $qb->addorderBy('s.id', 'DESC'); |
||
232 | |||
233 | return $qb; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * 同一商品のお届け先情報を取得 |
||
238 | * |
||
239 | * @param $Order |
||
240 | * |
||
241 | * @return array |
||
242 | */ |
||
243 | 27 | public function findShippingsProduct($Order, $productClass) |
|
256 | } |
||
257 |