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