Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class User implements RoleInterface |
||
28 | { |
||
29 | /** |
||
30 | * User unique id. |
||
31 | * |
||
32 | * @var ObjectId |
||
33 | */ |
||
34 | protected $_id; |
||
35 | |||
36 | /** |
||
37 | * Username. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $username; |
||
42 | |||
43 | /** |
||
44 | * Optional user attributes. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $optional = []; |
||
49 | |||
50 | /** |
||
51 | * Locale. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $locale = 'en_US'; |
||
56 | |||
57 | /** |
||
58 | * Groups. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $groups = []; |
||
63 | |||
64 | /** |
||
65 | * Last sync timestamp. |
||
66 | * |
||
67 | * @var UTCDateTime |
||
68 | */ |
||
69 | protected $last_attr_sync; |
||
70 | |||
71 | /** |
||
72 | * Soft Quota. |
||
73 | * |
||
74 | * @var int |
||
75 | */ |
||
76 | protected $soft_quota = -1; |
||
77 | |||
78 | /** |
||
79 | * Hard Quota. |
||
80 | * |
||
81 | * @var int |
||
82 | */ |
||
83 | protected $hard_quota = -1; |
||
84 | |||
85 | /** |
||
86 | * Is user deleted? |
||
87 | * |
||
88 | * @var bool|UTCDateTime |
||
89 | */ |
||
90 | protected $deleted = false; |
||
91 | |||
92 | /** |
||
93 | * Admin. |
||
94 | * |
||
95 | * @var bool |
||
96 | */ |
||
97 | protected $admin = false; |
||
98 | |||
99 | /** |
||
100 | * Created. |
||
101 | * |
||
102 | * @var UTCDateTime |
||
103 | */ |
||
104 | protected $created; |
||
105 | |||
106 | /** |
||
107 | * Changed. |
||
108 | * |
||
109 | * @var UTCDateTime |
||
110 | */ |
||
111 | protected $changed; |
||
112 | |||
113 | /** |
||
114 | * avatar. |
||
115 | * |
||
116 | * @var Binary |
||
117 | */ |
||
118 | protected $avatar; |
||
119 | |||
120 | /** |
||
121 | * Namespace. |
||
122 | * |
||
123 | * @var string |
||
124 | */ |
||
125 | protected $namespace; |
||
126 | |||
127 | /** |
||
128 | * Mail. |
||
129 | * |
||
130 | * @var string |
||
131 | */ |
||
132 | protected $mail; |
||
133 | |||
134 | /** |
||
135 | * Db. |
||
136 | * |
||
137 | * @var Database |
||
138 | */ |
||
139 | protected $db; |
||
140 | |||
141 | /** |
||
142 | * LoggerInterface. |
||
143 | * |
||
144 | * @var LoggerInterface |
||
145 | */ |
||
146 | protected $logger; |
||
147 | |||
148 | /** |
||
149 | * Server. |
||
150 | * |
||
151 | * @var Server |
||
152 | */ |
||
153 | protected $server; |
||
154 | |||
155 | /** |
||
156 | * Password. |
||
157 | * |
||
158 | * @var string |
||
159 | */ |
||
160 | protected $password; |
||
161 | |||
162 | /** |
||
163 | * Filesystem. |
||
164 | * |
||
165 | * @var Filesystem |
||
166 | */ |
||
167 | protected $fs; |
||
168 | |||
169 | /** |
||
170 | * Instance user. |
||
171 | */ |
||
172 | public function __construct(array $attributes, Server $server, Database $db, LoggerInterface $logger) |
||
182 | |||
183 | /** |
||
184 | * Return username as string. |
||
185 | */ |
||
186 | public function __toString(): string |
||
190 | |||
191 | /** |
||
192 | * Update user with identity attributes. |
||
193 | * |
||
194 | * |
||
195 | * @return User |
||
196 | */ |
||
197 | public function updateIdentity(Identity $identity): self |
||
233 | |||
234 | /** |
||
235 | * Set user attributes. |
||
236 | */ |
||
237 | public function setAttributes(array $attributes = []): bool |
||
247 | |||
248 | /** |
||
249 | * Get Attributes. |
||
250 | */ |
||
251 | public function getAttributes(): array |
||
269 | |||
270 | /** |
||
271 | * Find all shares with membership. |
||
272 | */ |
||
273 | public function getShares(): array |
||
288 | |||
289 | /** |
||
290 | * Get node attribute usage. |
||
291 | */ |
||
292 | public function getNodeAttributeSummary($attributes = [], int $limit = 25): array |
||
320 | |||
321 | /** |
||
322 | * Get filesystem. |
||
323 | */ |
||
324 | public function getFilesystem(): Filesystem |
||
332 | |||
333 | /** |
||
334 | * Is Admin user? |
||
335 | */ |
||
336 | public function isAdmin(): bool |
||
340 | |||
341 | /** |
||
342 | * Check if user has share. |
||
343 | */ |
||
344 | public function hasShare(Collection $node): bool |
||
354 | |||
355 | /** |
||
356 | * Find new shares and create reference. |
||
357 | */ |
||
358 | public function updateShares(): self |
||
488 | |||
489 | /** |
||
490 | * Get unique id. |
||
491 | */ |
||
492 | public function getId(): ObjectId |
||
496 | |||
497 | /** |
||
498 | * Get namespace. |
||
499 | */ |
||
500 | public function getNamespace(): ?string |
||
504 | |||
505 | /** |
||
506 | * Get hard quota. |
||
507 | */ |
||
508 | public function getHardQuota(): int |
||
512 | |||
513 | /** |
||
514 | * Set hard quota. |
||
515 | */ |
||
516 | public function setHardQuota(int $quota): self |
||
523 | |||
524 | /** |
||
525 | * Set soft quota. |
||
526 | */ |
||
527 | public function setSoftQuota(int $quota): self |
||
534 | |||
535 | /** |
||
536 | * Save. |
||
537 | */ |
||
538 | public function save(array $attributes = []): bool |
||
556 | |||
557 | /** |
||
558 | * Get used qota. |
||
559 | */ |
||
560 | public function getQuotaUsage(): array |
||
592 | |||
593 | /** |
||
594 | * Check quota. |
||
595 | */ |
||
596 | public function checkQuota(int $add): bool |
||
610 | |||
611 | /** |
||
612 | * Delete user. |
||
613 | */ |
||
614 | public function delete(bool $force = false, bool $data = false, bool $force_data = false): bool |
||
633 | |||
634 | /** |
||
635 | * Undelete user. |
||
636 | */ |
||
637 | public function undelete(): bool |
||
643 | |||
644 | /** |
||
645 | * Check if user is deleted. |
||
646 | */ |
||
647 | public function isDeleted(): bool |
||
651 | |||
652 | /** |
||
653 | * Get Username. |
||
654 | */ |
||
655 | public function getUsername(): string |
||
659 | |||
660 | /** |
||
661 | * Get groups. |
||
662 | */ |
||
663 | public function getGroups(): array |
||
667 | |||
668 | /** |
||
669 | * Get resolved groups. |
||
670 | * |
||
671 | * @return Generator |
||
672 | */ |
||
673 | public function getResolvedGroups(?int $offset = null, ?int $limit = null): ?Generator |
||
679 | |||
680 | /** |
||
681 | * Get attribute usage summary. |
||
682 | */ |
||
683 | protected function _getAttributeSummary(string $attribute, string $type = 'string', int $limit = 25): array |
||
727 | } |
||
728 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.