Complex classes like OwnerTree 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 OwnerTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 9 | class OwnerTree implements OwnerTreeInterface | ||
| 10 | { | ||
| 11 | /** | ||
| 12 | * An associative array to store owning organization of an user | ||
| 13 | * key = userId | ||
| 14 | * value = organizationId | ||
| 15 | * | ||
| 16 | * @var array | ||
| 17 | */ | ||
| 18 | protected $userOwningOrganizationId; | ||
| 19 | |||
| 20 | /** | ||
| 21 | * An associative array to store owning organization of a business unit | ||
| 22 | * key = businessUnitId | ||
| 23 | * value = organizationId | ||
| 24 | * | ||
| 25 | * @var array | ||
| 26 | */ | ||
| 27 | protected $businessUnitOwningOrganizationId; | ||
| 28 | |||
| 29 | /** | ||
| 30 | * An associative array to store owning business unit of an user | ||
| 31 | * key = userId | ||
| 32 | * value = businessUnitId | ||
| 33 | * | ||
| 34 | * @var array | ||
| 35 | */ | ||
| 36 | protected $userOwningBusinessUnitId; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * An associative array to store organizations assigned to an user | ||
| 40 | * key = userId | ||
| 41 | * value = array of organizationId | ||
| 42 | * | ||
| 43 | * @var array | ||
| 44 | */ | ||
| 45 | protected $userOrganizationIds; | ||
| 46 | |||
| 47 | /** | ||
| 48 | * An associative array to store business units assigned to an user | ||
| 49 | * key = userId | ||
| 50 | * value = array of businessUnitId | ||
| 51 | * | ||
| 52 | * @var array | ||
| 53 | */ | ||
| 54 | protected $userBusinessUnitIds; | ||
| 55 | |||
| 56 | /** | ||
| 57 | * An associative array to store business units assigned to an user through organizations | ||
| 58 | * key = userId | ||
| 59 | * value = array: | ||
| 60 | * key = organizationId | ||
| 61 | * value = array of businessUnitIds | ||
| 62 | * | ||
| 63 | * @var array | ||
| 64 | */ | ||
| 65 | protected $userOrganizationBusinessUnitIds; | ||
| 66 | |||
| 67 | /** | ||
| 68 | * An associative array to store subordinate business units | ||
| 69 | * key = businessUnitId | ||
| 70 | * value = array of businessUnitId | ||
| 71 | * | ||
| 72 | * @var array | ||
| 73 | */ | ||
| 74 | protected $subordinateBusinessUnitIds; | ||
| 75 | |||
| 76 | /** | ||
| 77 | * An associative array to store users belong to a business unit | ||
| 78 | * key = businessUnitId | ||
| 79 | * value = array of userId | ||
| 80 | * | ||
| 81 | * @var array | ||
| 82 | */ | ||
| 83 | protected $businessUnitUserIds; | ||
| 84 | |||
| 85 | /** | ||
| 86 | * An associative array to store users belong to a assigned business unit | ||
| 87 | * key = businessUnitId | ||
| 88 | * value = array of userId | ||
| 89 | * | ||
| 90 | * @var array | ||
| 91 | */ | ||
| 92 | protected $assignedBusinessUnitUserIds; | ||
| 93 | |||
| 94 | /** | ||
| 95 | * An associative array to store business units belong to an organization | ||
| 96 | * key = organizationId | ||
| 97 | * value = array of businessUnitId | ||
| 98 | * | ||
| 99 | * @var array | ||
| 100 | */ | ||
| 101 | protected $organizationBusinessUnitIds; | ||
| 102 | |||
| 103 | /** | ||
| 104 | * An associative array to store users belong to an organization | ||
| 105 | * key = organizationId | ||
| 106 | * value = array of userId | ||
| 107 | * | ||
| 108 | * @var array | ||
| 109 | */ | ||
| 110 | protected $organizationUserIds; | ||
| 111 | |||
| 112 | public function __construct() | ||
| 116 | |||
| 117 | /** | ||
| 118 | * The __set_state handler | ||
| 119 | * | ||
| 120 | * @param array $data Initialization array | ||
| 121 | * @return OwnerTree A new instance of a OwnerTree object | ||
| 122 | */ | ||
| 123 | // @codingStandardsIgnoreStart | ||
| 124 | public static function __set_state($data) | ||
| 125 |     { | ||
| 126 | $result = new OwnerTree(); | ||
| 127 |         foreach ($data as $key => $val) { | ||
| 128 |             $result->{$key} = $val; | ||
| 129 | } | ||
| 130 | |||
| 131 | return $result; | ||
| 132 | } | ||
| 133 | // @codingStandardsIgnoreEnd | ||
| 134 | |||
| 135 | /** | ||
| 136 | * Gets the owning organization id for the given user id | ||
| 137 | * | ||
| 138 | * @param int|string $userId | ||
| 139 | * @return int|string|null | ||
| 140 | */ | ||
| 141 | public function getUserOrganizationId($userId) | ||
| 142 |     { | ||
| 143 | return isset($this->userOwningOrganizationId[$userId]) | ||
| 144 | ? $this->userOwningOrganizationId[$userId] | ||
| 145 | : null; | ||
| 146 | } | ||
| 147 | |||
| 148 | /** | ||
| 149 | * Gets all organization ids assigned to the given user id | ||
| 150 | * | ||
| 151 | * @param int|string $userId | ||
| 152 | * @return int|string|null | ||
| 153 | */ | ||
| 154 | public function getUserOrganizationIds($userId) | ||
| 155 |     { | ||
| 156 | return isset($this->userOrganizationIds[$userId]) | ||
| 157 | ? $this->userOrganizationIds[$userId] | ||
| 158 | : []; | ||
| 159 | } | ||
| 160 | |||
| 161 | /** | ||
| 162 | * Gets the owning business unit id for the given user id | ||
| 163 | * | ||
| 164 | * @param int|string $userId | ||
| 165 | * @return int|string|null | ||
| 166 | */ | ||
| 167 | public function getUserBusinessUnitId($userId) | ||
| 168 |     { | ||
| 169 | return isset($this->userOwningBusinessUnitId[$userId]) | ||
| 170 | ? $this->userOwningBusinessUnitId[$userId] | ||
| 171 | : null; | ||
| 172 | } | ||
| 173 | |||
| 174 | /** | ||
| 175 | * Gets all business unit ids assigned to the given user id | ||
| 176 | * | ||
| 177 | * @param int|string $userId | ||
| 178 | * @param int|string|null $organizationId | ||
| 179 | * @return array of int|string | ||
| 180 | */ | ||
| 181 | public function getUserBusinessUnitIds($userId, $organizationId = null) | ||
| 193 | |||
| 194 | /** | ||
| 195 | * Gets all users ids for the given business unit id | ||
| 196 | * | ||
| 197 | * @param int|string $businessUnitId | ||
| 198 | * @return array of int|string | ||
| 199 | */ | ||
| 200 | public function getBusinessUnitUserIds($businessUnitId) | ||
| 206 | |||
| 207 | /** | ||
| 208 | * Gets the owning organization id for the given business unit id | ||
| 209 | * | ||
| 210 | * @param int|string $businessUnitId | ||
| 211 | * @return int|string|null | ||
| 212 | */ | ||
| 213 | public function getBusinessUnitOrganizationId($businessUnitId) | ||
| 219 | |||
| 220 | /** | ||
| 221 | * Gets all business unit ids for the given organization id | ||
| 222 | * | ||
| 223 | * @param int|string $organizationId | ||
| 224 | * @return array of int|string | ||
| 225 | */ | ||
| 226 | public function getOrganizationBusinessUnitIds($organizationId) | ||
| 232 | |||
| 233 | /** | ||
| 234 | * Gets all user ids for the given organization id | ||
| 235 | * | ||
| 236 | * @param int|string $organizationId | ||
| 237 | * @return array of int|string | ||
| 238 | */ | ||
| 239 | public function getOrganizationUserIds($organizationId) | ||
| 252 | |||
| 253 | /** | ||
| 254 | * Gets all subordinate business unit ids for the given business unit id | ||
| 255 | * | ||
| 256 | * @param int|string $businessUnitId | ||
| 257 | * @return array of int|string | ||
| 258 | */ | ||
| 259 | public function getSubordinateBusinessUnitIds($businessUnitId) | ||
| 265 | |||
| 266 | /** | ||
| 267 | * Gets all user business unit ids with subordinate business unit ids | ||
| 268 | * | ||
| 269 | * @param int $userId | ||
| 270 | * @param int|string $organizationId | ||
| 271 | * @return array of int|string | ||
| 272 | */ | ||
| 273 | public function getUserSubordinateBusinessUnitIds($userId, $organizationId = null) | ||
| 274 |     { | ||
| 275 | $buIds = $this->getUserBusinessUnitIds($userId, $organizationId); | ||
| 276 | $resultBuIds = array_merge($buIds, []); | ||
| 277 |         foreach ($buIds as $buId) { | ||
| 278 | $diff = array_diff( | ||
| 279 | $this->getSubordinateBusinessUnitIds($buId), | ||
| 280 | $resultBuIds | ||
| 281 | ); | ||
| 282 |             if ($diff) { | ||
|  | |||
| 283 | $resultBuIds = array_merge($resultBuIds, $diff); | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | return $resultBuIds; | ||
| 288 | } | ||
| 289 | |||
| 290 | /** | ||
| 291 | * Gets all user business unit ids by user organization ids | ||
| 292 | * | ||
| 293 | * @param int $userId | ||
| 294 | * @return array of int|string | ||
| 295 | */ | ||
| 296 | public function getBusinessUnitsIdByUserOrganizations($userId) | ||
| 309 | |||
| 310 | /** | ||
| 311 | * Get all business units in system | ||
| 312 | * | ||
| 313 | * @return array | ||
| 314 | */ | ||
| 315 | public function getAllBusinessUnitIds() | ||
| 327 | |||
| 328 | /** | ||
| 329 | * Add the given business unit to the tree | ||
| 330 | * | ||
| 331 | * @param int|string $businessUnitId | ||
| 332 | * @param int|string|null $owningOrganizationId | ||
| 333 | * | ||
| 334 | * @deprecated 1.8.0:2.1.0 use OwnerTree::addLocalEntity method | ||
| 335 | */ | ||
| 336 | public function addBusinessUnit($businessUnitId, $owningOrganizationId) | ||
| 340 | |||
| 341 | /** | ||
| 342 |      * {@inheritdoc} | ||
| 343 | */ | ||
| 344 | public function addLocalEntity($localLevelEntityId, $globalLevelEntityId = null) | ||
| 363 | |||
| 364 | /** | ||
| 365 | * Add a business unit relation to the tree | ||
| 366 | * | ||
| 367 | * @param int|string $businessUnitId | ||
| 368 | * @param int|string|null $parentBusinessUnitId | ||
| 369 | * | ||
| 370 | * @deprecated 1.8.0:2.1.0 use OwnerTree::addDeepEntity method | ||
| 371 | */ | ||
| 372 | public function addBusinessUnitRelation($businessUnitId, $parentBusinessUnitId) | ||
| 376 | |||
| 377 | /** | ||
| 378 |      * {@inheritdoc} | ||
| 379 | */ | ||
| 380 | public function addDeepEntity($localLevelEntityId, $deepLevelEntityId) | ||
| 390 | |||
| 391 | /** | ||
| 392 |      * {@inheritdoc} | ||
| 393 | */ | ||
| 394 | public function buildTree() | ||
| 423 | |||
| 424 | /** | ||
| 425 | * Add the given user to the tree | ||
| 426 | * | ||
| 427 | * @param int|string $userId | ||
| 428 | * @param int|string|null $owningBusinessUnitId | ||
| 429 | * | ||
| 430 | * @deprecated 1.8.0:2.1.0 use OwnerTree::addBasicEntity method | ||
| 431 | */ | ||
| 432 | public function addUser($userId, $owningBusinessUnitId) | ||
| 436 | |||
| 437 | /** | ||
| 438 |      * {@inheritdoc} | ||
| 439 | */ | ||
| 440 | public function addBasicEntity($basicLevelEntityId, $localLevelEntityId = null) | ||
| 464 | |||
| 465 | /** | ||
| 466 | * @param $buId | ||
| 467 | * @return array | ||
| 468 | */ | ||
| 469 | public function getUsersAssignedToBU($buId) | ||
| 475 | |||
| 476 | /** | ||
| 477 | * Add a business unit to the given user | ||
| 478 | * | ||
| 479 | * @param int|string $userId | ||
| 480 | * @param int|string|null $organizationId | ||
| 481 | * @param int|string $businessUnitId | ||
| 482 | * @throws \LogicException | ||
| 483 | * | ||
| 484 | * @deprecated 1.8.0:2.1.0 use OwnerTree::addLocalEntityToBasic method | ||
| 485 | */ | ||
| 486 | public function addUserBusinessUnit($userId, $organizationId, $businessUnitId) | ||
| 490 | |||
| 491 | /** | ||
| 492 | * Pay attention that now local level entity is second and global entity is third | ||
| 493 | * | ||
| 494 |      * {@inheritdoc} | ||
| 495 | */ | ||
| 496 | public function addLocalEntityToBasic($basicLevelEntityId, $localLevelEntityId, $globalLevelEntityId) | ||
| 517 | |||
| 518 | /** | ||
| 519 | * Add a organization to the given user | ||
| 520 | * | ||
| 521 | * @param int|string $userId | ||
| 522 | * @param int|string $organizationId | ||
| 523 | * | ||
| 524 | * @deprecated 1.8.0:2.1.0 use OwnerTree::addGlobalEntity method | ||
| 525 | */ | ||
| 526 | public function addUserOrganization($userId, $organizationId) | ||
| 530 | |||
| 531 | /** | ||
| 532 |      * {@inheritdoc} | ||
| 533 | */ | ||
| 534 | public function addGlobalEntity($basicLevelEntityId, $globalLevelEntityId) | ||
| 538 | |||
| 539 | /** | ||
| 540 | * Removes all elements from the tree | ||
| 541 | */ | ||
| 542 | public function clear() | ||
| 555 | } | ||
| 556 | 
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.