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 |
||
43 | class MemberRepository extends EntityRepository implements UserProviderInterface |
||
44 | { |
||
45 | /** |
||
46 | * @var EncoderFactoryInterface |
||
47 | */ |
||
48 | private $encoder_factory; |
||
49 | |||
50 | /** |
||
51 | * @param EncoderFactoryInterface $encoder_factory |
||
52 | */ |
||
53 | 591 | public function setEncoderFactorty(EncoderFactoryInterface $encoder_factory) |
|
57 | |||
58 | /** |
||
59 | * Loads the user for the given username. |
||
60 | * |
||
61 | * This method must throw UsernameNotFoundException if the user is not |
||
62 | * found. |
||
63 | * |
||
64 | * @param string $username The username |
||
65 | * |
||
66 | * @return UserInterface |
||
67 | * |
||
68 | * @see UsernameNotFoundException |
||
69 | * |
||
70 | * @throws UsernameNotFoundException if the user is not found |
||
71 | */ |
||
72 | 311 | public function loadUserByUsername($username) |
|
73 | { |
||
74 | $Work = $this |
||
75 | 311 | ->getEntityManager() |
|
76 | 311 | ->getRepository('Eccube\Entity\Master\Work') |
|
77 | 311 | ->find(\Eccube\Entity\Master\Work::WORK_ACTIVE_ID); |
|
78 | |||
79 | 311 | $query = $this->createQueryBuilder('m') |
|
80 | 311 | ->where('m.login_id = :login_id') |
|
81 | 311 | ->andWhere('m.Work = :Work') |
|
82 | 311 | ->setParameters(array( |
|
83 | 311 | 'login_id' => $username, |
|
84 | 311 | 'Work' => $Work, |
|
85 | )) |
||
86 | 311 | ->setMaxResults(1) |
|
87 | 311 | ->getQuery(); |
|
88 | 311 | $Member = $query->getOneOrNullResult(); |
|
89 | 311 | if (!$Member) { |
|
90 | 1 | throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); |
|
91 | } |
||
92 | |||
93 | 310 | return $Member; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Refreshes the user for the account interface. |
||
98 | * |
||
99 | * It is up to the implementation to decide if the user data should be |
||
100 | * totally reloaded (e.g. from the database), or if the UserInterface |
||
101 | * object can just be merged into some internal array of users / identity |
||
102 | * map. |
||
103 | * |
||
104 | * @param UserInterface $user |
||
105 | * |
||
106 | * @return UserInterface |
||
107 | * |
||
108 | * @throws UnsupportedUserException if the account is not supported |
||
109 | */ |
||
110 | 308 | View Code Duplication | public function refreshUser(UserInterface $user) |
118 | |||
119 | /** |
||
120 | * Whether this provider supports the given user class. |
||
121 | * |
||
122 | * @param string $class |
||
123 | * |
||
124 | * @return bool |
||
125 | */ |
||
126 | 1 | public function supportsClass($class) |
|
130 | |||
131 | /** |
||
132 | * @param \Eccube\Entity\Member $Member |
||
133 | * |
||
134 | * @return void |
||
135 | */ |
||
136 | 5 | View Code Duplication | public function up(\Eccube\Entity\Member $Member) |
165 | |||
166 | /** |
||
167 | * @param \Eccube\Entity\Member $Member |
||
168 | * @return bool |
||
169 | */ |
||
170 | 6 | View Code Duplication | public function down(\Eccube\Entity\Member $Member) |
200 | |||
201 | /** |
||
202 | * @param \Eccube\Entity\Member $Member |
||
203 | * @return bool |
||
204 | */ |
||
205 | 230 | View Code Duplication | public function save(\Eccube\Entity\Member $Member) |
235 | |||
236 | /** |
||
237 | * @param \Eccube\Entity\Member $Member |
||
238 | * @return bool |
||
239 | */ |
||
240 | 4 | View Code Duplication | public function delete(\Eccube\Entity\Member $Member) |
269 | |||
270 | /** |
||
271 | * saltを生成する |
||
272 | * |
||
273 | * @param $byte |
||
274 | * @return string |
||
275 | */ |
||
276 | 244 | public function createSalt($byte) |
|
282 | |||
283 | /** |
||
284 | * 入力されたパスワードをSaltと暗号化する |
||
285 | * |
||
286 | * @param $app |
||
287 | * @param \Eccube\Entity\Member $Member |
||
288 | * @return mixed |
||
289 | */ |
||
290 | 244 | public function encryptPassword(\Eccube\Entity\Member $Member) |
|
296 | } |
||
297 |
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.