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 |
||
36 | class BlockRepository extends EntityRepository |
||
37 | { |
||
38 | protected $app; |
||
39 | |||
40 | 14 | public function setApplication($app) |
|
44 | |||
45 | 5 | public function findOrCreate($block_id, $DeviceType) |
|
46 | { |
||
47 | |||
48 | 5 | if ($block_id == null) { |
|
49 | 1 | return $this->newBlock($DeviceType); |
|
50 | } else { |
||
51 | 5 | return $this->getBlock($block_id, $DeviceType); |
|
52 | } |
||
53 | |||
54 | } |
||
55 | |||
56 | 1 | public function newBlock($DeviceType) |
|
57 | { |
||
58 | 1 | $Block = new \Eccube\Entity\Block(); |
|
59 | $Block |
||
60 | 1 | ->setDeviceType($DeviceType) |
|
61 | 1 | ->setLogicFlg(0) |
|
62 | 1 | ->setDeletableFlg(1); |
|
63 | |||
64 | 1 | return $Block; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * @deprecated since 3.0.0, to be removed in 3.1 |
||
69 | */ |
||
70 | private function getNewBlockId($DeviceType) |
||
71 | { |
||
72 | |||
73 | $qb = $this->createQueryBuilder('b') |
||
74 | ->select('max(b.id) +1 as block_id') |
||
75 | ->where('b.DeviceType = :DeviceType') |
||
76 | ->setParameter('DeviceType', $DeviceType); |
||
77 | $result = $qb->getQuery()->getSingleResult(); |
||
78 | |||
79 | return $result['block_id']; |
||
80 | |||
81 | } |
||
82 | |||
83 | /** |
||
84 | * ブロックの情報を取得. |
||
85 | * |
||
86 | * @param integer $block_id ブロックID |
||
87 | * @param \Eccube\Entity\Master\DeviceType $DeviceType |
||
88 | * @return array |
||
89 | */ |
||
90 | 6 | public function getBlock($block_id, $DeviceType) |
|
91 | { |
||
92 | 6 | $Block = $this->findOneBy(array( |
|
93 | 6 | 'id' => $block_id, |
|
94 | 6 | 'DeviceType' => $DeviceType, |
|
95 | 6 | )); |
|
96 | |||
97 | 6 | return $Block; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * ブロック一覧の取得. |
||
102 | * |
||
103 | * @param \Eccube\Entity\Master\DeviceType $DeviceType |
||
104 | * @return array |
||
105 | */ |
||
106 | 9 | public function getList($DeviceType) |
|
107 | { |
||
108 | 3 | $qb = $this->createQueryBuilder('b') |
|
109 | 3 | ->orderBy('b.id', 'DESC') |
|
110 | 3 | ->where('b.DeviceType = :DeviceType') |
|
111 | 3 | ->setParameter('DeviceType', $DeviceType); |
|
112 | |||
113 | $Blocks = $qb |
||
114 | 3 | ->getQuery() |
|
115 | 3 | ->getResult(); |
|
116 | |||
117 | 9 | return $Blocks; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * ページの属性を取得する. |
||
122 | * |
||
123 | * この関数は, dtb_pagelayout の情報を検索する. |
||
124 | * $deviceTypeId は必須. デフォルト値は DEVICE_TYPE_PC. |
||
125 | * |
||
126 | * @access public |
||
127 | * @param DeviceType $DeviceType 端末種別ID |
||
128 | * @param string $where 追加の検索条件 |
||
129 | * @param string[] $parameters 追加の検索パラメーター |
||
130 | * @return array ページ属性の配列 |
||
131 | * @deprecated since 3.0.0, to be removed in 3.1 |
||
132 | */ |
||
133 | View Code Duplication | public function getPageList(DeviceType $DeviceType, $where = null, $parameters = array()) |
|
134 | { |
||
135 | $qb = $this->createQueryBuilder('l') |
||
136 | ->orderBy('l.id', 'DESC') |
||
137 | ->where('l.DeviceType = :DeviceType') |
||
138 | ->setParameter('DeviceType', $DeviceType) |
||
139 | ->andWhere('l.id <> 0') |
||
140 | ->orderBy('l.id', 'ASC'); |
||
141 | if (!is_null($where)) { |
||
142 | $qb->andWhere($where); |
||
143 | foreach ($parameters as $key => $val) { |
||
144 | $qb->setParameter($key, $val); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | $PageLayouts = $qb |
||
149 | ->getQuery() |
||
150 | ->getResult(); |
||
151 | |||
152 | return $PageLayouts; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * 書き込みパスの取得 |
||
157 | * User定義の場合: /html/user_data |
||
158 | * そうでない場合: /app/template/{template_code} |
||
159 | * |
||
160 | * @param boolean $isUser |
||
161 | * @return string |
||
162 | * |
||
163 | * @deprecated since 3.0.0, to be removed in 3.1 |
||
164 | */ |
||
165 | 1 | public function getWriteTemplatePath($isUser = false) |
|
169 | |||
170 | /** |
||
171 | * 読み込みファイルの取得 |
||
172 | * |
||
173 | * 1. block_realdir |
||
174 | * app/template/{template_code}/block |
||
175 | * 2. block_default_readldir |
||
176 | * src/Eccube/Resource/template/default/block |
||
177 | * |
||
178 | * @param string $fileName |
||
179 | * @param boolean $isUser |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | 6 | public function getReadTemplateFile($fileName, $isUser = false) |
|
200 | } |
||
201 |