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 |
||
25 | class User extends Entity |
||
26 | { |
||
27 | |||
28 | use StaticCacheTrait; |
||
29 | |||
30 | /** |
||
31 | * Updates this user's Token value. |
||
32 | * |
||
33 | * The new token is persisted in DB and in this entity property. |
||
34 | * |
||
35 | * @return $this |
||
36 | * @throws \Cake\Error\FatalErrorException When an invalid user entity was given |
||
37 | * @see \User\Model\Table\UsersTable::updateToken() |
||
38 | */ |
||
39 | public function updateToken() |
||
43 | |||
44 | /** |
||
45 | * Whether this use belongs to the administrator role. |
||
46 | * |
||
47 | * @return bool |
||
48 | */ |
||
49 | public function isAdmin() |
||
50 | { |
||
51 | $roles = $this->_getRoleIds(); |
||
52 | |||
53 | return in_array(ROLE_ID_ADMINISTRATOR, $roles); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Verifies this user is allowed access the given ACO. |
||
58 | * |
||
59 | * ### Usage: |
||
60 | * |
||
61 | * ```php |
||
62 | * // Checks if current user is allowed to edit created contents: |
||
63 | * user()->isAllowed('Content/Admin/Manage/edit'); |
||
64 | * ``` |
||
65 | * |
||
66 | * @param string $aco An ACO path. e.g. `Plugin/Prefix/Controller/action` |
||
67 | * @return bool True if user can access ACO, false otherwise |
||
68 | */ |
||
69 | public function isAllowed($aco) |
||
70 | { |
||
71 | $cacheKey = 'isAllowed(' . $this->get('id') . ", {$aco})"; |
||
72 | $cache = static::cache($cacheKey); |
||
73 | if ($cache === null) { |
||
74 | $cache = TableRegistry::get('User.Permissions')->check($this, $aco); |
||
75 | static::cache($cacheKey, $cache); |
||
76 | } |
||
77 | |||
78 | return $cache; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Gets user default-avatar image's URL. |
||
83 | * |
||
84 | * Powered by Gravatar, it uses user's email to get avatar image URL from |
||
85 | * Gravatar service. |
||
86 | * |
||
87 | * @return string URL to user's avatar |
||
88 | * @link http://www.gravatar.com |
||
89 | */ |
||
90 | protected function _getAvatar() |
||
94 | |||
95 | /** |
||
96 | * Gets user's real name. |
||
97 | * |
||
98 | * @return string Name |
||
99 | */ |
||
100 | protected function _getName() |
||
106 | |||
107 | /** |
||
108 | * Gets user avatar image's URL. |
||
109 | * |
||
110 | * Powered by Gravatar, it uses user's email to get avatar image URL from |
||
111 | * Gravatar service. |
||
112 | * |
||
113 | * Use this method instead of `avatar` property when you need to customize |
||
114 | * avatar's parameters such as `size`, etc. |
||
115 | * |
||
116 | * ```php |
||
117 | * $user->avatar(['s' => 150]); // instead of: $user->avatar; |
||
118 | * ``` |
||
119 | * |
||
120 | * @param array $options Array of options for Gravatar API |
||
121 | * @return string URL to user's avatar |
||
122 | * @link http://www.gravatar.com |
||
123 | */ |
||
124 | public function avatar($options = []) |
||
139 | |||
140 | /** |
||
141 | * Hashes the password if not empty. |
||
142 | * |
||
143 | * @param string $password The RAW password |
||
144 | * @return string Encrypted password |
||
145 | */ |
||
146 | protected function _setPassword($password) |
||
154 | |||
155 | /** |
||
156 | * Gets an array list of role IDs this user belongs to. |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | View Code Duplication | protected function _getRoleIds() |
|
172 | |||
173 | /** |
||
174 | * Gets an array list of role NAMES this user belongs to. |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | View Code Duplication | protected function _getRoleNames() |
|
190 | |||
191 | /** |
||
192 | * Gets an array list of role NAMES this user belongs to. |
||
193 | * |
||
194 | * @return array |
||
195 | */ |
||
196 | protected function _getRoleSlugs() |
||
208 | |||
209 | /** |
||
210 | * Generates cancel code for this user. |
||
211 | * |
||
212 | * @return string |
||
213 | * @throws \Cake\Error\FatalErrorException When code cannot be created |
||
214 | */ |
||
215 | protected function _getCancelCode() |
||
231 | } |
||
232 |