| Total Complexity | 61 |
| Total Lines | 542 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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) |
||
| 86 | { |
||
| 87 | $this->app = $app; |
||
| 88 | return $this; |
||
| 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) |
||
| 104 | { |
||
| 105 | if(strpos($setName, '_') === false){ |
||
| 106 | return $setName; |
||
| 107 | } |
||
| 108 | $pos = strrpos($setName, '\\'); |
||
| 109 | if($pos === false){ |
||
| 110 | return explode('_', $setName)[1]; |
||
| 111 | } |
||
| 112 | return substr($setName, $pos + 1); |
||
| 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() |
||
| 196 | { |
||
| 197 | $App = $this->App(); |
||
| 198 | |||
| 199 | $customFieldSet = $App->CustomFieldSet(); |
||
| 200 | $object = mb_substr(get_class($this), mb_strlen($App->classPrefix), - mb_strlen('Set')); |
||
| 201 | |||
| 202 | $customFields = $customFieldSet->select($customFieldSet->object->is($object)); |
||
| 203 | |||
| 204 | return $customFields; |
||
| 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) |
||
| 248 | { |
||
| 249 | $record = $this->get($mixedParam, $sPropertyName); |
||
| 250 | if(! isset($record)){ |
||
| 251 | // This will remove the default criteria for TraceableRecords and |
||
| 252 | // fetch even 'deleted' ones. |
||
| 253 | $this->setDefaultCriteria(null); |
||
| 254 | $record = $this->get($mixedParam, $sPropertyName); |
||
| 255 | if(isset($record)){ |
||
| 256 | throw new AppDeletedRecordException($record, $mixedParam); |
||
| 257 | } |
||
| 258 | throw new AppNotFoundException($this, $mixedParam); |
||
| 259 | } |
||
| 260 | return $record; |
||
| 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 | if(is_array($source) || ($source instanceof \Iterator)){ |
||
| 277 | return $linkSet->selectForSources($source, $this->getRecordClassName(), $linkType); |
||
| 278 | } |
||
| 279 | else{ |
||
| 280 | return $linkSet->selectForSource($source, $this->getRecordClassName(), $linkType); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Returns an iterator of traceableRecord linked to the specified target, |
||
| 286 | * optionally filtered on the specified link type. |
||
| 287 | * |
||
| 288 | * @param |
||
| 289 | * AppRecord | array $target target can be an array of record |
||
| 290 | * @param string $linkType |
||
| 291 | * |
||
| 292 | * @return ORMIterator |
||
| 293 | */ |
||
| 294 | public function selectLinkedFrom($target, $linkType = null) |
||
| 295 | { |
||
| 296 | $linkSet = $this->App()->LinkSet(); |
||
| 297 | if(is_array($target) || ($target instanceof \Iterator)){ |
||
| 298 | return $linkSet->selectForTargets($target, $this->getRecordClassName(), $linkType); |
||
| 299 | } |
||
| 300 | else{ |
||
| 301 | return $linkSet->selectForTarget($target, $this->getRecordClassName(), $linkType); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns a criteria usable to select records of this set which are source of app_Links to the specified AppRecord. |
||
| 307 | * |
||
| 308 | * @param AppRecord $target |
||
| 309 | * @param string $linkType |
||
| 310 | * @return ORMCriteria |
||
| 311 | */ |
||
| 312 | public function isSourceOf(AppRecord $target, $linkType = null) |
||
| 313 | { |
||
| 314 | $App = $this->App(); |
||
| 315 | $linkSet = $App->LinkSet(); |
||
| 316 | |||
| 317 | $recordClassName = $this->getRecordClassName(); |
||
| 318 | $linkSet->hasOne('sourceId', $recordClassName); |
||
| 319 | |||
| 320 | $criteria = $linkSet->targetClass->is($target->getClassName()) |
||
| 321 | ->_AND_($linkSet->targetId->is($target->id)) |
||
| 322 | ->_AND_($linkSet->sourceClass->is($this->newRecord() |
||
| 323 | ->getClassName())); |
||
| 324 | if(isset($linkType)){ |
||
| 325 | if(is_array($linkType)){ |
||
| 326 | $criteria = $criteria->_AND_($linkSet->type->in($linkType)); |
||
| 327 | } |
||
| 328 | else{ |
||
| 329 | $criteria = $criteria->_AND_($linkSet->type->is($linkType)); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | $criteria = $this->id->in($criteria); |
||
| 333 | |||
| 334 | return $criteria; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Returns a criteria usable to select records of this set which are target of app_Links to the specified AppRecord. |
||
| 339 | * |
||
| 340 | * @param AppRecord $source |
||
| 341 | * @param null|string|string[] $linkType |
||
| 342 | * @return ORMCriteria |
||
| 343 | */ |
||
| 344 | public function isTargetOf(AppRecord $source, $linkType = null) |
||
| 345 | { |
||
| 346 | $App = $this->App(); |
||
| 347 | $linkSet = $App->LinkSet(); |
||
| 348 | |||
| 349 | $recordClassName = $this->getRecordClassName(); |
||
| 350 | $linkSet->hasOne('targetId', $recordClassName); |
||
| 351 | |||
| 352 | $criteria = $linkSet->sourceClass->is($source->getClassName()) |
||
| 353 | ->_AND_($linkSet->sourceId->is($source->id)) |
||
| 354 | ->_AND_($linkSet->targetClass->is($this->newRecord() |
||
| 355 | ->getClassName())); |
||
| 356 | if(isset($linkType)){ |
||
| 357 | if(is_array($linkType)){ |
||
| 358 | $criteria = $criteria->_AND_($linkSet->type->in($linkType)); |
||
| 359 | } |
||
| 360 | else{ |
||
| 361 | $criteria = $criteria->_AND_($linkSet->type->is($linkType)); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | $criteria = $this->id->in($criteria); |
||
| 365 | |||
| 366 | return $criteria; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Returns a criteria usable to select records of this set which are target of app_Links from the specified AppRecords. |
||
| 371 | * |
||
| 372 | * @since 1.0.23 |
||
| 373 | * @param AppRecord[] $sources |
||
| 374 | * @param null|string|string[] $linkType |
||
| 375 | * @return ORMCriteria |
||
| 376 | */ |
||
| 377 | public function isTargetOfAny($sources, $linkType = null) |
||
| 378 | { |
||
| 379 | $App = $this->App(); |
||
| 380 | $linkSet = $App->LinkSet(); |
||
| 381 | $recordClassName = $linkSet->getRecordClassName(); |
||
| 382 | $linkSet->hasOne('targetId', $recordClassName); |
||
| 383 | |||
| 384 | $sourceIdsByClasses = array(); |
||
| 385 | foreach ($sources as $source){ |
||
| 386 | $sourceClass = $source->getClassName(); |
||
| 387 | if(! isset($sourceIdsByClasses[$sourceClass])){ |
||
| 388 | $sourceIdsByClasses[$sourceClass] = array(); |
||
| 389 | } |
||
| 390 | $sourceIdsByClasses[$sourceClass][] = $source->id; |
||
| 391 | } |
||
| 392 | |||
| 393 | $sourcesCriteria = array(); |
||
| 394 | foreach ($sourceIdsByClasses as $sourceClass => $sourceIds){ |
||
| 395 | $sourcesCriteria[] = $linkSet->sourceClass->is($sourceClass)->_AND_($linkSet->sourceId->in($sourceIds)); |
||
| 396 | } |
||
| 397 | |||
| 398 | $criteria = $linkSet->all($linkSet->targetClass->is($this->newRecord() |
||
| 399 | ->getClassName()), $linkSet->any($sourcesCriteria)); |
||
| 400 | if(isset($linkType)){ |
||
| 401 | if(is_array($linkType)){ |
||
| 402 | $criteria = $criteria->_AND_($linkSet->type->in($linkType)); |
||
| 403 | } |
||
| 404 | else{ |
||
| 405 | $criteria = $criteria->_AND_($linkSet->type->is($linkType)); |
||
| 406 | } |
||
| 407 | } |
||
| 408 | $criteria = $this->id->in($criteria); |
||
| 409 | |||
| 410 | return $criteria; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Returns a criteria usable to select records of this set which are source of app_Links to the specified AppRecords. |
||
| 415 | * |
||
| 416 | * @since 1.0.23 |
||
| 417 | * @param AppRecord[] $targets |
||
| 418 | * @param string|null $linkType |
||
| 419 | * @return ORMCriteria |
||
| 420 | */ |
||
| 421 | public function isSourceOfAny($targets, $linkType = null) |
||
| 422 | { |
||
| 423 | $App = $this->App(); |
||
| 424 | $linkSet = $App->LinkSet(); |
||
| 425 | $recordClassName = $this->getRecordClassName(); |
||
| 426 | $linkSet->hasOne('sourceId', $recordClassName); |
||
| 427 | |||
| 428 | $targetIdsByClasses = array(); |
||
| 429 | foreach ($targets as $target){ |
||
| 430 | $targetClass = $target->getClassName(); |
||
| 431 | if(! isset($targetIdsByClasses[$targetClass])){ |
||
| 432 | $targetIdsByClasses[$targetClass] = array(); |
||
| 433 | } |
||
| 434 | $targetIdsByClasses[$targetClass][] = $target->id; |
||
| 435 | } |
||
| 436 | |||
| 437 | $targetsCriteria = array(); |
||
| 438 | foreach ($targetIdsByClasses as $targetClass => $targetIds){ |
||
| 439 | $targetsCriteria[] = $linkSet->targetClass->is($targetClass)->_AND_($linkSet->targetId->in($targetIds)); |
||
| 440 | } |
||
| 441 | |||
| 442 | $criteria = $linkSet->all($linkSet->sourceClass->is($this->newRecord() |
||
| 443 | ->getClassName()), $linkSet->any($targetsCriteria)); |
||
| 444 | if(isset($linkType)){ |
||
| 445 | if(is_array($linkType)){ |
||
| 446 | $criteria = $criteria->_AND_($linkSet->type->in($linkType)); |
||
| 447 | } |
||
| 448 | else{ |
||
| 449 | $criteria = $criteria->_AND_($linkSet->type->is($linkType)); |
||
| 450 | } |
||
| 451 | } |
||
| 452 | $criteria = $this->id->in($criteria); |
||
| 453 | |||
| 454 | return $criteria; |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param AppAccessManager $accessManager |
||
| 459 | * @return self |
||
| 460 | */ |
||
| 461 | public function setAccessManager(AppAccessManager $accessManager) |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @return AppAccessManager |
||
| 469 | */ |
||
| 470 | public function getAccessManager() |
||
| 471 | { |
||
| 472 | return $this->accessManager; |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Defines if records can be created by the current user. |
||
| 477 | * |
||
| 478 | * @return boolean |
||
| 479 | */ |
||
| 480 | public function isCreatable() |
||
| 481 | { |
||
| 482 | return false; |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @return ORMCriteria |
||
| 487 | */ |
||
| 488 | public function isReadable() |
||
| 489 | { |
||
| 490 | return $this->hasAccess('read'); |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @return ORMCriteria |
||
| 495 | */ |
||
| 496 | public function isUpdatable() |
||
| 497 | { |
||
| 498 | return $this->hasAccess('update'); |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @return ORMCriteria |
||
| 503 | */ |
||
| 504 | public function isDeletable() |
||
| 505 | { |
||
| 506 | return $this->hasAccess('delete'); |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Returns a criterion matching records that can be put to trash by the current user. |
||
| 511 | * |
||
| 512 | * @return ORMCriterion |
||
| 513 | */ |
||
| 514 | public function isRemovable() |
||
| 515 | { |
||
| 516 | return $this->isUpdatable(); |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Returns a criterion matching records that can be restored from the trash by the current user. |
||
| 521 | * |
||
| 522 | * @return ORMCriterion |
||
| 523 | */ |
||
| 524 | public function isRestorable() |
||
| 525 | { |
||
| 526 | return $this->isUpdatable(); |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Returns a criterion matching records deletable by the current user. |
||
| 531 | * |
||
| 532 | * @param string $accessType |
||
| 533 | * @param int|null $user |
||
| 534 | * |
||
| 535 | * @return ORMCriterion |
||
| 536 | */ |
||
| 537 | public function hasAccess($accessType, $user = null) |
||
| 538 | { |
||
| 539 | $accessManager = $this->getAccessManager(); |
||
| 540 | return $accessManager->getAccessCriterion($this, $accessType, $user); |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Returns a criteria usable to select records of this set associated to the specified tags. |
||
| 545 | * |
||
| 546 | * @array $tags An array of tag ids |
||
| 547 | * @string $type The link type [optional] |
||
| 548 | * @return ORMCriteria |
||
| 549 | */ |
||
| 550 | public function haveTagLabels($tagLabels, $linkType = null) |
||
| 580 | } |
||
| 581 | |||
| 582 | public function getVisibilityCriteriaFields() |
||
| 583 | { |
||
| 584 | $fields = $this->getFields(); |
||
| 585 | $manyRelations = $this->getHasManyRelations(); |
||
| 586 | foreach ($manyRelations as $fieldName => $manyRelation){ |
||
| 587 | $fields[$fieldName] = $manyRelation; |
||
| 588 | } |
||
| 589 | return $fields; |
||
| 590 | } |
||
| 591 | } |
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