Conditions | 41 |
Paths | > 20000 |
Total Lines | 173 |
Code Lines | 116 |
Lines | 86 |
Ratio | 49.71 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
142 | public function getQueryBuilderBySearchData($searchData) |
||
143 | { |
||
144 | $qb = $this->createQueryBuilder('c') |
||
145 | ->select('c') |
||
146 | ->andWhere('c.del_flg = 0'); |
||
147 | |||
148 | if (isset($searchData['multi']) && Str::isNotBlank($searchData['multi'])) { |
||
149 | //スペース除去 |
||
150 | $clean_key_multi = preg_replace('/\s+|[ ]+/u', '',$searchData['multi']); |
||
151 | if (preg_match('/^\d+$/', $clean_key_multi)) { |
||
152 | $qb |
||
153 | ->andWhere('c.id = :customer_id') |
||
154 | ->setParameter('customer_id', $clean_key_multi); |
||
155 | } else { |
||
156 | $qb |
||
157 | ->andWhere('CONCAT(c.name01, c.name02) LIKE :name OR CONCAT(c.kana01, c.kana02) LIKE :kana OR c.email LIKE :email') |
||
158 | ->setParameter('name', '%' . $clean_key_multi . '%') |
||
159 | ->setParameter('kana', '%' . $clean_key_multi . '%') |
||
160 | ->setParameter('email', '%' . $clean_key_multi . '%'); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | // Pref |
||
165 | if (!empty($searchData['pref']) && $searchData['pref']) { |
||
166 | $qb |
||
167 | ->andWhere('c.Pref = :pref') |
||
168 | ->setParameter('pref', $searchData['pref']->getId()); |
||
169 | } |
||
170 | |||
171 | // sex |
||
172 | if (!empty($searchData['sex']) && count($searchData['sex']) > 0) { |
||
173 | $sexs = array(); |
||
174 | foreach ($searchData['sex'] as $sex) { |
||
175 | $sexs[] = $sex->getId(); |
||
176 | } |
||
177 | |||
178 | $qb |
||
179 | ->andWhere($qb->expr()->in('c.Sex', ':sexs')) |
||
180 | ->setParameter('sexs', $sexs); |
||
181 | } |
||
182 | |||
183 | // birth_month |
||
184 | // TODO: http://docs.symfony.gr.jp/symfony2/cookbook/doctrine/custom_dql_functions.html |
||
185 | if (!empty($searchData['birth_month']) && $searchData['birth_month']) { |
||
186 | // $qb |
||
187 | // ->andWhere('extract(month from c.birth) = :birth_month') |
||
188 | // ->setParameter('birth_month', $searchData['birth_month']); |
||
189 | } |
||
190 | |||
191 | // birth |
||
192 | View Code Duplication | if (!empty($searchData['birth_start']) && $searchData['birth_start']) { |
|
193 | $date = $searchData['birth_start'] |
||
194 | ->format('Y-m-d H:i:s'); |
||
195 | $qb |
||
196 | ->andWhere('c.birth >= :birth_start') |
||
197 | ->setParameter('birth_start', $date); |
||
198 | } |
||
199 | View Code Duplication | if (!empty($searchData['birth_end']) && $searchData['birth_end']) { |
|
200 | $date = clone $searchData['birth_end']; |
||
201 | $date = $date |
||
202 | ->modify('+1 days') |
||
203 | ->format('Y-m-d H:i:s'); |
||
204 | $qb |
||
205 | ->andWhere('c.birth < :birth_end') |
||
206 | ->setParameter('birth_end', $date); |
||
207 | } |
||
208 | |||
209 | // tel |
||
210 | View Code Duplication | if (isset($searchData['tel']) && Str::isNotBlank($searchData['tel'])) { |
|
211 | $qb |
||
212 | ->andWhere('CONCAT(c.tel01, c.tel02, c.tel03) LIKE :tel') |
||
213 | ->setParameter('tel', '%' . $searchData['tel'] . '%'); |
||
214 | } |
||
215 | |||
216 | // buy_total |
||
217 | View Code Duplication | if (isset($searchData['buy_total_start']) && Str::isNotBlank($searchData['buy_total_start'])) { |
|
218 | $qb |
||
219 | ->andWhere('c.buy_total >= :buy_total_start') |
||
220 | ->setParameter('buy_total_start', $searchData['buy_total_start']); |
||
221 | } |
||
222 | View Code Duplication | if (isset($searchData['buy_total_end']) && Str::isNotBlank($searchData['buy_total_end'])) { |
|
223 | $qb |
||
224 | ->andWhere('c.buy_total <= :buy_total_end') |
||
225 | ->setParameter('buy_total_end', $searchData['buy_total_end']); |
||
226 | } |
||
227 | |||
228 | // buy_times |
||
229 | if (!empty($searchData['buy_times_start']) && $searchData['buy_times_start']) { |
||
230 | $qb |
||
231 | ->andWhere('c.buy_times >= :buy_times_start') |
||
232 | ->setParameter('buy_times_start', $searchData['buy_times_start']); |
||
233 | } |
||
234 | if (!empty($searchData['buy_times_end']) && $searchData['buy_times_end']) { |
||
235 | $qb |
||
236 | ->andWhere('c.buy_times <= :buy_times_end') |
||
237 | ->setParameter('buy_times_end', $searchData['buy_times_end']); |
||
238 | } |
||
239 | |||
240 | // create_date |
||
241 | View Code Duplication | if (!empty($searchData['create_date_start']) && $searchData['create_date_start']) { |
|
242 | $date = $searchData['create_date_start'] |
||
243 | ->format('Y-m-d H:i:s'); |
||
244 | $qb |
||
245 | ->andWhere('c.create_date >= :create_date_start') |
||
246 | ->setParameter('create_date_start', $date); |
||
247 | } |
||
248 | View Code Duplication | if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) { |
|
249 | $date = clone $searchData['create_date_end']; |
||
250 | $date = $date |
||
251 | ->modify('+1 days') |
||
252 | ->format('Y-m-d H:i:s'); |
||
253 | $qb |
||
254 | ->andWhere('c.create_date < :create_date_end') |
||
255 | ->setParameter('create_date_end', $date); |
||
256 | } |
||
257 | |||
258 | // update_date |
||
259 | View Code Duplication | if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) { |
|
260 | $date = $searchData['update_date_start'] |
||
261 | ->format('Y-m-d H:i:s'); |
||
262 | $qb |
||
263 | ->andWhere('c.update_date >= :update_date_start') |
||
264 | ->setParameter('update_date_start', $date); |
||
265 | } |
||
266 | View Code Duplication | if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) { |
|
267 | $date = clone $searchData['update_date_end']; |
||
268 | $date = $date |
||
269 | ->modify('+1 days') |
||
270 | ->format('Y-m-d H:i:s'); |
||
271 | $qb |
||
272 | ->andWhere('c.update_date < :update_date_end') |
||
273 | ->setParameter('update_date_end', $date); |
||
274 | } |
||
275 | |||
276 | // last_buy |
||
277 | View Code Duplication | if (!empty($searchData['last_buy_start']) && $searchData['last_buy_start']) { |
|
278 | $date = $searchData['last_buy_start'] |
||
279 | ->format('Y-m-d H:i:s'); |
||
280 | $qb |
||
281 | ->andWhere('c.last_buy_date >= :last_buy_start') |
||
282 | ->setParameter('last_buy_start', $date); |
||
283 | } |
||
284 | View Code Duplication | if (!empty($searchData['last_buy_end']) && $searchData['last_buy_end']) { |
|
285 | $date = clone $searchData['last_buy_end']; |
||
286 | $date = $date |
||
287 | ->modify('+1 days') |
||
288 | ->format('Y-m-d H:i:s'); |
||
289 | $qb |
||
290 | ->andWhere('c.last_buy_date < :last_buy_end') |
||
291 | ->setParameter('last_buy_end', $date); |
||
292 | } |
||
293 | |||
294 | // status |
||
295 | if (!empty($searchData['customer_status']) && count($searchData['customer_status']) > 0) { |
||
296 | $qb |
||
297 | ->andWhere($qb->expr()->in('c.Status', ':statuses')) |
||
298 | ->setParameter('statuses', $searchData['customer_status']); |
||
299 | } |
||
300 | |||
301 | // buy_product_name、buy_product_code |
||
302 | View Code Duplication | if (isset($searchData['buy_product_code']) && Str::isNotBlank($searchData['buy_product_code'])) { |
|
303 | $qb |
||
304 | ->leftJoin('c.Orders', 'o') |
||
305 | ->leftJoin('o.OrderDetails', 'od') |
||
306 | ->andWhere('od.product_name LIKE :buy_product_name OR od.product_code LIKE :buy_product_name') |
||
307 | ->setParameter('buy_product_name', '%' . $searchData['buy_product_code'] . '%'); |
||
308 | } |
||
309 | |||
310 | // Order By |
||
311 | $qb->addOrderBy('c.update_date', 'DESC'); |
||
312 | |||
313 | return $qb; |
||
314 | } |
||
315 | |||
477 |