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 |
||
22 | class Wishlist extends ActiveRecord |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @inheritdoc |
||
27 | */ |
||
28 | public static function tableName() |
||
32 | |||
33 | /** |
||
34 | * @inheritdoc |
||
35 | */ |
||
36 | View Code Duplication | public function rules() |
|
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | public function attributeLabels() |
||
74 | |||
75 | /** |
||
76 | * @return User|null |
||
77 | */ |
||
78 | public function getUser() |
||
82 | |||
83 | /** |
||
84 | * @return WishlistProduct[]|null |
||
85 | */ |
||
86 | public function getItems() |
||
90 | |||
91 | /** |
||
92 | * @param $user_id |
||
93 | * @param array $wishlist_ids |
||
94 | * @return array |
||
95 | */ |
||
96 | public static function getWishlist($user_id, $wishlist_ids) |
||
106 | |||
107 | /** |
||
108 | * @param string $title |
||
109 | * @param $user_id |
||
110 | * @param array $wishlist_ids |
||
111 | * @param bool $default |
||
112 | * @return Wishlist|null |
||
113 | */ |
||
114 | public static function createWishlist($title, $user_id, $wishlist_ids, $default = true) |
||
129 | |||
130 | /** |
||
131 | * @param $productId |
||
132 | * @return bool |
||
133 | */ |
||
134 | public function addToWishlist($productId) |
||
154 | |||
155 | /** |
||
156 | * @param $user_id |
||
157 | * @param array $wishlist_ids |
||
158 | * @param $wishlistId |
||
159 | * @return int |
||
160 | */ |
||
161 | public static function countItems($user_id, $wishlist_ids, $wishlistId = null) |
||
178 | |||
179 | /** |
||
180 | * @param $id |
||
181 | * @param $user_id |
||
182 | * @param array $wishlist_ids |
||
183 | * @return bool |
||
184 | */ |
||
185 | public static function setDefaultWishlist($id, $user_id, $wishlist_ids) |
||
201 | |||
202 | /** |
||
203 | * @param string $title |
||
204 | * @return bool |
||
205 | */ |
||
206 | public function renameWishlist($title) |
||
211 | |||
212 | /** |
||
213 | * @param $itemId |
||
214 | * @return bool |
||
215 | */ |
||
216 | public function removeItem($itemId) |
||
226 | |||
227 | /** |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function clearWishlist() |
||
235 | |||
236 | /** |
||
237 | * @param $user_id |
||
238 | * @param array $wishlist_ids |
||
239 | * @param $wishlistId |
||
240 | * @param array $selections |
||
241 | * @return string |
||
242 | */ |
||
243 | public static function getTotalPrice($user_id, $wishlist_ids, $wishlistId = null, $selections = null) |
||
275 | } |
||
276 |
This check looks for accesses to local static members using the fully qualified name instead of
self::
.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBC
could just as well be replaced byself::TRIPLEDES_CBC
. Referencing local members withself::
assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.