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 |
||
45 | class MemberRepository extends AbstractRepository implements UserProviderInterface |
||
46 | { |
||
47 | /** |
||
48 | * @var EncoderFactoryInterface |
||
49 | * @Inject("security.encoder_factory") |
||
50 | */ |
||
51 | private $encoder_factory; |
||
52 | |||
53 | /** |
||
54 | * @param EncoderFactoryInterface $encoder_factory |
||
55 | */ |
||
56 | public function setEncoderFactorty(EncoderFactoryInterface $encoder_factory) |
||
57 | { |
||
58 | $this->encoder_factory = $encoder_factory; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Loads the user for the given username. |
||
63 | * |
||
64 | * This method must throw UsernameNotFoundException if the user is not |
||
65 | * found. |
||
66 | * |
||
67 | * @param string $username The username |
||
68 | * |
||
69 | * @return UserInterface |
||
70 | * |
||
71 | * @see UsernameNotFoundException |
||
72 | * |
||
73 | * @throws UsernameNotFoundException if the user is not found |
||
74 | */ |
||
75 | 223 | public function loadUserByUsername($username) |
|
76 | { |
||
77 | $Work = $this |
||
78 | 223 | ->getEntityManager() |
|
79 | 223 | ->getRepository('Eccube\Entity\Master\Work') |
|
80 | 223 | ->find(\Eccube\Entity\Master\Work::WORK_ACTIVE_ID); |
|
81 | |||
82 | 223 | $query = $this->createQueryBuilder('m') |
|
83 | 223 | ->where('m.login_id = :login_id') |
|
84 | 223 | ->andWhere('m.Work = :Work') |
|
85 | 223 | ->setParameters(array( |
|
86 | 223 | 'login_id' => $username, |
|
87 | 223 | 'Work' => $Work, |
|
88 | )) |
||
89 | 223 | ->setMaxResults(1) |
|
90 | 223 | ->getQuery(); |
|
91 | 223 | $Member = $query->getOneOrNullResult(); |
|
92 | 223 | if (!$Member) { |
|
93 | 1 | throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); |
|
94 | } |
||
95 | |||
96 | 222 | return $Member; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Refreshes the user for the account interface. |
||
101 | * |
||
102 | * It is up to the implementation to decide if the user data should be |
||
103 | * totally reloaded (e.g. from the database), or if the UserInterface |
||
104 | * object can just be merged into some internal array of users / identity |
||
105 | * map. |
||
106 | * |
||
107 | * @param UserInterface $user |
||
108 | * |
||
109 | * @return UserInterface |
||
110 | * |
||
111 | * @throws UnsupportedUserException if the account is not supported |
||
112 | */ |
||
113 | 220 | View Code Duplication | public function refreshUser(UserInterface $user) |
|
|||
114 | { |
||
115 | 220 | if (!$user instanceof Member) { |
|
116 | 1 | throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); |
|
117 | } |
||
118 | |||
119 | 219 | return $this->loadUserByUsername($user->getUsername()); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Whether this provider supports the given user class. |
||
124 | * |
||
125 | * @param string $class |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | 1 | public function supportsClass($class) |
|
130 | { |
||
131 | 1 | return $class === 'Eccube\Entity\Member'; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param \Eccube\Entity\Member $Member |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | 4 | View Code Duplication | public function up(\Eccube\Entity\Member $Member) |
140 | { |
||
141 | 4 | $em = $this->getEntityManager(); |
|
142 | 4 | $em->getConnection()->beginTransaction(); |
|
143 | try { |
||
144 | 4 | $rank = $Member->getRank(); |
|
145 | |||
146 | 4 | $Member2 = $this->findOneBy(array('rank' => $rank + 1)); |
|
147 | 4 | if (!$Member2) { |
|
148 | 2 | throw new \Exception(); |
|
149 | } |
||
150 | 2 | $Member2->setRank($rank); |
|
151 | 2 | $em->persist($Member2); |
|
152 | |||
153 | // Member更新 |
||
154 | 2 | $Member->setRank($rank + 1); |
|
155 | |||
156 | 2 | $em->persist($Member); |
|
157 | 2 | $em->flush(); |
|
158 | |||
159 | 2 | $em->getConnection()->commit(); |
|
160 | 2 | } catch (\Exception $e) { |
|
161 | 2 | $em->getConnection()->rollback(); |
|
162 | |||
163 | 2 | return false; |
|
164 | } |
||
165 | |||
166 | 2 | return true; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param \Eccube\Entity\Member $Member |
||
171 | * @return bool |
||
172 | */ |
||
173 | 5 | View Code Duplication | public function down(\Eccube\Entity\Member $Member) |
174 | { |
||
175 | 5 | $em = $this->getEntityManager(); |
|
176 | 5 | $em->getConnection()->beginTransaction(); |
|
177 | try { |
||
178 | 5 | $rank = $Member->getRank(); |
|
179 | |||
180 | // |
||
181 | 5 | $Member2 = $this->findOneBy(array('rank' => $rank - 1)); |
|
182 | 5 | if (!$Member2) { |
|
183 | 2 | throw new \Exception(); |
|
184 | } |
||
185 | 3 | $Member2->setRank($rank); |
|
186 | 3 | $em->persist($Member2); |
|
187 | |||
188 | // Member更新 |
||
189 | 3 | $Member->setRank($rank - 1); |
|
190 | |||
191 | 3 | $em->persist($Member); |
|
192 | 3 | $em->flush(); |
|
193 | |||
194 | 3 | $em->getConnection()->commit(); |
|
195 | 2 | } catch (\Exception $e) { |
|
196 | 2 | $em->getConnection()->rollback(); |
|
197 | |||
198 | 2 | return false; |
|
199 | } |
||
200 | |||
201 | 3 | return true; |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param \Eccube\Entity\Member $Member |
||
206 | * @return bool |
||
207 | */ |
||
208 | 237 | public function save($Member) |
|
209 | { |
||
210 | 237 | $em = $this->getEntityManager(); |
|
211 | 237 | $em->getConnection()->beginTransaction(); |
|
212 | try { |
||
213 | 237 | if (!$Member->getId()) { |
|
214 | 237 | $rank = $this->createQueryBuilder('m') |
|
215 | 237 | ->select('MAX(m.rank)') |
|
216 | 237 | ->getQuery() |
|
217 | 237 | ->getSingleScalarResult(); |
|
218 | 237 | if (!$rank) { |
|
219 | $rank = 0; |
||
220 | } |
||
221 | $Member |
||
222 | 237 | ->setRank($rank + 1); |
|
223 | } |
||
224 | |||
225 | 237 | $em->persist($Member); |
|
226 | 237 | $em->flush(); |
|
227 | |||
228 | 237 | $em->getConnection()->commit(); |
|
229 | 1 | } catch (\Exception $e) { |
|
230 | 1 | $em->getConnection()->rollback(); |
|
231 | |||
232 | 1 | return false; |
|
233 | } |
||
234 | |||
235 | 237 | return true; |
|
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param \Eccube\Entity\Member $Member |
||
240 | * @return bool |
||
241 | */ |
||
242 | 3 | View Code Duplication | public function delete($Member) |
243 | { |
||
244 | 3 | $em = $this->getEntityManager(); |
|
245 | 3 | $em->getConnection()->beginTransaction(); |
|
246 | try { |
||
247 | 3 | $rank = $Member->getRank(); |
|
248 | 3 | $em->createQueryBuilder() |
|
249 | 3 | ->update('Eccube\Entity\Member', 'm') |
|
250 | 3 | ->set('m.rank', 'm.rank - 1') |
|
251 | 3 | ->where('m.rank > :rank')->setParameter('rank', $rank) |
|
252 | 3 | ->getQuery() |
|
253 | 3 | ->execute(); |
|
254 | |||
255 | 3 | $em->remove($Member); |
|
256 | 3 | $em->flush(); |
|
257 | |||
258 | 2 | $em->getConnection()->commit(); |
|
259 | 1 | } catch (\Exception $e) { |
|
260 | 1 | $em->getConnection()->rollback(); |
|
261 | |||
262 | 1 | return false; |
|
263 | } |
||
264 | |||
265 | 2 | return true; |
|
266 | } |
||
267 | |||
268 | /** |
||
269 | * saltを生成する |
||
270 | * |
||
271 | * @param $byte |
||
272 | * @return string |
||
273 | */ |
||
274 | 237 | public function createSalt($byte) |
|
278 | |||
279 | /** |
||
280 | * 入力されたパスワードをSaltと暗号化する |
||
281 | * |
||
282 | * @param $app |
||
283 | * @param \Eccube\Entity\Member $Member |
||
284 | * @return mixed |
||
285 | */ |
||
286 | 237 | public function encryptPassword(\Eccube\Entity\Member $Member) |
|
292 | } |
||
293 |
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.