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 | * Loads the user for the given username. |
||
49 | * |
||
50 | * This method must throw UsernameNotFoundException if the user is not |
||
51 | * found. |
||
52 | * |
||
53 | * @param string $username The username |
||
54 | * |
||
55 | * @return UserInterface |
||
56 | * |
||
57 | * @see UsernameNotFoundException |
||
58 | * |
||
59 | * @throws UsernameNotFoundException if the user is not found |
||
60 | */ |
||
61 | 218 | public function loadUserByUsername($username) |
|
71 | |||
72 | /** |
||
73 | * Refreshes the user for the account interface. |
||
74 | * |
||
75 | * It is up to the implementation to decide if the user data should be |
||
76 | * totally reloaded (e.g. from the database), or if the UserInterface |
||
77 | * object can just be merged into some internal array of users / identity |
||
78 | * map. |
||
79 | * |
||
80 | * @param UserInterface $user |
||
81 | * |
||
82 | * @return UserInterface |
||
83 | * |
||
84 | * @throws UnsupportedUserException if the account is not supported |
||
85 | */ |
||
86 | 217 | View Code Duplication | public function refreshUser(UserInterface $user) |
94 | |||
95 | /** |
||
96 | * Whether this provider supports the given user class. |
||
97 | * |
||
98 | * @param string $class |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | 1 | public function supportsClass($class) |
|
106 | |||
107 | /** |
||
108 | * 管理ユーザの表示順を一つ上げる. |
||
109 | * |
||
110 | * @param Member $Member |
||
111 | * @throws \Exception 更新対象のユーザより上位のユーザが存在しない場合. |
||
112 | */ |
||
113 | 4 | public function up(Member $Member) |
|
128 | |||
129 | /** |
||
130 | * 管理ユーザの表示順を一つ下げる. |
||
131 | * |
||
132 | * @param Member $Member |
||
133 | * @throws \Exception 更新対象のユーザより下位のユーザが存在しない場合. |
||
134 | */ |
||
135 | 5 | View Code Duplication | public function down(Member $Member) |
150 | |||
151 | /** |
||
152 | * 管理ユーザを登録します. |
||
153 | * |
||
154 | * @param Member $Member |
||
155 | */ |
||
156 | 230 | public function save($Member) |
|
171 | |||
172 | /** |
||
173 | * 管理ユーザを削除します. |
||
174 | * |
||
175 | * @param Member $Member |
||
176 | * |
||
177 | * @throws ForeignKeyConstraintViolationException 外部キー制約違反の場合 |
||
178 | * @throws DriverException SQLiteの場合, 外部キー制約違反が発生すると, DriverExceptionをthrowします. |
||
179 | */ |
||
180 | 3 | public function delete($Member) |
|
194 | } |
||
195 |
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.