Complex classes like CoreQueryBuilder 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 CoreQueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 55 | class CoreQueryBuilder extends NC22ExtendedQueryBuilder { | ||
| 56 | |||
| 57 | |||
| 58 | use TArrayTools; | ||
| 59 | |||
| 60 | |||
| 61 | const SINGLE = 'single'; | ||
| 62 | const CIRCLE = 'circle'; | ||
| 63 | const MEMBER = 'member'; | ||
| 64 | const OWNER = 'owner'; | ||
| 65 | const REMOTE = 'remote'; | ||
| 66 | const BASED_ON = 'basedOn'; | ||
| 67 | const INITIATOR = 'initiator'; | ||
| 68 | const MEMBERSHIPS = 'memberships'; | ||
| 69 | const UPSTREAM_MEMBERSHIPS = 'upstreamMemberships'; | ||
| 70 | const INHERITANCE_FROM = 'inheritanceFrom'; | ||
| 71 | const INHERITED_BY = 'inheritedBy'; | ||
| 72 | const MOUNT = 'mount'; | ||
| 73 | const MOUNTPOINT = 'mountpoint'; | ||
| 74 | const SHARE = 'share'; | ||
| 75 | const FILE_CACHE = 'fileCache'; | ||
| 76 | const STORAGES = 'storages'; | ||
| 77 | const OPTIONS = 'options'; | ||
| 78 | |||
| 79 | |||
| 80 | public static $SQL_PATH = [ | ||
| 81 | self::SINGLE => [ | ||
| 82 | self::MEMBER | ||
| 83 | ], | ||
| 84 | self::CIRCLE => [ | ||
| 85 | self::OPTIONS => [ | ||
| 86 | 'getPersonalCircle' => true | ||
| 87 | ], | ||
| 88 | self::MEMBER, | ||
| 89 | self::OWNER => [ | ||
| 90 | self::BASED_ON | ||
| 91 | ], | ||
| 92 | self::MEMBERSHIPS, | ||
| 93 | self::INITIATOR => [ | ||
| 94 | self::BASED_ON, | ||
| 95 | self::INHERITED_BY => [ | ||
| 96 | self::MEMBERSHIPS | ||
| 97 | ] | ||
| 98 | ], | ||
| 99 | self::REMOTE => [ | ||
| 100 | self::MEMBER, | ||
| 101 | self::CIRCLE => [ | ||
| 102 | self::OWNER | ||
| 103 | ] | ||
| 104 | ] | ||
| 105 | ], | ||
| 106 | self::MEMBER => [ | ||
| 107 | self::MEMBERSHIPS, | ||
| 108 | self::INHERITANCE_FROM, | ||
| 109 | self::CIRCLE => [ | ||
| 110 | self::OPTIONS => [ | ||
| 111 | 'getData' => true | ||
| 112 | ], | ||
| 113 | self::OWNER, | ||
| 114 | self::MEMBERSHIPS, | ||
| 115 | self::INITIATOR => [ | ||
| 116 | self::OPTIONS => [ | ||
| 117 | 'mustBeMember' => true, | ||
| 118 | 'canBeVisitor' => false | ||
| 119 | ], | ||
| 120 | self::BASED_ON, | ||
| 121 | self::INHERITED_BY => [ | ||
| 122 | self::MEMBERSHIPS | ||
| 123 | ] | ||
| 124 | ] | ||
| 125 | ], | ||
| 126 | self::BASED_ON => [ | ||
| 127 | self::OWNER, | ||
| 128 | self::MEMBERSHIPS, | ||
| 129 | self::INITIATOR => [ | ||
| 130 | self::BASED_ON, | ||
| 131 | self::INHERITED_BY => [ | ||
| 132 | self::MEMBERSHIPS | ||
| 133 | ] | ||
| 134 | ] | ||
| 135 | ], | ||
| 136 | self::REMOTE => [ | ||
| 137 | self::MEMBER, | ||
| 138 | self::CIRCLE => [ | ||
| 139 | self::OWNER | ||
| 140 | ] | ||
| 141 | ] | ||
| 142 | ], | ||
| 143 | self::SHARE => [ | ||
| 144 | self::SHARE, | ||
| 145 | self::FILE_CACHE => [ | ||
| 146 | self::STORAGES | ||
| 147 | ], | ||
| 148 | self::UPSTREAM_MEMBERSHIPS => [ | ||
| 149 | self::MEMBERSHIPS, | ||
| 150 | self::INHERITED_BY => [ | ||
| 151 | self::BASED_ON | ||
| 152 | ], | ||
| 153 | self::SHARE, | ||
| 154 | ], | ||
| 155 | self::MEMBERSHIPS, | ||
| 156 | self::INHERITANCE_FROM, | ||
| 157 | self::INHERITED_BY => [ | ||
| 158 | self::BASED_ON | ||
| 159 | ], | ||
| 160 | self::CIRCLE => [ | ||
| 161 | self::OWNER | ||
| 162 | ], | ||
| 163 | self::INITIATOR => [ | ||
| 164 | self::BASED_ON, | ||
| 165 | self::INHERITED_BY => [ | ||
| 166 | self::MEMBERSHIPS | ||
| 167 | ] | ||
| 168 | ] | ||
| 169 | ], | ||
| 170 | self::REMOTE => [ | ||
| 171 | self::MEMBER | ||
| 172 | ], | ||
| 173 | self::MOUNT => [ | ||
| 174 | self::MEMBER => [ | ||
| 175 | self::REMOTE | ||
| 176 | ], | ||
| 177 | self::INITIATOR => [ | ||
| 178 | self::INHERITED_BY => [ | ||
| 179 | self::MEMBERSHIPS | ||
| 180 | ] | ||
| 181 | ], | ||
| 182 | self::MOUNTPOINT, | ||
| 183 | self::MEMBERSHIPS | ||
| 184 | ] | ||
| 185 | ]; | ||
| 186 | |||
| 187 | |||
| 188 | /** @var ConfigService */ | ||
| 189 | private $configService; | ||
| 190 | |||
| 191 | |||
| 192 | /** @var array */ | ||
| 193 | private $options = []; | ||
| 194 | |||
| 195 | /** | ||
| 196 | * CoreRequestBuilder constructor. | ||
| 197 | */ | ||
| 198 | 	public function __construct() { | ||
| 203 | |||
| 204 | |||
| 205 | /** | ||
| 206 | * @param IFederatedModel $federatedModel | ||
| 207 | * | ||
| 208 | * @return string | ||
| 209 | */ | ||
| 210 | 	public function getInstance(IFederatedModel $federatedModel): string { | ||
| 215 | |||
| 216 | |||
| 217 | /** | ||
| 218 | * @param string $id | ||
| 219 | */ | ||
| 220 | 	public function limitToCircleId(string $id): void { | ||
| 223 | |||
| 224 | /** | ||
| 225 | * @param string $name | ||
| 226 | */ | ||
| 227 | 	public function limitToName(string $name): void { | ||
| 230 | |||
| 231 | /** | ||
| 232 | * @param int $config | ||
| 233 | */ | ||
| 234 | 	public function limitToConfig(int $config): void { | ||
| 237 | |||
| 238 | /** | ||
| 239 | * @param int $source | ||
| 240 | */ | ||
| 241 | 	public function limitToSource(int $source): void { | ||
| 244 | |||
| 245 | /** | ||
| 246 | * @param int $config | ||
| 247 | */ | ||
| 248 | 	public function limitToConfigFlag(int $config): void { | ||
| 251 | |||
| 252 | |||
| 253 | /** | ||
| 254 | * @param string $singleId | ||
| 255 | */ | ||
| 256 | 	public function limitToSingleId(string $singleId): void { | ||
| 259 | |||
| 260 | |||
| 261 | /** | ||
| 262 | * @param string $itemId | ||
| 263 | */ | ||
| 264 | 	public function limitToItemId(string $itemId): void { | ||
| 267 | |||
| 268 | |||
| 269 | /** | ||
| 270 | * @param string $host | ||
| 271 | */ | ||
| 272 | 	public function limitToInstance(string $host): void { | ||
| 275 | |||
| 276 | |||
| 277 | /** | ||
| 278 | * @param int $userType | ||
| 279 | */ | ||
| 280 | 	public function limitToUserType(int $userType): void { | ||
| 283 | |||
| 284 | |||
| 285 | /** | ||
| 286 | * @param int $shareType | ||
| 287 | */ | ||
| 288 | 	public function limitToShareType(int $shareType): void { | ||
| 291 | |||
| 292 | |||
| 293 | /** | ||
| 294 | * @param string $shareWith | ||
| 295 | */ | ||
| 296 | 	public function limitToShareWith(string $shareWith): void { | ||
| 299 | |||
| 300 | |||
| 301 | /** | ||
| 302 | * @param int $nodeId | ||
| 303 | */ | ||
| 304 | 	public function limitToFileSource(int $nodeId): void { | ||
| 307 | |||
| 308 | /** | ||
| 309 | * @param array $files | ||
| 310 | */ | ||
| 311 | 	public function limitToFileSourceArray(array $files): void { | ||
| 314 | |||
| 315 | |||
| 316 | /** | ||
| 317 | * @param int $shareId | ||
| 318 | */ | ||
| 319 | 	public function limitToShareParent(int $shareId): void { | ||
| 322 | |||
| 323 | |||
| 324 | /** | ||
| 325 | * @param Circle $circle | ||
| 326 | */ | ||
| 327 | 	public function filterCircle(Circle $circle): void { | ||
| 336 | |||
| 337 | |||
| 338 | /** | ||
| 339 | * left join RemoteInstance based on a Member | ||
| 340 | */ | ||
| 341 | 	public function leftJoinRemoteInstance(string $alias): void { | ||
| 354 | |||
| 355 | |||
| 356 | /** | ||
| 357 | * @param string $alias | ||
| 358 | * @param RemoteInstance $remoteInstance | ||
| 359 | * @param bool $filterSensitiveData | ||
| 360 | * @param string $aliasCircle | ||
| 361 | * | ||
| 362 | * @throws RequestBuilderException | ||
| 363 | */ | ||
| 364 | public function limitToRemoteInstance( | ||
| 380 | |||
| 381 | |||
| 382 | /** | ||
| 383 | * Left join RemoteInstance based on an incoming request | ||
| 384 | * | ||
| 385 | * @param string $alias | ||
| 386 | * @param RemoteInstance $remoteInstance | ||
| 387 | * | ||
| 388 | * @throws RequestBuilderException | ||
| 389 | */ | ||
| 390 | public function leftJoinRemoteInstanceIncomingRequest( | ||
| 405 | |||
| 406 | |||
| 407 | /** | ||
| 408 | * left join members to check memberships of someone from instance | ||
| 409 | * | ||
| 410 | * @param string $alias | ||
| 411 | * @param RemoteInstance $remoteInstance | ||
| 412 | * @param string $aliasCircle | ||
| 413 | * | ||
| 414 | * @throws RequestBuilderException | ||
| 415 | */ | ||
| 416 | private function leftJoinMemberFromInstance( | ||
| 439 | |||
| 440 | |||
| 441 | /** | ||
| 442 | * left join circle is member of a circle from remote instance | ||
| 443 | * | ||
| 444 | * @param string $alias | ||
| 445 | * @param RemoteInstance $remoteInstance | ||
| 446 | * @param string $aliasCircle | ||
| 447 | * | ||
| 448 | * @throws RequestBuilderException | ||
| 449 | */ | ||
| 450 | private function leftJoinMemberFromRemoteCircle( | ||
| 486 | |||
| 487 | |||
| 488 | /** | ||
| 489 | * - global_scale: visibility on all Circles | ||
| 490 | * - trusted: visibility on all FEDERATED Circle if owner is local | ||
| 491 | * - external: visibility on all FEDERATED Circle if owner is local and: | ||
| 492 | * - with if Circle contains at least one member from the remote instance | ||
| 493 | * - one circle from the remote instance contains the local circle as member, and confirmed (using | ||
| 494 | * sync locally) | ||
| 495 | * - passive: like external, but the members list will only contains member from the local instance and | ||
| 496 | * from the remote instance. | ||
| 497 | * | ||
| 498 | * @param string $alias | ||
| 499 | * @param bool $sensitive | ||
| 500 | * @param string $aliasCircle | ||
| 501 | * | ||
| 502 | * @throws RequestBuilderException | ||
| 503 | */ | ||
| 504 | 	protected function limitRemoteVisibility(string $alias, bool $sensitive, string $aliasCircle) { | ||
| 553 | |||
| 554 | |||
| 555 | /** | ||
| 556 | * @param string $alias | ||
| 557 | * @param Member $member | ||
| 558 | * | ||
| 559 | * @throws RequestBuilderException | ||
| 560 | */ | ||
| 561 | 	public function limitToDirectMembership(string $alias, Member $member): void { | ||
| 580 | |||
| 581 | |||
| 582 | /** | ||
| 583 | * @param string $aliasMember | ||
| 584 | * @param Member $member | ||
| 585 | */ | ||
| 586 | 	public function filterDirectMembership(string $aliasMember, Member $member): void { | ||
| 622 | |||
| 623 | |||
| 624 | /** | ||
| 625 | * @param string $alias | ||
| 626 | * @param IFederatedUser|null $initiator | ||
| 627 | * @param string $field | ||
| 628 | * | ||
| 629 | * @throws RequestBuilderException | ||
| 630 | */ | ||
| 631 | public function leftJoinCircle( | ||
| 666 | |||
| 667 | |||
| 668 | /** | ||
| 669 | * @param string $aliasMember | ||
| 670 | * @param IFederatedUser|null $initiator | ||
| 671 | * | ||
| 672 | * @throws RequestBuilderException | ||
| 673 | */ | ||
| 674 | public function leftJoinBasedOn( | ||
| 700 | |||
| 701 | |||
| 702 | /** | ||
| 703 | * @param string $alias | ||
| 704 | * @param string $field | ||
| 705 | * | ||
| 706 | * @throws RequestBuilderException | ||
| 707 | */ | ||
| 708 | 	public function leftJoinOwner(string $alias, string $field = 'unique_id'): void { | ||
| 735 | |||
| 736 | |||
| 737 | /** | ||
| 738 | * @param string $alias | ||
| 739 | * @param string $fieldCircleId | ||
| 740 | * @param string $fieldSingleId | ||
| 741 | * | ||
| 742 | * @throws RequestBuilderException | ||
| 743 | */ | ||
| 744 | public function leftJoinMember( | ||
| 777 | |||
| 778 | |||
| 779 | /** | ||
| 780 | * if 'getData' is true, will returns 'inheritanceBy': the Member at the end of a sub-chain of | ||
| 781 | * memberships (based on $field for Top Circle's singleId) | ||
| 782 | * | ||
| 783 | * @param string $alias | ||
| 784 | * @param string $field | ||
| 785 | * @param string $aliasInheritedBy | ||
| 786 | * | ||
| 787 | * @throws RequestBuilderException | ||
| 788 | */ | ||
| 789 | public function leftJoinInheritedMembers(string $alias, string $field = '', string $aliasInheritedBy = '' | ||
| 819 | |||
| 820 | |||
| 821 | /** | ||
| 822 | * @throws RequestBuilderException | ||
| 823 | */ | ||
| 824 | 	public function limitToInheritedMemberships(string $alias, string $singleId, string $field = ''): void { | ||
| 840 | |||
| 841 | |||
| 842 | /** | ||
| 843 | * limit the request to Members and Sub Members of a Circle. | ||
| 844 | * | ||
| 845 | * @param string $alias | ||
| 846 | * @param string $singleId | ||
| 847 | * | ||
| 848 | * @throws RequestBuilderException | ||
| 849 | */ | ||
| 850 | 	public function limitToMembersByInheritance(string $alias, string $singleId): void { | ||
| 857 | |||
| 858 | |||
| 859 | /** | ||
| 860 | * if 'getData' is true, will returns 'inheritanceFrom': the Circle-As-Member of the Top Circle | ||
| 861 | * that explain the membership of a Member (based on $field for singleId) to a specific Circle | ||
| 862 | * | ||
| 863 | * // TODO: returns the link/path ? | ||
| 864 | * | ||
| 865 | * @param string $alias | ||
| 866 | * @param string $field | ||
| 867 | * | ||
| 868 | * @throws RequestBuilderException | ||
| 869 | */ | ||
| 870 | 	public function leftJoinMembersByInheritance(string $alias, string $field = ''): void { | ||
| 898 | |||
| 899 | |||
| 900 | /** | ||
| 901 | * limit the result to the point of view of a FederatedUser | ||
| 902 | * | ||
| 903 | * @param string $alias | ||
| 904 | * @param IFederatedUser $user | ||
| 905 | * @param string $field | ||
| 906 | * | ||
| 907 | * @throws RequestBuilderException | ||
| 908 | */ | ||
| 909 | 	public function limitToInitiator(string $alias, IFederatedUser $user, string $field = ''): void { | ||
| 918 | |||
| 919 | |||
| 920 | /** | ||
| 921 | * Left join members to filter userId as initiator. | ||
| 922 | * | ||
| 923 | * @param string $alias | ||
| 924 | * @param IFederatedUser $initiator | ||
| 925 | * @param string $field | ||
| 926 | * | ||
| 927 | * @throws RequestBuilderException | ||
| 928 | */ | ||
| 929 | 	public function leftJoinInitiator(string $alias, IFederatedUser $initiator, string $field = ''): void { | ||
| 990 | |||
| 991 | |||
| 992 | /** | ||
| 993 | * @param string $alias | ||
| 994 | * | ||
| 995 | * @throws RequestBuilderException | ||
| 996 | */ | ||
| 997 | 	protected function limitInitiatorVisibility(string $alias) { | ||
| 1051 | |||
| 1052 | |||
| 1053 | /** | ||
| 1054 | * CFG_SINGLE, CFG_HIDDEN and CFG_BACKEND means hidden from listing. | ||
| 1055 | * | ||
| 1056 | * @param string $aliasCircle | ||
| 1057 | * @param int $flag | ||
| 1058 | */ | ||
| 1059 | public function filterCircles( | ||
| 1077 | |||
| 1078 | |||
| 1079 | /** | ||
| 1080 | * Limit visibility on Sensitive information when search for members. | ||
| 1081 | * | ||
| 1082 | * @param string $alias | ||
| 1083 | * | ||
| 1084 | * @return ICompositeExpression | ||
| 1085 | */ | ||
| 1086 | 	private function limitRemoteVisibility_Sensitive_Members(string $alias = 'ri'): ICompositeExpression { | ||
| 1112 | |||
| 1113 | |||
| 1114 | /** | ||
| 1115 | * | ||
| 1116 | * @param string $aliasCircle | ||
| 1117 | * @param int $flag | ||
| 1118 | */ | ||
| 1119 | 	public function filterConfig(string $aliasCircle, int $flag): void { | ||
| 1122 | |||
| 1123 | |||
| 1124 | /** | ||
| 1125 | * Link to storage/filecache | ||
| 1126 | * | ||
| 1127 | * @param string $aliasShare | ||
| 1128 | * | ||
| 1129 | * @throws RequestBuilderException | ||
| 1130 | */ | ||
| 1131 | 	public function leftJoinFileCache(string $aliasShare) { | ||
| 1153 | |||
| 1154 | |||
| 1155 | /** | ||
| 1156 | * @param string $aliasShare | ||
| 1157 | * @param string $aliasShareMemberships | ||
| 1158 | * | ||
| 1159 | * @throws RequestBuilderException | ||
| 1160 | */ | ||
| 1161 | 	public function leftJoinShareChild(string $aliasShare, string $aliasShareMemberships = '') { | ||
| 1181 | |||
| 1182 | |||
| 1183 | /** | ||
| 1184 | * @param string $alias | ||
| 1185 | * @param FederatedUser $federatedUser | ||
| 1186 | * @param bool $reshares | ||
| 1187 | */ | ||
| 1188 | 	public function limitToShareOwner(string $alias, FederatedUser $federatedUser, bool $reshares): void { | ||
| 1203 | |||
| 1204 | |||
| 1205 | /** | ||
| 1206 | * @param string $aliasMount | ||
| 1207 | * @param string $aliasMountMemberships | ||
| 1208 | * | ||
| 1209 | * @throws RequestBuilderException | ||
| 1210 | */ | ||
| 1211 | 	public function leftJoinMountpoint(string $aliasMount, string $aliasMountMemberships = '') { | ||
| 1230 | |||
| 1231 | |||
| 1232 | /** | ||
| 1233 | * @param string $alias | ||
| 1234 | * @param array $default | ||
| 1235 | * | ||
| 1236 | * @return CoreQueryBuilder | ||
| 1237 | */ | ||
| 1238 | 	private function generateCircleSelectAlias(string $alias, array $default = []): self { | ||
| 1248 | |||
| 1249 | /** | ||
| 1250 | * @param string $alias | ||
| 1251 | * @param array $default | ||
| 1252 | * | ||
| 1253 | * @return $this | ||
| 1254 | */ | ||
| 1255 | 	private function generateMemberSelectAlias(string $alias, array $default = []): self { | ||
| 1265 | |||
| 1266 | |||
| 1267 | /** | ||
| 1268 | * @param string $alias | ||
| 1269 | * @param array $default | ||
| 1270 | * @param string $prefix | ||
| 1271 | * | ||
| 1272 | * @return $this | ||
| 1273 | */ | ||
| 1274 | private function generateMembershipSelectAlias( | ||
| 1287 | |||
| 1288 | |||
| 1289 | /** | ||
| 1290 | * @param string $alias | ||
| 1291 | * @param array $default | ||
| 1292 | * | ||
| 1293 | * @return $this | ||
| 1294 | */ | ||
| 1295 | 	private function generateRemoteInstanceSelectAlias(string $alias, array $default = []): self { | ||
| 1302 | |||
| 1303 | |||
| 1304 | /** | ||
| 1305 | * @param array $path | ||
| 1306 | * @param array $options | ||
| 1307 | */ | ||
| 1308 | 	public function setOptions(array $path, array $options): void { | ||
| 1316 | |||
| 1317 | |||
| 1318 | /** | ||
| 1319 | * @param string $base | ||
| 1320 | * @param string $extension | ||
| 1321 | * @param array|null $options | ||
| 1322 | * | ||
| 1323 | * @return string | ||
| 1324 | * @throws RequestBuilderException | ||
| 1325 | */ | ||
| 1326 | 	public function generateAlias(string $base, string $extension, ?array &$options = []): string { | ||
| 1350 | |||
| 1351 | |||
| 1352 | /** | ||
| 1353 | * @param string $prefix | ||
| 1354 | * | ||
| 1355 | * @return array | ||
| 1356 | */ | ||
| 1357 | 	public function getAvailablePath(string $prefix): array { | ||
| 1373 | |||
| 1374 | } | ||
| 1375 | |||
| 1376 |