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 | 232 | public function setApplication($app) |
|
| 48 | |||
| 49 | 7 | public function findUnusedBlocks(DeviceType $DeviceType, $pageId) |
|
| 71 | |||
| 72 | 12 | public function get(DeviceType $DeviceType, $pageId) |
|
| 73 | { |
||
| 74 | 12 | $qb = $this->createQueryBuilder('p') |
|
| 75 | 12 | ->select('p, bp, b') |
|
| 76 | 12 | ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id') |
|
| 77 | 12 | ->leftJoin('bp.Block', 'b') |
|
| 78 | 12 | ->andWhere('p.DeviceType = :DeviceType AND p.id = :pageId') |
|
| 79 | 12 | ->addOrderBy('bp.target_id', 'ASC') |
|
| 80 | 12 | ->addOrderBy('bp.block_row', 'ASC'); |
|
| 81 | |||
| 82 | $ownResult = $qb |
||
| 83 | 12 | ->getQuery() |
|
| 84 | 12 | ->setParameters(array( |
|
| 85 | 12 | 'DeviceType' => $DeviceType, |
|
| 86 | 12 | 'pageId' => $pageId, |
|
| 87 | )) |
||
| 88 | 12 | ->getSingleResult(); |
|
| 89 | |||
| 90 | 12 | $qb = $this->createQueryBuilder('p') |
|
| 91 | 12 | ->select('p, bp, b') |
|
| 92 | 12 | ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id') |
|
| 93 | 12 | ->leftJoin('bp.Block', 'b') |
|
| 94 | 12 | ->andWhere('p.DeviceType = :DeviceType AND bp.anywhere = 1') |
|
| 95 | 12 | ->addOrderBy('bp.target_id', 'ASC') |
|
| 96 | 12 | ->addOrderBy('bp.block_row', 'ASC'); |
|
| 97 | |||
| 98 | $anyResults = $qb |
||
| 99 | 12 | ->getQuery() |
|
| 100 | 12 | ->setParameters(array( |
|
| 101 | 12 | 'DeviceType' => $DeviceType, |
|
| 102 | )) |
||
| 103 | 12 | ->getResult(); |
|
| 104 | |||
| 105 | 12 | $OwnBlockPosition = $ownResult->getBlockPositions(); |
|
| 106 | 12 | foreach ($anyResults as $anyResult) { |
|
| 107 | 12 | $BlockPositions = $anyResult->getBlockPositions(); |
|
| 108 | 12 | foreach ($BlockPositions as $BlockPosition) { |
|
| 109 | 12 | if (!$OwnBlockPosition->contains($BlockPosition)) { |
|
| 110 | 12 | $ownResult->addBlockPosition($BlockPosition); |
|
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | 12 | return $ownResult; |
|
| 116 | |||
| 117 | } |
||
| 118 | |||
| 119 | 207 | public function getByUrl(DeviceType $DeviceType, $url) |
|
| 120 | { |
||
| 121 | 207 | $options = $this->app['config']['doctrine_cache']; |
|
| 122 | 207 | $lifetime = $options['result_cache']['lifetime']; |
|
| 123 | |||
| 124 | 207 | $qb = $this->createQueryBuilder('p') |
|
| 125 | 207 | ->select('p, bp, b') |
|
| 126 | 207 | ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id') |
|
| 127 | 207 | ->leftJoin('bp.Block', 'b') |
|
| 128 | 207 | ->andWhere('p.DeviceType = :DeviceType AND p.url = :url') |
|
| 129 | 207 | ->addOrderBy('bp.target_id', 'ASC') |
|
| 130 | 207 | ->addOrderBy('bp.block_row', 'ASC'); |
|
| 131 | |||
| 132 | $ownResult = $qb |
||
| 133 | 207 | ->getQuery() |
|
| 134 | 207 | ->useResultCache(true, $lifetime) |
|
| 135 | 207 | ->setParameters(array( |
|
| 136 | 207 | 'DeviceType' => $DeviceType, |
|
| 137 | 207 | 'url' => $url, |
|
| 138 | )) |
||
| 139 | 207 | ->getSingleResult(); |
|
| 140 | |||
| 141 | 104 | $qb = $this->createQueryBuilder('p') |
|
| 142 | 104 | ->select('p, bp, b') |
|
| 143 | 104 | ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id') |
|
| 144 | 104 | ->leftJoin('bp.Block', 'b') |
|
| 145 | 104 | ->andWhere('p.DeviceType = :DeviceType AND bp.anywhere = 1') |
|
| 146 | 104 | ->addOrderBy('bp.target_id', 'ASC') |
|
| 147 | 104 | ->addOrderBy('bp.block_row', 'ASC'); |
|
| 148 | |||
| 149 | $anyResults = $qb |
||
| 150 | 104 | ->getQuery() |
|
| 151 | 104 | ->useResultCache(true, $lifetime) |
|
| 152 | 104 | ->setParameters(array( |
|
| 153 | 104 | 'DeviceType' => $DeviceType, |
|
| 154 | )) |
||
| 155 | 104 | ->getResult(); |
|
| 156 | |||
| 157 | 104 | $OwnBlockPosition = $ownResult->getBlockPositions(); |
|
| 158 | 104 | foreach ($anyResults as $anyResult) { |
|
| 159 | 104 | $BlockPositions = $anyResult->getBlockPositions(); |
|
| 160 | 104 | foreach ($BlockPositions as $BlockPosition) { |
|
| 161 | 104 | if (!$OwnBlockPosition->contains($BlockPosition)) { |
|
| 162 | 104 | $ownResult->addBlockPosition($BlockPosition); |
|
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | 104 | return $ownResult; |
|
| 168 | } |
||
| 169 | |||
| 170 | 105 | public function newPageLayout(DeviceType $DeviceType) |
|
| 179 | |||
| 180 | 6 | public function findOrCreate($page_id, DeviceType $DeviceType) |
|
| 181 | { |
||
| 182 | 6 | if (is_null($page_id)) { |
|
| 183 | $PageLayout = $this |
||
| 184 | 2 | ->newPageLayout($DeviceType); |
|
| 185 | 2 | return $PageLayout; |
|
| 186 | } else { |
||
| 187 | 4 | return $this->get($DeviceType, $page_id); |
|
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * ページの属性を取得する. |
||
| 193 | * |
||
| 194 | * この関数は, dtb_pagelayout の情報を検索する. |
||
| 195 | * $deviceTypeId は必須. デフォルト値は DEVICE_TYPE_PC. |
||
| 196 | * |
||
| 197 | * @access public |
||
| 198 | * @param \Eccube\Entity\Master\DeviceType $DeviceType 端末種別ID |
||
| 199 | * @param string $where 追加の検索条件 |
||
| 200 | * @param string[] $parameters 追加の検索パラメーター |
||
| 201 | * @return array ページ属性の配列 |
||
| 202 | */ |
||
| 203 | 3 | View Code Duplication | public function getPageList(DeviceType $DeviceType, $where = null, $parameters = array()) |
| 204 | { |
||
| 205 | 3 | $qb = $this->createQueryBuilder('l') |
|
| 206 | 3 | ->orderBy('l.id', 'DESC') |
|
| 207 | 3 | ->where('l.DeviceType = :DeviceType') |
|
| 208 | 3 | ->setParameter('DeviceType', $DeviceType) |
|
| 209 | 3 | ->andWhere('l.id <> 0') |
|
| 210 | 3 | ->orderBy('l.id', 'ASC'); |
|
| 211 | 3 | if (!is_null($where)) { |
|
| 212 | $qb->andWhere($where); |
||
| 213 | foreach ($parameters as $key => $val) { |
||
| 214 | $qb->setParameter($key, $val); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | $PageLayouts = $qb |
||
| 219 | 3 | ->getQuery() |
|
| 220 | 3 | ->getResult(); |
|
| 221 | |||
| 222 | 3 | return $PageLayouts; |
|
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * 書き込みパスの取得 |
||
| 227 | * User定義の場合: /html/user_data |
||
| 228 | * そうでない場合: /app/template/{template_code} |
||
| 229 | * |
||
| 230 | * @param boolean $isUser |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | 8 | public function getWriteTemplatePath($isUser = false) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * 読み込みファイルの取得 |
||
| 240 | * |
||
| 241 | * 1. template_realdir |
||
| 242 | * app/template/{template_code} |
||
| 243 | * 2. template_default_readldir |
||
| 244 | * src/Eccube/Resource/template/default |
||
| 245 | * |
||
| 246 | * @param string $fileName |
||
| 247 | * @param boolean $isUser |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | 6 | public function getReadTemplateFile($fileName, $isUser = false) |
|
| 276 | } |
||
| 277 |