Complex classes like Profile often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Profile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | trait Profile { |
||
28 | /** |
||
29 | * Copy of columns list of users table for internal needs without Cache usage |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $users_columns = []; |
||
33 | /** |
||
34 | * Local cache of users data |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $data = []; |
||
38 | /** |
||
39 | * Whether to use memory cache (locally, inside object, may require a lot of memory if working with many users together) |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $memory_cache = true; |
||
43 | protected function initialize_data () { |
||
51 | /** |
||
52 | * Get data item of specified user |
||
53 | * |
||
54 | * @param string|string[] $item |
||
55 | * @param false|int $user If not specified - current user assumed |
||
56 | * |
||
57 | * @return false|int|mixed[]|string|Properties If <i>$item</i> is integer - cs\User\Properties object will be returned |
||
58 | */ |
||
59 | function get ($item, $user = false) { |
||
65 | /** |
||
66 | * Get data item of specified user |
||
67 | * |
||
68 | * @param string|string[] $item |
||
69 | * @param false|int $user If not specified - current user assumed |
||
70 | * |
||
71 | * @return false|int|string|mixed[] |
||
72 | */ |
||
73 | protected function get_internal ($item, $user = false) { |
||
112 | /** |
||
113 | * Set data item of specified user |
||
114 | * |
||
115 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
116 | * @param int|null|string $value |
||
117 | * @param false|int $user If not specified - current user assumed |
||
118 | * |
||
119 | * @return bool |
||
|
|||
120 | */ |
||
121 | function set ($item, $value = null, $user = false) { |
||
146 | /** |
||
147 | * Set data item of specified user |
||
148 | * |
||
149 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
150 | * @param int|null|string $value |
||
151 | * @param int $user If not specified - current user assumed |
||
152 | * @param array $data_set |
||
153 | * |
||
154 | * @return bool |
||
155 | */ |
||
156 | protected function set_internal ($item, $value = null, $user, &$data_set) { |
||
212 | /** |
||
213 | * Check whether setting specified item to specified value for specified user is allowed |
||
214 | * |
||
215 | * @param int $user |
||
216 | * @param string $item |
||
217 | * @param string $value |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | protected function set_internal_allowed ($user, $item, $value) { |
||
244 | /** |
||
245 | * Get user id by login or email hash (sha224) (hash from lowercase string) |
||
246 | * |
||
247 | * @param string $login_hash Login or email hash |
||
248 | * |
||
249 | * @return false|int User id if found and not guest, otherwise - boolean <i>false</i> |
||
250 | */ |
||
251 | function get_id ($login_hash) { |
||
272 | /** |
||
273 | * Get user avatar, if no one present - uses Gravatar |
||
274 | * |
||
275 | * @param int|null $size Avatar size, if not specified or resizing is not possible - original image is used |
||
276 | * @param false|int $user If not specified - current user assumed |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | function avatar ($size = null, $user = false) { |
||
294 | /** |
||
295 | * Get user name or login or email, depending on existing information |
||
296 | * |
||
297 | * @param false|int $user If not specified - current user assumed |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | function username ($user = false) { |
||
315 | /** |
||
316 | * Disable memory cache |
||
317 | * |
||
318 | * Memory cache stores users data inside User class in order to get data faster next time. |
||
319 | * But in case of working with large amount of users this cache can be too large. Disabling will cause some performance drop, but save a lot of RAM. |
||
320 | */ |
||
321 | function disable_memory_cache () { |
||
325 | /** |
||
326 | * Returns array of users columns, available for getting of data |
||
327 | * |
||
328 | * @return array |
||
329 | */ |
||
330 | function get_users_columns () { |
||
333 | } |
||
334 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.