Conditions | 24 |
Paths | 4608 |
Total Lines | 124 |
Lines | 48 |
Ratio | 38.71 % |
Changes | 0 |
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 |
||
216 | public function getQueryBuilderBySearchDataForAdmin($searchData) |
||
217 | { |
||
218 | $qb = $this->createQueryBuilder('p') |
||
219 | ->addSelect('pc', 'pi', 'tr', 'ps') |
||
220 | ->innerJoin('p.ProductClasses', 'pc') |
||
221 | ->leftJoin('p.ProductImage', 'pi') |
||
222 | ->leftJoin('pc.TaxRule', 'tr') |
||
223 | ->leftJoin('pc.ProductStock', 'ps'); |
||
224 | |||
225 | // id |
||
226 | if (isset($searchData['id']) && StringUtil::isNotBlank($searchData['id'])) { |
||
227 | $id = preg_match('/^\d{0,10}$/', $searchData['id']) ? $searchData['id'] : null; |
||
228 | $qb |
||
229 | ->andWhere('p.id = :id OR p.name LIKE :likeid OR pc.code LIKE :likeid') |
||
230 | ->setParameter('id', $id) |
||
231 | ->setParameter('likeid', '%'.str_replace(['%', '_'], ['\\%', '\\_'], $searchData['id']).'%'); |
||
232 | } |
||
233 | |||
234 | // code |
||
235 | /* |
||
236 | if (!empty($searchData['code']) && $searchData['code']) { |
||
237 | $qb |
||
238 | ->innerJoin('p.ProductClasses', 'pc') |
||
239 | ->andWhere('pc.code LIKE :code') |
||
240 | ->setParameter('code', '%' . $searchData['code'] . '%'); |
||
241 | } |
||
242 | |||
243 | // name |
||
244 | if (!empty($searchData['name']) && $searchData['name']) { |
||
245 | $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY); |
||
246 | foreach ($keywords as $keyword) { |
||
247 | $qb |
||
248 | ->andWhere('p.name LIKE :name') |
||
249 | ->setParameter('name', '%' . $keyword . '%'); |
||
250 | } |
||
251 | } |
||
252 | */ |
||
253 | |||
254 | // category |
||
255 | View Code Duplication | if (!empty($searchData['category_id']) && $searchData['category_id']) { |
|
256 | $Categories = $searchData['category_id']->getSelfAndDescendants(); |
||
257 | if ($Categories) { |
||
258 | $qb |
||
259 | ->innerJoin('p.ProductCategories', 'pct') |
||
260 | ->innerJoin('pct.Category', 'c') |
||
261 | ->andWhere($qb->expr()->in('pct.Category', ':Categories')) |
||
262 | ->setParameter('Categories', $Categories); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | // status |
||
267 | View Code Duplication | if (!empty($searchData['status']) && $searchData['status']) { |
|
268 | $qb |
||
269 | ->andWhere($qb->expr()->in('p.Status', ':Status')) |
||
270 | ->setParameter('Status', $searchData['status']); |
||
271 | } |
||
272 | |||
273 | // link_status |
||
274 | View Code Duplication | if (isset($searchData['link_status']) && !empty($searchData['link_status'])) { |
|
275 | $qb |
||
276 | ->andWhere($qb->expr()->in('p.Status', ':Status')) |
||
277 | ->setParameter('Status', $searchData['link_status']); |
||
278 | } |
||
279 | |||
280 | // stock status |
||
281 | if (isset($searchData['stock_status'])) { |
||
282 | $qb |
||
283 | ->andWhere('pc.stock_unlimited = :StockUnlimited AND pc.stock = 0') |
||
284 | ->setParameter('StockUnlimited', $searchData['stock_status']); |
||
285 | } |
||
286 | |||
287 | // stock status |
||
288 | if (isset($searchData['stock']) && !empty($searchData['stock'])) { |
||
289 | switch ($searchData['stock']) { |
||
290 | case [ProductStock::IN_STOCK]: |
||
291 | $qb->andWhere('pc.stock_unlimited = true OR pc.stock > 0'); |
||
292 | break; |
||
293 | case [ProductStock::OUT_OF_STOCK]: |
||
294 | $qb->andWhere('pc.stock_unlimited = false AND pc.stock <= 0'); |
||
295 | break; |
||
296 | default: |
||
297 | // 共に選択された場合は全権該当するので検索条件に含めない |
||
298 | } |
||
299 | } |
||
300 | |||
301 | // crate_date |
||
302 | View Code Duplication | if (!empty($searchData['create_date_start']) && $searchData['create_date_start']) { |
|
303 | $date = $searchData['create_date_start']; |
||
304 | $qb |
||
305 | ->andWhere('p.create_date >= :create_date_start') |
||
306 | ->setParameter('create_date_start', $date); |
||
307 | } |
||
308 | |||
309 | View Code Duplication | if (!empty($searchData['create_date_end']) && $searchData['create_date_end']) { |
|
310 | $date = clone $searchData['create_date_end']; |
||
311 | $date = $date |
||
312 | ->modify('+1 days'); |
||
313 | $qb |
||
314 | ->andWhere('p.create_date < :create_date_end') |
||
315 | ->setParameter('create_date_end', $date); |
||
316 | } |
||
317 | |||
318 | // update_date |
||
319 | View Code Duplication | if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) { |
|
320 | $date = $searchData['update_date_start']; |
||
321 | $qb |
||
322 | ->andWhere('p.update_date >= :update_date_start') |
||
323 | ->setParameter('update_date_start', $date); |
||
324 | } |
||
325 | View Code Duplication | if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) { |
|
326 | $date = clone $searchData['update_date_end']; |
||
327 | $date = $date |
||
328 | ->modify('+1 days'); |
||
329 | $qb |
||
330 | ->andWhere('p.update_date < :update_date_end') |
||
331 | ->setParameter('update_date_end', $date); |
||
332 | } |
||
333 | |||
334 | // Order By |
||
335 | $qb |
||
336 | ->orderBy('p.update_date', 'DESC'); |
||
337 | |||
338 | return $this->queries->customize(QueryKey::PRODUCT_SEARCH_ADMIN, $qb, $searchData); |
||
339 | } |
||
340 | } |
||
341 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.