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 |
||
40 | class PageLayoutRepository extends EntityRepository |
||
41 | { |
||
42 | protected $app; |
||
43 | |||
44 | 5 | public function setApplication($app) |
|
48 | |||
49 | public function findUnusedBlocks(DeviceType $DeviceType, $pageId) |
||
50 | { |
||
51 | $em = $this |
||
52 | ->getEntityManager(); |
||
53 | $blockRepo = $em->getRepository('Eccube\Entity\Block'); |
||
54 | $ownBlockPositions = $this->get($DeviceType, $pageId)->getBlockPositions(); |
||
55 | $ids = array(); |
||
56 | foreach ($ownBlockPositions as $ownBlockPosition) { |
||
57 | $ids[] = $ownBlockPosition->getBlock()->getId(); |
||
58 | } |
||
59 | |||
60 | # $idsが空配列だと、$ids以外のblockを取得するSQLが生成されないため、存在しないidを入れる |
||
61 | if (empty($ids)) { |
||
62 | $ids[] = \Eccube\Entity\Block::UNUSED_BLOCK_ID; |
||
63 | } |
||
64 | |||
65 | return $blockRepo->createQueryBuilder('b') |
||
66 | ->where('b.id not in (:ids)') |
||
67 | ->setParameter(':ids', $ids) |
||
68 | ->getQuery() |
||
69 | ->getResult(); |
||
70 | } |
||
71 | |||
72 | 2 | View Code Duplication | public function get(DeviceType $DeviceType, $pageId) |
73 | { |
||
74 | 2 | $qb = $this->createQueryBuilder('p') |
|
75 | 2 | ->select('p, bp, b') |
|
76 | 2 | ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id') |
|
77 | 2 | ->leftJoin('bp.Block', 'b') |
|
78 | 2 | ->andWhere('p.DeviceType = :DeviceType AND p.id = :pageId') |
|
79 | 2 | ->addOrderBy('bp.target_id', 'ASC') |
|
80 | ->addOrderBy('bp.block_row', 'ASC'); |
||
81 | |||
82 | $ownResult = $qb |
||
83 | 2 | ->getQuery() |
|
84 | ->setParameters(array( |
||
85 | 'DeviceType' => $DeviceType, |
||
86 | 'pageId' => $pageId, |
||
87 | 2 | )) |
|
88 | ->getSingleResult(); |
||
89 | |||
90 | 2 | $qb = $this->createQueryBuilder('p') |
|
91 | 2 | ->select('p, bp, b') |
|
92 | 2 | ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id') |
|
93 | 2 | ->leftJoin('bp.Block', 'b') |
|
94 | 2 | ->andWhere('p.DeviceType = :DeviceType AND bp.anywhere = 1') |
|
95 | 2 | ->addOrderBy('bp.target_id', 'ASC') |
|
96 | ->addOrderBy('bp.block_row', 'ASC'); |
||
97 | |||
98 | $anyResults = $qb |
||
99 | 2 | ->getQuery() |
|
100 | ->setParameters(array( |
||
101 | 'DeviceType' => $DeviceType, |
||
102 | 2 | )) |
|
103 | ->getResult(); |
||
104 | |||
105 | $OwnBlockPosition = $ownResult->getBlockPositions(); |
||
106 | foreach ($anyResults as $anyResult) { |
||
107 | $BlockPositions = $anyResult->getBlockPositions(); |
||
108 | foreach ($BlockPositions as $BlockPosition) { |
||
109 | if (!$OwnBlockPosition->contains($BlockPosition)) { |
||
110 | $ownResult->addBlockPosition($BlockPosition); |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | |||
115 | 2 | return $ownResult; |
|
116 | |||
117 | 2 | } |
|
118 | |||
119 | View Code Duplication | public function getByUrl(DeviceType $DeviceType, $url) |
|
120 | { |
||
121 | $qb = $this->createQueryBuilder('p') |
||
122 | ->select('p, bp, b') |
||
123 | ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id') |
||
124 | ->leftJoin('bp.Block', 'b') |
||
125 | ->andWhere('p.DeviceType = :DeviceType AND p.url = :url') |
||
126 | ->addOrderBy('bp.target_id', 'ASC') |
||
127 | ->addOrderBy('bp.block_row', 'ASC'); |
||
128 | |||
129 | $ownResult = $qb |
||
130 | ->getQuery() |
||
131 | ->setParameters(array( |
||
132 | 'DeviceType' => $DeviceType, |
||
133 | 'url' => $url, |
||
134 | )) |
||
135 | ->getSingleResult(); |
||
136 | |||
137 | $qb = $this->createQueryBuilder('p') |
||
138 | ->select('p, bp, b') |
||
139 | ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id') |
||
140 | ->leftJoin('bp.Block', 'b') |
||
141 | ->andWhere('p.DeviceType = :DeviceType AND bp.anywhere = 1') |
||
142 | ->addOrderBy('bp.target_id', 'ASC') |
||
143 | ->addOrderBy('bp.block_row', 'ASC'); |
||
144 | |||
145 | $anyResults = $qb |
||
146 | ->getQuery() |
||
147 | ->setParameters(array( |
||
148 | 'DeviceType' => $DeviceType, |
||
149 | )) |
||
150 | ->getResult(); |
||
151 | |||
152 | $OwnBlockPosition = $ownResult->getBlockPositions(); |
||
153 | foreach ($anyResults as $anyResult) { |
||
154 | $BlockPositions = $anyResult->getBlockPositions(); |
||
155 | foreach ($BlockPositions as $BlockPosition) { |
||
156 | if (!$OwnBlockPosition->contains($BlockPosition)) { |
||
157 | $ownResult->addBlockPosition($BlockPosition); |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | |||
162 | return $ownResult; |
||
163 | } |
||
164 | |||
165 | public function newPageLayout(DeviceType $DeviceType) |
||
166 | { |
||
167 | $PageLayout = new \Eccube\Entity\PageLayout(); |
||
168 | $PageLayout |
||
169 | ->setDeviceType($DeviceType) |
||
170 | ->setEditFlg(PageLayout::EDIT_FLG_USER); |
||
171 | |||
172 | return $PageLayout; |
||
173 | } |
||
174 | |||
175 | 1 | public function findOrCreate($page_id, DeviceType $DeviceType) |
|
176 | { |
||
177 | if (is_null($page_id)) { |
||
178 | $PageLayout = $this |
||
179 | ->newPageLayout($DeviceType); |
||
180 | return $PageLayout; |
||
181 | } else { |
||
182 | return $this->get($DeviceType, $page_id); |
||
183 | } |
||
184 | 1 | } |
|
185 | |||
186 | /** |
||
187 | * ページの属性を取得する. |
||
188 | * |
||
189 | * この関数は, dtb_pagelayout の情報を検索する. |
||
190 | * $deviceTypeId は必須. デフォルト値は DEVICE_TYPE_PC. |
||
191 | * |
||
192 | * @access public |
||
193 | * @param \Eccube\Entity\Master\DeviceType $DeviceType 端末種別ID |
||
194 | * @param string $where 追加の検索条件 |
||
195 | * @param string[] $parameters 追加の検索パラメーター |
||
196 | * @return array ページ属性の配列 |
||
197 | */ |
||
198 | 1 | View Code Duplication | public function getPageList(DeviceType $DeviceType, $where = null, $parameters = array()) |
219 | |||
220 | /** |
||
221 | * 書き込みパスの取得 |
||
222 | * User定義の場合: /html/user_data |
||
223 | * そうでない場合: /app/template/{template_code} |
||
224 | * |
||
225 | * @param boolean $isUser |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getWriteTemplatePath($isUser = false) |
||
232 | |||
233 | /** |
||
234 | * 読み込みファイルの取得 |
||
235 | * |
||
236 | * 1. template_realdir |
||
237 | * app/template/{template_code} |
||
238 | * 2. template_default_readldir |
||
239 | * src/Eccube/Resource/template/default |
||
240 | * |
||
241 | * @param string $fileName |
||
242 | * @param boolean $isUser |
||
243 | * |
||
244 | * @return array |
||
245 | */ |
||
246 | 1 | public function getReadTemplateFile($fileName, $isUser = false) |
|
270 | } |
||
271 |