| Total Complexity | 74 |
| Total Lines | 594 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
Complex classes like AppRecordSet 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.
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 AppRecordSet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class AppRecordSet extends ORMRecordSet implements AppObjectInterface |
||
| 49 | { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var Func_App |
||
| 53 | */ |
||
| 54 | protected $app = null; |
||
| 55 | |||
| 56 | protected $accessRights = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var AppAccessManager |
||
| 60 | */ |
||
| 61 | private $accessManager = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private $customFields = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param Func_App $app |
||
| 70 | */ |
||
| 71 | public function __construct(Func_App $app) |
||
| 72 | { |
||
| 73 | parent::__construct(); |
||
| 74 | $this->setApp($app); |
||
| 75 | $this->accessRights = array(); |
||
| 76 | |||
| 77 | $this->setAccessManager($app->AccessManager()); |
||
| 78 | $this->setPrimaryKey('id'); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | * @see AppObjectInterface::setApp() |
||
| 84 | */ |
||
| 85 | public function setApp(Func_App $app) |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | * @see AppObjectInterface::App() |
||
| 94 | */ |
||
| 95 | public function App() |
||
| 96 | { |
||
| 97 | return $this->app; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $setName |
||
| 102 | */ |
||
| 103 | protected function trimSetName($setName) |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Similar to the ORMRecordSet::join() method but instanciates the |
||
| 117 | * joined RecordSet using App methods. |
||
| 118 | * |
||
| 119 | * @see ORMRecordSet::join() |
||
| 120 | */ |
||
| 121 | public function join($fkFieldName) |
||
| 122 | { |
||
| 123 | $fkField = $this->getField($fkFieldName); |
||
| 124 | if(! ($fkField instanceof ORMFkField)){ |
||
| 125 | return $this; |
||
| 126 | } |
||
| 127 | $setName = $fkField->getForeignSetName(); |
||
| 128 | |||
| 129 | if(! $setName || 'Set' === $setName){ |
||
| 130 | throw new \Exception('The set name is missing on foreign key field ' . $fkFieldName); |
||
| 131 | } |
||
| 132 | |||
| 133 | $appSetName = $this->trimSetName($setName); |
||
| 134 | $set = $this->App()->$appSetName(); |
||
| 135 | $set->setName($fkField->getName()); |
||
| 136 | $set->setDescription($fkField->getDescription()); |
||
| 137 | |||
| 138 | $this->aField[$fkFieldName] = $set; |
||
| 139 | $set->setParentSet($this); |
||
| 140 | return $this; |
||
| 141 | } |
||
| 142 | |||
| 143 | // /** |
||
| 144 | // * |
||
| 145 | // * @param string $accessName |
||
| 146 | // * @param string $type |
||
| 147 | // */ |
||
| 148 | // public function addAccessRight($accessName, $type = 'acl') |
||
| 149 | // { |
||
| 150 | // $this->accessRights[$accessName] = $type; |
||
| 151 | // } |
||
| 152 | |||
| 153 | // /** |
||
| 154 | // * @return array |
||
| 155 | // */ |
||
| 156 | // public function getAccessRights() |
||
| 157 | // { |
||
| 158 | // return $this->accessRights; |
||
| 159 | // } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return AppCustomField[] |
||
| 163 | */ |
||
| 164 | public function getCustomFields() |
||
| 165 | { |
||
| 166 | $App = $this->App(); |
||
| 167 | |||
| 168 | if(null === $this->customFields){ |
||
| 169 | $this->customFields = array(); |
||
| 170 | |||
| 171 | if(isset($App->CustomField)){ |
||
| 172 | $customFieldSet = $App->CustomFieldSet(); |
||
| 173 | $object = mb_substr(get_class($this), mb_strlen($App->classPrefix), - mb_strlen('Set')); |
||
| 174 | try{ |
||
| 175 | $customFields = $customFieldSet->select($customFieldSet->object->is($object)); |
||
| 176 | |||
| 177 | foreach ($customFields as $customfield){ |
||
| 178 | $this->customFields[] = $customfield; |
||
| 179 | |||
| 180 | /*@var $customfield AppCustomField */ |
||
| 181 | } |
||
| 182 | } |
||
| 183 | catch (ORMBackEndSelectException $e){ |
||
| 184 | // table does not exist, this error is thrown by the install program while creating the sets |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | return $this->customFields; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return AppCustomField[] |
||
| 194 | */ |
||
| 195 | public function selectCustomFields() |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return self |
||
| 209 | */ |
||
| 210 | public function addCustomFields() |
||
| 211 | { |
||
| 212 | $customFields = $this->selectCustomFields(); |
||
| 213 | foreach ($customFields as $customField){ |
||
| 214 | /*@var $customField AppCustomField */ |
||
| 215 | $description = $customField->name; |
||
| 216 | $ormField = $customField->getORMField()->setDescription($description); |
||
| 217 | if($ormField instanceof ORMFkField){ |
||
| 218 | $this->hasOne($customField->fieldname, $ormField->getForeignSetName()) |
||
| 219 | ->setDescription($description); |
||
| 220 | } |
||
| 221 | else{ |
||
| 222 | $this->addFields($ormField); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | return $this; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Similar to ORMRecordSet::get() method but throws a AppNotFoundException if the |
||
| 231 | * record is not found. |
||
| 232 | * |
||
| 233 | * @since 1.0.18 |
||
| 234 | * @throws ORMException:: |
||
| 235 | * @throws AppNotFoundException |
||
| 236 | * |
||
| 237 | * @param ORMCriteria|string $mixedParam |
||
| 238 | * Criteria for selecting records |
||
| 239 | * or the value for selecting record |
||
| 240 | * @param string $sPropertyName |
||
| 241 | * The name of the property on which |
||
| 242 | * the value applies. If not |
||
| 243 | * specified or null, the set's |
||
| 244 | * primary key will be used. |
||
| 245 | * @return AppRecord |
||
| 246 | */ |
||
| 247 | public function request($mixedParam = null, $sPropertyName = null) |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Returns an iterator of traceableRecord linked to the specified source, |
||
| 265 | * optionally filtered on the specified link type. |
||
| 266 | * |
||
| 267 | * @param |
||
| 268 | * AppRecord | array $source source can be an array of record |
||
| 269 | * @param string $linkType |
||
| 270 | * |
||
| 271 | * @return ORMIterator |
||
| 272 | */ |
||
| 273 | public function selectLinkedTo($source, $linkType = null) |
||
| 274 | { |
||
| 275 | $linkSet = $this->App()->LinkSet(); |
||
| 276 | |||
| 277 | $targetClass = $this->getRecordClassName(); |
||
| 278 | if(strpos($targetClass, '\\')){ |
||
| 279 | $targetClass = (new \ReflectionClass($targetClass))->getShortName(); |
||
| 280 | } |
||
| 281 | |||
| 282 | if(is_array($source) || ($source instanceof \Iterator)){ |
||
| 283 | return $linkSet->selectForSources($source, $targetClass, $linkType); |
||
| 284 | } |
||
| 285 | else{ |
||
| 286 | return $linkSet->selectForSource($source, $targetClass, $linkType); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns an iterator of traceableRecord linked to the specified target, |
||
| 292 | * optionally filtered on the specified link type. |
||
| 293 | * |
||
| 294 | * @param |
||
| 295 | * AppRecord | array $target target can be an array of record |
||
| 296 | * @param string $linkType |
||
| 297 | * |
||
| 298 | * @return ORMIterator |
||
| 299 | */ |
||
| 300 | public function selectLinkedFrom($target, $linkType = null) |
||
| 301 | { |
||
| 302 | $linkSet = $this->App()->LinkSet(); |
||
| 303 | |||
| 304 | $sourceClass = $this->getRecordClassName(); |
||
| 305 | if(strpos($sourceClass, '\\')){ |
||
| 306 | $sourceClass = (new \ReflectionClass($sourceClass))->getShortName(); |
||
| 307 | } |
||
| 308 | |||
| 309 | if(is_array($target) || ($target instanceof \Iterator)){ |
||
| 310 | return $linkSet->selectForTargets($target, $sourceClass, $linkType); |
||
| 311 | } |
||
| 312 | else{ |
||
| 313 | return $linkSet->selectForTarget($target, $sourceClass, $linkType); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns a criteria usable to select records of this set which are source of app_Links to the specified AppRecord. |
||
| 319 | * |
||
| 320 | * @param AppRecord $target |
||
| 321 | * @param string $linkType |
||
| 322 | * @return ORMCriteria |
||
| 323 | */ |
||
| 324 | public function isSourceOf(AppRecord $target, $linkType = null) |
||
| 325 | { |
||
| 326 | $App = $this->App(); |
||
| 327 | $linkSet = $App->LinkSet(); |
||
| 328 | |||
| 329 | $recordClassName = $this->getRecordClassName(); |
||
| 330 | if(strpos($recordClassName, '\\')){ |
||
| 331 | $recordClassName = (new \ReflectionClass($recordClassName))->getShortName(); |
||
| 332 | } |
||
| 333 | $linkSet->hasOne('sourceId', $recordClassName); |
||
| 334 | |||
| 335 | $targetClass = $target->getClassName(); |
||
| 336 | if(strpos($targetClass, '\\')){ |
||
| 337 | $targetClass = (new \ReflectionClass($targetClass))->getShortName(); |
||
| 338 | } |
||
| 339 | |||
| 340 | $criteria = $linkSet->targetClass->is($targetClass) |
||
| 341 | ->_AND_($linkSet->targetId->is($target->id)) |
||
| 342 | ->_AND_($linkSet->sourceClass->is($this->newRecord() |
||
| 343 | ->getClassName())); |
||
| 344 | if(isset($linkType)){ |
||
| 345 | if(is_array($linkType)){ |
||
| 346 | $criteria = $criteria->_AND_($linkSet->type->in($linkType)); |
||
| 347 | } |
||
| 348 | else{ |
||
| 349 | $criteria = $criteria->_AND_($linkSet->type->is($linkType)); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | $criteria = $this->id->in($criteria, 'sourceId'); |
||
| 353 | |||
| 354 | return $criteria; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Returns a criteria usable to select records of this set which are target of app_Links to the specified AppRecord. |
||
| 359 | * |
||
| 360 | * @param AppRecord $source |
||
| 361 | * @param null|string|string[] $linkType |
||
| 362 | * @return ORMCriteria |
||
| 363 | */ |
||
| 364 | public function isTargetOf(AppRecord $source, $linkType = null) |
||
| 365 | { |
||
| 366 | $App = $this->App(); |
||
| 367 | $linkSet = $App->LinkSet(); |
||
| 368 | |||
| 369 | $recordClassName = $this->getRecordClassName(); |
||
| 370 | if(strpos($recordClassName, '\\')){ |
||
| 371 | $recordClassName = (new \ReflectionClass($recordClassName))->getShortName(); |
||
| 372 | } |
||
| 373 | $linkSet->hasOne('targetId', $recordClassName); |
||
| 374 | |||
| 375 | $sourceClass = $source->getClassName(); |
||
| 376 | if(strpos($sourceClass, '\\')){ |
||
| 377 | $sourceClass = new \ReflectionClass($sourceClass); |
||
| 378 | } |
||
| 379 | |||
| 380 | $targetClass = $this->newRecord()->getClassName(); |
||
| 381 | if(strpos($targetClass, '\\')){ |
||
| 382 | $targetClass = new \ReflectionClass($targetClass); |
||
| 383 | } |
||
| 384 | |||
| 385 | $criteria = $linkSet->sourceClass->is($sourceClass) |
||
| 386 | ->_AND_($linkSet->sourceId->is($source->id)) |
||
| 387 | ->_AND_($linkSet->targetClass->is($targetClass)); |
||
| 388 | if(isset($linkType)){ |
||
| 389 | if(is_array($linkType)){ |
||
| 390 | $criteria = $criteria->_AND_($linkSet->type->in($linkType)); |
||
| 391 | } |
||
| 392 | else{ |
||
| 393 | $criteria = $criteria->_AND_($linkSet->type->is($linkType)); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | $criteria = $this->id->in($criteria, 'targetId'); |
||
| 397 | |||
| 398 | return $criteria; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Returns a criteria usable to select records of this set which are target of app_Links from the specified AppRecords. |
||
| 403 | * |
||
| 404 | * @since 1.0.23 |
||
| 405 | * @param AppRecord[] $sources |
||
| 406 | * @param null|string|string[] $linkType |
||
| 407 | * @return ORMCriteria |
||
| 408 | */ |
||
| 409 | public function isTargetOfAny($sources, $linkType = null) |
||
| 410 | { |
||
| 411 | $App = $this->App(); |
||
| 412 | $linkSet = $App->LinkSet(); |
||
| 413 | $recordClassName = $linkSet->getRecordClassName(); |
||
| 414 | if(strpos($recordClassName, '\\')){ |
||
| 415 | $recordClassName = (new \ReflectionClass($recordClassName))->getShortName(); |
||
| 416 | } |
||
| 417 | $linkSet->hasOne('targetId', $recordClassName); |
||
| 418 | |||
| 419 | $sourceIdsByClasses = array(); |
||
| 420 | foreach ($sources as $source){ |
||
| 421 | $sourceClass = $source->getClassName(); |
||
| 422 | if(strpos($sourceClass, '\\')){ |
||
| 423 | $sourceClass = (new \ReflectionClass($sourceClass))->getShortName(); |
||
| 424 | } |
||
| 425 | if(! isset($sourceIdsByClasses[$sourceClass])){ |
||
| 426 | $sourceIdsByClasses[$sourceClass] = array(); |
||
| 427 | } |
||
| 428 | $sourceIdsByClasses[$sourceClass][] = $source->id; |
||
| 429 | } |
||
| 430 | |||
| 431 | $sourcesCriteria = array(); |
||
| 432 | foreach ($sourceIdsByClasses as $sourceClass => $sourceIds){ |
||
| 433 | $sourcesCriteria[] = $linkSet->sourceClass->is($sourceClass)->_AND_($linkSet->sourceId->in($sourceIds)); |
||
| 434 | } |
||
| 435 | |||
| 436 | $targetClass = $this->newRecord()->getClassName(); |
||
| 437 | if(strpos($targetClass, '\\')){ |
||
| 438 | $targetClass = (new \ReflectionClass($targetClass))->getShortName(); |
||
| 439 | } |
||
| 440 | |||
| 441 | $criteria = $linkSet->all($linkSet->targetClass->is($targetClass), $linkSet->any($sourcesCriteria)); |
||
| 442 | if(isset($linkType)){ |
||
| 443 | if(is_array($linkType)){ |
||
| 444 | $criteria = $criteria->_AND_($linkSet->type->in($linkType)); |
||
| 445 | } |
||
| 446 | else{ |
||
| 447 | $criteria = $criteria->_AND_($linkSet->type->is($linkType)); |
||
| 448 | } |
||
| 449 | } |
||
| 450 | $criteria = $this->id->in($criteria, 'targetId'); |
||
| 451 | |||
| 452 | return $criteria; |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Returns a criteria usable to select records of this set which are source of app_Links to the specified AppRecords. |
||
| 457 | * |
||
| 458 | * @since 1.0.23 |
||
| 459 | * @param AppRecord[] $targets |
||
| 460 | * @param string|null $linkType |
||
| 461 | * @return ORMCriteria |
||
| 462 | */ |
||
| 463 | public function isSourceOfAny($targets, $linkType = null) |
||
| 464 | { |
||
| 465 | $App = $this->App(); |
||
| 466 | $linkSet = $App->LinkSet(); |
||
| 467 | $recordClassName = $this->getRecordClassName(); |
||
| 468 | if(strpos($recordClassName, '\\')){ |
||
| 469 | $recordClassName = (new \ReflectionClass($recordClassName))->getShortName(); |
||
| 470 | } |
||
| 471 | $linkSet->hasOne('sourceId', $recordClassName); |
||
| 472 | |||
| 473 | $targetIdsByClasses = array(); |
||
| 474 | foreach ($targets as $target){ |
||
| 475 | $targetClass = $target->getClassName(); |
||
| 476 | if(strpos($targetClass, '\\')){ |
||
| 477 | $targetClass = (new \ReflectionClass($targetClass))->getShortName(); |
||
| 478 | } |
||
| 479 | if(! isset($targetIdsByClasses[$targetClass])){ |
||
| 480 | $targetIdsByClasses[$targetClass] = array(); |
||
| 481 | } |
||
| 482 | $targetIdsByClasses[$targetClass][] = $target->id; |
||
| 483 | } |
||
| 484 | |||
| 485 | $targetsCriteria = array(); |
||
| 486 | foreach ($targetIdsByClasses as $targetClass => $targetIds){ |
||
| 487 | $targetsCriteria[] = $linkSet->targetClass->is($targetClass)->_AND_($linkSet->targetId->in($targetIds)); |
||
| 488 | } |
||
| 489 | |||
| 490 | $sourceClass = $this->newRecord()->getClassName(); |
||
| 491 | if(strpos($sourceClass, '\\')){ |
||
| 492 | $sourceClass = (new \ReflectionClass($sourceClass))->getShortName(); |
||
| 493 | } |
||
| 494 | |||
| 495 | $criteria = $linkSet->all($linkSet->sourceClass->is($sourceClass), $linkSet->any($targetsCriteria)); |
||
| 496 | if(isset($linkType)){ |
||
| 497 | if(is_array($linkType)){ |
||
| 498 | $criteria = $criteria->_AND_($linkSet->type->in($linkType)); |
||
| 499 | } |
||
| 500 | else{ |
||
| 501 | $criteria = $criteria->_AND_($linkSet->type->is($linkType)); |
||
| 502 | } |
||
| 503 | } |
||
| 504 | $criteria = $this->id->in($criteria, 'sourceId'); |
||
| 505 | |||
| 506 | return $criteria; |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @param AppAccessManager $accessManager |
||
| 511 | * @return self |
||
| 512 | */ |
||
| 513 | public function setAccessManager(AppAccessManager $accessManager) |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @return AppAccessManager |
||
| 521 | */ |
||
| 522 | public function getAccessManager() |
||
| 523 | { |
||
| 524 | return $this->accessManager; |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Defines if records can be created by the current user. |
||
| 529 | * |
||
| 530 | * @return boolean |
||
| 531 | */ |
||
| 532 | public function isCreatable() |
||
| 533 | { |
||
| 534 | return false; |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @return ORMCriteria |
||
| 539 | */ |
||
| 540 | public function isReadable() |
||
| 541 | { |
||
| 542 | return $this->hasAccess('read'); |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @return ORMCriteria |
||
| 547 | */ |
||
| 548 | public function isUpdatable() |
||
| 549 | { |
||
| 550 | return $this->hasAccess('update'); |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @return ORMCriteria |
||
| 555 | */ |
||
| 556 | public function isDeletable() |
||
| 557 | { |
||
| 558 | return $this->hasAccess('delete'); |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Returns a criterion matching records that can be put to trash by the current user. |
||
| 563 | * |
||
| 564 | * @return ORMCriterion |
||
| 565 | */ |
||
| 566 | public function isRemovable() |
||
| 567 | { |
||
| 568 | return $this->isUpdatable(); |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Returns a criterion matching records that can be restored from the trash by the current user. |
||
| 573 | * |
||
| 574 | * @return ORMCriterion |
||
| 575 | */ |
||
| 576 | public function isRestorable() |
||
| 577 | { |
||
| 578 | return $this->isUpdatable(); |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Returns a criterion matching records deletable by the current user. |
||
| 583 | * |
||
| 584 | * @param string $accessType |
||
| 585 | * @param int|null $user |
||
| 586 | * |
||
| 587 | * @return ORMCriterion |
||
| 588 | */ |
||
| 589 | public function hasAccess($accessType, $user = null) |
||
| 590 | { |
||
| 591 | $accessManager = $this->getAccessManager(); |
||
| 592 | return $accessManager->getAccessCriterion($this, $accessType, $user); |
||
| 593 | } |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Returns a criteria usable to select records of this set associated to the specified tags. |
||
| 597 | * |
||
| 598 | * @array $tags An array of tag ids |
||
| 599 | * @string $type The link type [optional] |
||
| 600 | * @return ORMCriteria |
||
| 601 | */ |
||
| 602 | public function haveTagLabels($tagLabels, $linkType = null) |
||
| 632 | } |
||
| 633 | |||
| 634 | public function getVisibilityCriteriaFields() |
||
| 635 | { |
||
| 636 | $fields = $this->getFields(); |
||
| 637 | $manyRelations = $this->getHasManyRelations(); |
||
| 638 | foreach ($manyRelations as $fieldName => $manyRelation){ |
||
| 639 | $fields[$fieldName] = $manyRelation; |
||
| 640 | } |
||
| 641 | return $fields; |
||
| 642 | } |
||
| 643 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths