Complex classes like Data 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 Data, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | trait Data { |
||
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 | * Changed users data, at the finish, data in db must be replaced by this data |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $data_set = []; |
||
43 | /** |
||
44 | * Whether to use memory cache (locally, inside object, may require a lot of memory if working with many users together) |
||
45 | * @var bool |
||
46 | */ |
||
47 | protected $memory_cache = true; |
||
48 | protected function initialize_data () { |
||
56 | /** |
||
57 | * Get data item of specified user |
||
58 | * |
||
59 | * @param string|string[] $item |
||
60 | * @param false|int $user If not specified - current user assumed |
||
61 | * |
||
62 | * @return false|int|mixed[]|string|Properties If <i>$item</i> is integer - cs\User\Properties object will be returned |
||
|
|||
63 | */ |
||
64 | function get ($item, $user = false) { |
||
70 | /** |
||
71 | * Get data item of specified user |
||
72 | * |
||
73 | * @todo Refactor this to select all or nothing; this selection of only necessary stuff is tricky and should be simplified |
||
74 | * |
||
75 | * @param string|string[] $item |
||
76 | * @param false|int $user If not specified - current user assumed |
||
77 | * |
||
78 | * @return false|int|string|mixed[] |
||
79 | */ |
||
80 | protected function get_internal ($item, $user = false) { |
||
142 | /** |
||
143 | * @param string $item |
||
144 | * @param int $user |
||
145 | * @param mixed[] $data |
||
146 | * |
||
147 | * @return false|int|string |
||
148 | */ |
||
149 | protected function get_internal_one_item ($item, $user, &$data) { |
||
175 | /** |
||
176 | * Set data item of specified user |
||
177 | * |
||
178 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
179 | * @param int|null|string $value |
||
180 | * @param false|int $user If not specified - current user assumed |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | function set ($item, $value = null, $user = false) { |
||
189 | /** |
||
190 | * Set data item of specified user |
||
191 | * |
||
192 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
193 | * @param int|null|string $value |
||
194 | * @param false|int $user If not specified - current user assumed |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | protected function set_internal ($item, $value = null, $user = false) { |
||
254 | /** |
||
255 | * Check whether setting specified item to specified value for specified user is allowed |
||
256 | * |
||
257 | * @param int $user |
||
258 | * @param string $item |
||
259 | * @param string $value |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | protected function set_internal_allowed ($user, $item, $value) { |
||
287 | /** |
||
288 | * Getting additional data item(s) of specified user |
||
289 | * |
||
290 | * @param string|string[] $item |
||
291 | * @param false|int $user If not specified - current user assumed |
||
292 | * |
||
293 | * @return false|string|mixed[] |
||
294 | */ |
||
295 | function get_data ($item, $user = false) { |
||
366 | /** |
||
367 | * Setting additional data item(s) of specified user |
||
368 | * |
||
369 | * @param array|string $item Item-value array may be specified for setting several items at once |
||
370 | * @param mixed|null $value |
||
371 | * @param false|int $user If not specified - current user assumed |
||
372 | * |
||
373 | * @return bool |
||
374 | */ |
||
375 | function set_data ($item, $value = null, $user = false) { |
||
421 | /** |
||
422 | * Deletion of additional data item(s) of specified user |
||
423 | * |
||
424 | * @param string|string[] $item |
||
425 | * @param false|int $user If not specified - current user assumed |
||
426 | * |
||
427 | * @return bool |
||
428 | */ |
||
429 | function del_data ($item, $user = false) { |
||
447 | /** |
||
448 | * Get user id by login or email hash (sha224) (hash from lowercase string) |
||
449 | * |
||
450 | * @param string $login_hash Login or email hash |
||
451 | * |
||
452 | * @return false|int User id if found and not guest, otherwise - boolean <i>false</i> |
||
453 | */ |
||
454 | function get_id ($login_hash) { |
||
477 | /** |
||
478 | * Get user avatar, if no one present - uses Gravatar |
||
479 | * |
||
480 | * @param int|null $size Avatar size, if not specified or resizing is not possible - original image is used |
||
481 | * @param false|int $user If not specified - current user assumed |
||
482 | * |
||
483 | * @return string |
||
484 | */ |
||
485 | function avatar ($size = null, $user = false) { |
||
499 | /** |
||
500 | * Get user name or login or email, depending on existing information |
||
501 | * |
||
502 | * @param false|int $user If not specified - current user assumed |
||
503 | * |
||
504 | * @return string |
||
505 | */ |
||
506 | function username ($user = false) { |
||
520 | /** |
||
521 | * Disable memory cache |
||
522 | * |
||
523 | * Memory cache stores users data inside User class in order to get data faster next time. |
||
524 | * 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. |
||
525 | */ |
||
526 | function disable_memory_cache () { |
||
529 | /** |
||
530 | * Returns array of users columns, available for getting of data |
||
531 | * |
||
532 | * @return array |
||
533 | */ |
||
534 | function get_users_columns () { |
||
537 | /** |
||
538 | * Saving changes of cache and users data |
||
539 | */ |
||
540 | protected function persist_data () { |
||
566 | } |
||
567 |
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.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.