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  | 
            ||
| 26 | class User implements RoleInterface  | 
            ||
| 27 | { | 
            ||
| 28 | /**  | 
            ||
| 29 | * User unique id.  | 
            ||
| 30 | *  | 
            ||
| 31 | * @var ObjectId  | 
            ||
| 32 | */  | 
            ||
| 33 | protected $_id;  | 
            ||
| 34 | |||
| 35 | /**  | 
            ||
| 36 | * Username.  | 
            ||
| 37 | *  | 
            ||
| 38 | * @var string  | 
            ||
| 39 | */  | 
            ||
| 40 | protected $username;  | 
            ||
| 41 | |||
| 42 | /**  | 
            ||
| 43 | * Optional user attributes.  | 
            ||
| 44 | *  | 
            ||
| 45 | * @var array  | 
            ||
| 46 | */  | 
            ||
| 47 | protected $optional = [];  | 
            ||
| 48 | |||
| 49 | /**  | 
            ||
| 50 | * Locale.  | 
            ||
| 51 | *  | 
            ||
| 52 | * @var string  | 
            ||
| 53 | */  | 
            ||
| 54 | protected $locale = 'en_US';  | 
            ||
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * Groups.  | 
            ||
| 58 | *  | 
            ||
| 59 | * @var array  | 
            ||
| 60 | */  | 
            ||
| 61 | protected $groups = [];  | 
            ||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * Last sync timestamp.  | 
            ||
| 65 | *  | 
            ||
| 66 | * @var UTCDateTime  | 
            ||
| 67 | */  | 
            ||
| 68 | protected $last_attr_sync;  | 
            ||
| 69 | |||
| 70 | /**  | 
            ||
| 71 | * Soft Quota.  | 
            ||
| 72 | *  | 
            ||
| 73 | * @var int  | 
            ||
| 74 | */  | 
            ||
| 75 | protected $soft_quota = -1;  | 
            ||
| 76 | |||
| 77 | /**  | 
            ||
| 78 | * Hard Quota.  | 
            ||
| 79 | *  | 
            ||
| 80 | * @var int  | 
            ||
| 81 | */  | 
            ||
| 82 | protected $hard_quota = -1;  | 
            ||
| 83 | |||
| 84 | /**  | 
            ||
| 85 | * Is user deleted?  | 
            ||
| 86 | *  | 
            ||
| 87 | * @var bool|UTCDateTime  | 
            ||
| 88 | */  | 
            ||
| 89 | protected $deleted = false;  | 
            ||
| 90 | |||
| 91 | /**  | 
            ||
| 92 | * Admin.  | 
            ||
| 93 | *  | 
            ||
| 94 | * @var bool  | 
            ||
| 95 | */  | 
            ||
| 96 | protected $admin = false;  | 
            ||
| 97 | |||
| 98 | /**  | 
            ||
| 99 | * Created.  | 
            ||
| 100 | *  | 
            ||
| 101 | * @var UTCDateTime  | 
            ||
| 102 | */  | 
            ||
| 103 | protected $created;  | 
            ||
| 104 | |||
| 105 | /**  | 
            ||
| 106 | * Changed.  | 
            ||
| 107 | *  | 
            ||
| 108 | * @var UTCDateTime  | 
            ||
| 109 | */  | 
            ||
| 110 | protected $changed;  | 
            ||
| 111 | |||
| 112 | /**  | 
            ||
| 113 | * avatar.  | 
            ||
| 114 | *  | 
            ||
| 115 | * @var Binary  | 
            ||
| 116 | */  | 
            ||
| 117 | protected $avatar;  | 
            ||
| 118 | |||
| 119 | /**  | 
            ||
| 120 | * Namespace.  | 
            ||
| 121 | *  | 
            ||
| 122 | * @var string  | 
            ||
| 123 | */  | 
            ||
| 124 | protected $namespace;  | 
            ||
| 125 | |||
| 126 | /**  | 
            ||
| 127 | * Mail.  | 
            ||
| 128 | *  | 
            ||
| 129 | * @var string  | 
            ||
| 130 | */  | 
            ||
| 131 | protected $mail;  | 
            ||
| 132 | |||
| 133 | /**  | 
            ||
| 134 | * Db.  | 
            ||
| 135 | *  | 
            ||
| 136 | * @var Database  | 
            ||
| 137 | */  | 
            ||
| 138 | protected $db;  | 
            ||
| 139 | |||
| 140 | /**  | 
            ||
| 141 | * LoggerInterface.  | 
            ||
| 142 | *  | 
            ||
| 143 | * @var LoggerInterface  | 
            ||
| 144 | */  | 
            ||
| 145 | protected $logger;  | 
            ||
| 146 | |||
| 147 | /**  | 
            ||
| 148 | * Server.  | 
            ||
| 149 | *  | 
            ||
| 150 | * @var Server  | 
            ||
| 151 | */  | 
            ||
| 152 | protected $server;  | 
            ||
| 153 | |||
| 154 | /**  | 
            ||
| 155 | * Password.  | 
            ||
| 156 | *  | 
            ||
| 157 | * @var string  | 
            ||
| 158 | */  | 
            ||
| 159 | protected $password;  | 
            ||
| 160 | |||
| 161 | /**  | 
            ||
| 162 | * Filesystem.  | 
            ||
| 163 | *  | 
            ||
| 164 | * @var Filesystem  | 
            ||
| 165 | */  | 
            ||
| 166 | protected $fs;  | 
            ||
| 167 | |||
| 168 | /**  | 
            ||
| 169 | * Instance user.  | 
            ||
| 170 | *  | 
            ||
| 171 | * @param array $attributes  | 
            ||
| 172 | * @param Server $server  | 
            ||
| 173 | * @param Database $db  | 
            ||
| 174 | * @param LoggerInterface $logger  | 
            ||
| 175 | */  | 
            ||
| 176 | public function __construct(array $attributes, Server $server, Database $db, LoggerInterface $logger)  | 
            ||
| 186 | |||
| 187 | /**  | 
            ||
| 188 | * Return username as string.  | 
            ||
| 189 | *  | 
            ||
| 190 | * @return string  | 
            ||
| 191 | */  | 
            ||
| 192 | public function __toString(): string  | 
            ||
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * Update user with identity attributes.  | 
            ||
| 199 | *  | 
            ||
| 200 | * @param Identity $identity  | 
            ||
| 201 | *  | 
            ||
| 202 | * @return User  | 
            ||
| 203 | */  | 
            ||
| 204 | public function updateIdentity(Identity $identity): self  | 
            ||
| 240 | |||
| 241 | /**  | 
            ||
| 242 | * Set user attributes.  | 
            ||
| 243 | *  | 
            ||
| 244 | * @param array $attributes  | 
            ||
| 245 | *  | 
            ||
| 246 | * @return bool  | 
            ||
| 247 | */  | 
            ||
| 248 | public function setAttributes(array $attributes = []): bool  | 
            ||
| 258 | |||
| 259 | /**  | 
            ||
| 260 | * Get Attributes.  | 
            ||
| 261 | *  | 
            ||
| 262 | * @return array  | 
            ||
| 263 | */  | 
            ||
| 264 | public function getAttributes(): array  | 
            ||
| 281 | |||
| 282 | /**  | 
            ||
| 283 | * Find all shares with membership.  | 
            ||
| 284 | *  | 
            ||
| 285 | * @return array  | 
            ||
| 286 | */  | 
            ||
| 287 | public function getShares(): array  | 
            ||
| 306 | |||
| 307 | /**  | 
            ||
| 308 | * Get node attribute usage.  | 
            ||
| 309 | *  | 
            ||
| 310 | * @param array|string $attributes  | 
            ||
| 311 | * @param int $limit  | 
            ||
| 312 | *  | 
            ||
| 313 | * @return array  | 
            ||
| 314 | */  | 
            ||
| 315 | public function getNodeAttributeSummary($attributes = [], int $limit = 25): array  | 
            ||
| 343 | |||
| 344 | /**  | 
            ||
| 345 | * Get filesystem.  | 
            ||
| 346 | *  | 
            ||
| 347 | * @return Filesystem  | 
            ||
| 348 | */  | 
            ||
| 349 | public function getFilesystem(): Filesystem  | 
            ||
| 357 | |||
| 358 | /**  | 
            ||
| 359 | * Is Admin user?  | 
            ||
| 360 | *  | 
            ||
| 361 | * @return bool  | 
            ||
| 362 | */  | 
            ||
| 363 | public function isAdmin(): bool  | 
            ||
| 367 | |||
| 368 | /**  | 
            ||
| 369 | * Check if user has share.  | 
            ||
| 370 | *  | 
            ||
| 371 | * @param Collection $node  | 
            ||
| 372 | *  | 
            ||
| 373 | * @return bool  | 
            ||
| 374 | */  | 
            ||
| 375 | public function hasShare(Collection $node): bool  | 
            ||
| 385 | |||
| 386 | /**  | 
            ||
| 387 | * Find new shares and create reference.  | 
            ||
| 388 | *  | 
            ||
| 389 | * @return User  | 
            ||
| 390 | */  | 
            ||
| 391 | public function updateShares(): self  | 
            ||
| 501 | |||
| 502 | /**  | 
            ||
| 503 | * Get unique id.  | 
            ||
| 504 | *  | 
            ||
| 505 | * @return ObjectId  | 
            ||
| 506 | */  | 
            ||
| 507 | public function getId(): ObjectId  | 
            ||
| 511 | |||
| 512 | /**  | 
            ||
| 513 | * Get namespace.  | 
            ||
| 514 | *  | 
            ||
| 515 | * @return string  | 
            ||
| 516 | */  | 
            ||
| 517 | public function getNamespace(): ?string  | 
            ||
| 521 | |||
| 522 | /**  | 
            ||
| 523 | * Get hard quota.  | 
            ||
| 524 | *  | 
            ||
| 525 | * @return int  | 
            ||
| 526 | */  | 
            ||
| 527 | public function getHardQuota(): int  | 
            ||
| 531 | |||
| 532 | /**  | 
            ||
| 533 | * Set hard quota.  | 
            ||
| 534 | *  | 
            ||
| 535 | * @param int $quota In Bytes  | 
            ||
| 536 | *  | 
            ||
| 537 | * @return User  | 
            ||
| 538 | */  | 
            ||
| 539 | public function setHardQuota(int $quota): self  | 
            ||
| 546 | |||
| 547 | /**  | 
            ||
| 548 | * Set soft quota.  | 
            ||
| 549 | *  | 
            ||
| 550 | * @param int $quota In Bytes  | 
            ||
| 551 | *  | 
            ||
| 552 | * @return User  | 
            ||
| 553 | */  | 
            ||
| 554 | public function setSoftQuota(int $quota): self  | 
            ||
| 561 | |||
| 562 | /**  | 
            ||
| 563 | * Save.  | 
            ||
| 564 | *  | 
            ||
| 565 | * @param array $attributes  | 
            ||
| 566 | *  | 
            ||
| 567 | * @return bool  | 
            ||
| 568 | */  | 
            ||
| 569 | public function save(array $attributes = []): bool  | 
            ||
| 587 | |||
| 588 | /**  | 
            ||
| 589 | * Get used qota.  | 
            ||
| 590 | *  | 
            ||
| 591 | * @return array  | 
            ||
| 592 | */  | 
            ||
| 593 | public function getQuotaUsage(): array  | 
            ||
| 618 | |||
| 619 | /**  | 
            ||
| 620 | * Check quota.  | 
            ||
| 621 | *  | 
            ||
| 622 | * @param int $add Size in bytes  | 
            ||
| 623 | *  | 
            ||
| 624 | * @return bool  | 
            ||
| 625 | */  | 
            ||
| 626 | public function checkQuota(int $add): bool  | 
            ||
| 640 | |||
| 641 | /**  | 
            ||
| 642 | * Delete user.  | 
            ||
| 643 | *  | 
            ||
| 644 | * @param bool $force  | 
            ||
| 645 | *  | 
            ||
| 646 | * @return bool  | 
            ||
| 647 | */  | 
            ||
| 648 | public function delete(bool $force = false, bool $data = false, bool $force_data = false): bool  | 
            ||
| 667 | |||
| 668 | /**  | 
            ||
| 669 | * Undelete user.  | 
            ||
| 670 | *  | 
            ||
| 671 | * @return bool  | 
            ||
| 672 | */  | 
            ||
| 673 | public function undelete(): bool  | 
            ||
| 679 | |||
| 680 | /**  | 
            ||
| 681 | * Check if user is deleted.  | 
            ||
| 682 | *  | 
            ||
| 683 | * @return bool  | 
            ||
| 684 | */  | 
            ||
| 685 | public function isDeleted(): bool  | 
            ||
| 689 | |||
| 690 | /**  | 
            ||
| 691 | * Get Username.  | 
            ||
| 692 | *  | 
            ||
| 693 | * @return string  | 
            ||
| 694 | */  | 
            ||
| 695 | public function getUsername(): string  | 
            ||
| 699 | |||
| 700 | /**  | 
            ||
| 701 | * Get groups.  | 
            ||
| 702 | *  | 
            ||
| 703 | * @return array  | 
            ||
| 704 | */  | 
            ||
| 705 | public function getGroups(): array  | 
            ||
| 709 | |||
| 710 | /**  | 
            ||
| 711 | * Get resolved groups.  | 
            ||
| 712 | *  | 
            ||
| 713 | * @return Generator  | 
            ||
| 714 | */  | 
            ||
| 715 | public function getResolvedGroups(?int $offset = null, ?int $limit = null): ?Generator  | 
            ||
| 721 | |||
| 722 | /**  | 
            ||
| 723 | * Get attribute usage summary.  | 
            ||
| 724 | *  | 
            ||
| 725 | * @param string $attribute  | 
            ||
| 726 | * @param string $type  | 
            ||
| 727 | * @param int $limit  | 
            ||
| 728 | *  | 
            ||
| 729 | * @return array  | 
            ||
| 730 | */  | 
            ||
| 731 | protected function _getAttributeSummary(string $attribute, string $type = 'string', int $limit = 25): array  | 
            ||
| 773 | }  | 
            ||
| 774 | 
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto 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
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.