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:
1 | <?php |
||
39 | class ProductRepository extends EntityRepository |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * @var \Eccube\Application |
||
44 | */ |
||
45 | protected $app; |
||
46 | |||
47 | /** |
||
48 | * @param Application $app |
||
49 | */ |
||
50 | 99 | public function setApplication(Application $app) |
|
54 | |||
55 | /** |
||
56 | * get Product. |
||
57 | * |
||
58 | * @param integer $productId |
||
59 | * @return \Eccube\Entity\Product |
||
60 | * |
||
61 | * @throws NotFoundHttpException |
||
62 | */ |
||
63 | 17 | public function get($productId) |
|
82 | |||
83 | /** |
||
84 | * get query builder. |
||
85 | * |
||
86 | * @param array $searchData |
||
87 | * @return \Doctrine\ORM\QueryBuilder |
||
88 | */ |
||
89 | 16 | public function getQueryBuilderBySearchData($searchData) |
|
90 | { |
||
91 | 16 | $qb = $this->createQueryBuilder('p') |
|
92 | 16 | ->andWhere('p.Status = 1'); |
|
93 | |||
94 | // category |
||
95 | 16 | $categoryJoin = false; |
|
96 | 16 | View Code Duplication | if (!empty($searchData['category_id']) && $searchData['category_id']) { |
|
|||
97 | 3 | $Categories = $searchData['category_id']->getSelfAndDescendants(); |
|
98 | 3 | if ($Categories) { |
|
99 | $qb |
||
100 | 3 | ->innerJoin('p.ProductCategories', 'pct') |
|
101 | 3 | ->innerJoin('pct.Category', 'c') |
|
102 | 3 | ->andWhere($qb->expr()->in('pct.Category', ':Categories')) |
|
103 | 3 | ->setParameter('Categories', $Categories); |
|
104 | 3 | $categoryJoin = true; |
|
105 | } |
||
106 | } |
||
107 | |||
108 | // name |
||
109 | 16 | if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) { |
|
110 | 1 | $keywords = preg_split('/[\s ]+/u', $searchData['name'], -1, PREG_SPLIT_NO_EMPTY); |
|
111 | |||
112 | 1 | foreach ($keywords as $index => $keyword) { |
|
113 | 1 | $key = sprintf('keyword%s', $index); |
|
114 | $qb |
||
115 | 1 | ->andWhere(sprintf('NORMALIZE(p.name) LIKE NORMALIZE(:%s) OR NORMALIZE(p.search_word) LIKE NORMALIZE(:%s)', $key, $key)) |
|
116 | 1 | ->setParameter($key, '%' . $keyword . '%'); |
|
117 | } |
||
118 | } |
||
119 | |||
120 | // Order By |
||
121 | // 価格低い順 |
||
122 | 16 | $config = $this->app['config']; |
|
123 | 16 | if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_price_lower']) { |
|
124 | //@see http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html |
||
125 | 6 | $qb->addSelect('MIN(pc.price02) as HIDDEN price02_min'); |
|
126 | 6 | $qb->innerJoin('p.ProductClasses', 'pc'); |
|
127 | 6 | $qb->groupBy('p'); |
|
128 | // postgres9.0以下は, groupBy('p.id')が利用できない |
||
129 | // mysqlおよびpostgresql9.1以上であればgroupBy('p.id')にすることで性能向上が期待できる. |
||
130 | // @see https://github.com/EC-CUBE/ec-cube/issues/1904 |
||
131 | // $qb->groupBy('p.id'); |
||
132 | 6 | $qb->orderBy('price02_min', 'ASC'); |
|
133 | 6 | $qb->addOrderBy('p.id', 'DESC'); |
|
134 | // 価格高い順 |
||
135 | 10 | } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_price_higher']) { |
|
136 | 1 | $qb->addSelect('MAX(pc.price02) as HIDDEN price02_max'); |
|
137 | 1 | $qb->innerJoin('p.ProductClasses', 'pc'); |
|
138 | 1 | $qb->groupBy('p'); |
|
139 | 1 | $qb->orderBy('price02_max', 'DESC'); |
|
140 | 1 | $qb->addOrderBy('p.id', 'DESC'); |
|
141 | // 新着順 |
||
142 | 9 | } else if (!empty($searchData['orderby']) && $searchData['orderby']->getId() == $config['product_order_newer']) { |
|
143 | // 在庫切れ商品非表示の設定が有効時対応 |
||
144 | // @see https://github.com/EC-CUBE/ec-cube/issues/1998 |
||
145 | 2 | if ($this->app['orm.em']->getFilters()->isEnabled('nostock_hidden') == true) { |
|
146 | $qb->innerJoin('p.ProductClasses', 'pc'); |
||
147 | } |
||
148 | 2 | $qb->orderBy('p.create_date', 'DESC'); |
|
149 | } else { |
||
150 | 7 | if ($categoryJoin === false) { |
|
151 | $qb |
||
152 | 5 | ->leftJoin('p.ProductCategories', 'pct') |
|
153 | 5 | ->leftJoin('pct.Category', 'c'); |
|
154 | } |
||
155 | $qb |
||
156 | 7 | ->addOrderBy('p.id', 'DESC'); |
|
157 | } |
||
158 | |||
159 | 16 | return $qb; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * get query builder. |
||
164 | * |
||
165 | * @param array $searchData |
||
166 | * @return \Doctrine\ORM\QueryBuilder |
||
167 | */ |
||
168 | 22 | public function getQueryBuilderBySearchDataForAdmin($searchData) |
|
279 | |||
280 | /** |
||
281 | * get query builder. |
||
282 | * |
||
283 | * @param $Customer |
||
284 | * @return \Doctrine\ORM\QueryBuilder |
||
285 | * @see CustomerFavoriteProductRepository::getQueryBuilderByCustomer() |
||
286 | * @deprecated since 3.0.0, to be removed in 3.1 |
||
287 | */ |
||
288 | public function getFavoriteProductQueryBuilderByCustomer($Customer) |
||
301 | } |
||
302 |
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.