| Total Complexity | 49 |
| Total Lines | 493 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like app_Record 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 app_Record, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 353 | class app_Record extends ORM_Record implements app_Object_Interface |
||
| 354 | { |
||
| 355 | /** |
||
| 356 | * @var Func_App |
||
| 357 | */ |
||
| 358 | protected $app = null; |
||
| 359 | |||
| 360 | |||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * Returns the value of the specified field or an iterator if $sFieldName represents a 'Many relation'. |
||
| 365 | * |
||
| 366 | * @param string $sFieldName The name of the field or relation for which the value must be returned. |
||
| 367 | * @param array $args Optional arguments. |
||
| 368 | * |
||
| 369 | * @return mixed The value of the field or null if the field is not a part of the record. |
||
| 370 | */ |
||
| 371 | public function __call($sFieldName, $args) |
||
| 372 | { |
||
| 373 | $value = $this->oParentSet->getBackend()->getRecordValue($this, $sFieldName); |
||
| 374 | $field = $this->oParentSet->$sFieldName; |
||
| 375 | if (!is_null($value) && $field instanceof ORM_FkField) { |
||
| 376 | |||
| 377 | $sClassName = $field->getForeignSetName(); |
||
| 378 | |||
| 379 | $App = $this->App(); |
||
| 380 | $prefixLength = mb_strlen($App->classPrefix); |
||
| 381 | $methodName = mb_substr($sClassName, $prefixLength); |
||
| 382 | $set = $App->$methodName(); |
||
| 383 | |||
| 384 | $set->setName($field->getName()); |
||
| 385 | $set->setDescription($field->getDescription()); |
||
| 386 | |||
| 387 | $record = $set->get($value); |
||
| 388 | return $record; |
||
| 389 | } |
||
| 390 | return $value; |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * {@inheritDoc} |
||
| 395 | * @see app_Object_Interface::setApp() |
||
| 396 | */ |
||
| 397 | public function setApp(Func_App $app) |
||
| 398 | { |
||
| 399 | $this->app = $app; |
||
| 400 | return $this; |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get APP object to use with this record |
||
| 405 | * |
||
| 406 | * @return Func_App |
||
| 407 | */ |
||
| 408 | public function App() |
||
| 409 | { |
||
| 410 | if (!isset($this->app)) { |
||
| 411 | // If the app object was not specified (through the setApp() method), |
||
| 412 | // we set it as parent set's App. |
||
| 413 | $this->setApp($this->getParentSet()->App()); |
||
| 414 | } |
||
| 415 | return $this->app; |
||
| 416 | } |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * Returns the base class name of a record. |
||
| 421 | * For example xxx_Contact will return 'Contact'. |
||
| 422 | * |
||
| 423 | * @since 1.0.40 |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function getClassName() |
||
| 427 | { |
||
| 428 | list(, $classname) = explode('_', get_class($this)); |
||
| 429 | return $classname; |
||
| 430 | } |
||
| 431 | |||
| 432 | |||
| 433 | /** |
||
| 434 | * Returns the string reference corresponding to the record. |
||
| 435 | * |
||
| 436 | * @return string A reference string (e.g. Contact:12) |
||
| 437 | */ |
||
| 438 | public function getRef() |
||
| 439 | { |
||
| 440 | if (!isset($this->id)) { |
||
| 441 | throw new app_Exception('Trying to get the reference string of a record without an id.'); |
||
| 442 | } |
||
| 443 | $classname = $this->getClassName(); |
||
| 444 | return $classname . ':' . $this->id; |
||
| 445 | } |
||
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * @return app_Controller |
||
| 450 | */ |
||
| 451 | public function getController() |
||
| 452 | { |
||
| 453 | $App = $this->App(); |
||
| 454 | |||
| 455 | $ctrlName = $this->getClassName(); |
||
| 456 | return $App->Controller()->$ctrlName(); |
||
| 457 | } |
||
| 458 | |||
| 459 | |||
| 460 | /** |
||
| 461 | * Deletes the record with respect to referential integrity. |
||
| 462 | * |
||
| 463 | * Uses referential integrity as defined by hasManyRelation to delete/update |
||
| 464 | * referenced elements. |
||
| 465 | * |
||
| 466 | * @see app_RecordSet::hasMany() |
||
| 467 | * |
||
| 468 | * @return self |
||
| 469 | */ |
||
| 470 | public function delete($deletedStatus = null) |
||
| 471 | { |
||
| 472 | $App = $this->App(); |
||
| 473 | |||
| 474 | if (!isset($deletedStatus)) { |
||
| 475 | $deletedStatus = app_TraceableRecord::DELETED_STATUS_DELETED; |
||
| 476 | } |
||
| 477 | |||
| 478 | $set = $this->getParentSet(); |
||
| 479 | $recordIdName = $set->getPrimaryKey(); |
||
| 480 | $recordId = $this->$recordIdName; |
||
| 481 | |||
| 482 | // Uses referential integrity as defined by hasManyRelation to delete/update |
||
| 483 | // referenced elements. |
||
| 484 | $manyRelations = $set->getHasManyRelations(); |
||
| 485 | |||
| 486 | |||
| 487 | foreach ($manyRelations as $manyRelation) { |
||
| 488 | /* @var $manyRelation ORM_ManyRelation */ |
||
| 489 | |||
| 490 | $foreignSetClassName = $manyRelation->getForeignSetClassName(); |
||
| 491 | $foreignSetFieldName = $manyRelation->getForeignFieldName(); |
||
| 492 | $method = mb_substr($foreignSetClassName, mb_strlen($App->classPrefix)); |
||
| 493 | $foreignSet = $App->$method(); |
||
| 494 | |||
| 495 | switch ($manyRelation->getOnDeleteMethod()) { |
||
| 496 | |||
| 497 | case ORM_ManyRelation::ON_DELETE_SET_NULL: |
||
| 498 | |||
| 499 | $foreignRecords = $foreignSet->select($foreignSet->$foreignSetFieldName->is($recordId)); |
||
| 500 | |||
| 501 | foreach ($foreignRecords as $foreignRecord) { |
||
| 502 | $foreignRecord->$foreignSetFieldName = 0; |
||
| 503 | $foreignRecord->save(); |
||
| 504 | } |
||
| 505 | |||
| 506 | break; |
||
| 507 | |||
| 508 | case ORM_ManyRelation::ON_DELETE_CASCADE: |
||
| 509 | |||
| 510 | $foreignRecords = $foreignSet->select($foreignSet->$foreignSetFieldName->is($recordId)); |
||
| 511 | |||
| 512 | foreach ($foreignRecords as $foreignRecord) { |
||
| 513 | $foreignRecord->delete(); |
||
| 514 | } |
||
| 515 | |||
| 516 | break; |
||
| 517 | |||
| 518 | case ORM_ManyRelation::ON_DELETE_NO_ACTION: |
||
| 519 | default: |
||
| 520 | break; |
||
| 521 | |||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | |||
| 526 | // We remove all links to and from this record. |
||
| 527 | $linkSet = $App->LinkSet(); |
||
| 528 | |||
| 529 | $linkSet->delete( |
||
| 530 | $linkSet->sourceClass->is(get_class($this))->_AND_($linkSet->sourceId->is($recordId)) |
||
| 531 | ->_OR_( |
||
| 532 | $linkSet->targetClass->is(get_class($this))->_AND_($linkSet->targetId->is($recordId)) |
||
| 533 | ) |
||
| 534 | ); |
||
| 535 | |||
| 536 | |||
| 537 | $set->delete($set->$recordIdName->is($recordId), $deletedStatus); |
||
| 538 | |||
| 539 | return $this; |
||
| 540 | } |
||
| 541 | |||
| 542 | |||
| 543 | |||
| 544 | /** |
||
| 545 | * Reassociates all data asociated to the record to another |
||
| 546 | * specified one. |
||
| 547 | * |
||
| 548 | * @param int $id |
||
| 549 | * |
||
| 550 | * @return self |
||
| 551 | */ |
||
| 552 | public function replaceWith($id) |
||
| 553 | { |
||
| 554 | $App = $this->App(); |
||
| 555 | |||
| 556 | $set = $this->getParentSet(); |
||
| 557 | $recordIdName = $set->getPrimaryKey(); |
||
| 558 | $recordId = $this->$recordIdName; |
||
| 559 | |||
| 560 | // Use referential integrity as defined by hasManyRelation to delete/update |
||
| 561 | // referenced elements. |
||
| 562 | $manyRelations = $set->getHasManyRelations(); |
||
| 563 | |||
| 564 | |||
| 565 | foreach ($manyRelations as $manyRelation) { |
||
| 566 | /* @var $manyRelation ORM_ManyRelation */ |
||
| 567 | |||
| 568 | $foreignSetClassName = $manyRelation->getForeignSetClassName(); |
||
| 569 | $foreignSetFieldName = $manyRelation->getForeignFieldName(); |
||
| 570 | $method = mb_substr($foreignSetClassName, mb_strlen($App->classPrefix)); |
||
| 571 | $foreignSet = $App->$method(); |
||
| 572 | // $foreignSet = new $foreignSetClassName($App); |
||
| 573 | $foreignRecords = $foreignSet->select($foreignSet->$foreignSetFieldName->is($recordId)); |
||
| 574 | |||
| 575 | foreach ($foreignRecords as $foreignRecord) { |
||
| 576 | $foreignRecord->$foreignSetFieldName = $id; |
||
| 577 | $foreignRecord->save(); |
||
| 578 | } |
||
| 579 | } |
||
| 580 | |||
| 581 | |||
| 582 | // We replace all links to and from this record. |
||
| 583 | $linkSet = $App->LinkSet(); |
||
| 584 | |||
| 585 | $links = $linkSet->select( |
||
| 586 | $linkSet->sourceClass->is(get_class($this))->_AND_($linkSet->sourceId->is($recordId)) |
||
| 587 | ); |
||
| 588 | |||
| 589 | foreach ($links as $link) { |
||
| 590 | $link->sourceId = $id; |
||
| 591 | $link->save(); |
||
| 592 | } |
||
| 593 | |||
| 594 | $links = $linkSet->select( |
||
| 595 | $linkSet->targetClass->is(get_class($this))->_AND_($linkSet->targetId->is($recordId)) |
||
| 596 | ); |
||
| 597 | |||
| 598 | foreach ($links as $link) { |
||
| 599 | $link->targetId = $id; |
||
| 600 | $link->save(); |
||
| 601 | } |
||
| 602 | |||
| 603 | return $this; |
||
| 604 | } |
||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * |
||
| 609 | * |
||
| 610 | * @return array |
||
| 611 | */ |
||
| 612 | public function getRelatedRecords() |
||
| 613 | { |
||
| 614 | $App = $this->App(); |
||
| 615 | |||
| 616 | $set = $this->getParentSet(); |
||
| 617 | $recordIdName = $set->getPrimaryKey(); |
||
| 618 | $recordId = $this->$recordIdName; |
||
| 619 | |||
| 620 | // Use referential integrity as defined by hasManyRelation to delete/update |
||
| 621 | // referenced elements. |
||
| 622 | $manyRelations = $set->getHasManyRelations(); |
||
| 623 | |||
| 624 | $relatedRecords = array(); |
||
| 625 | |||
| 626 | foreach ($manyRelations as $manyRelation) { |
||
| 627 | /* @var $manyRelation ORM_ManyRelation */ |
||
| 628 | |||
| 629 | $foreignSetClassName = $manyRelation->getForeignSetClassName(); |
||
| 630 | $foreignSetFieldName = $manyRelation->getForeignFieldName(); |
||
| 631 | |||
| 632 | $method = mb_substr($foreignSetClassName, mb_strlen($App->classPrefix)); |
||
| 633 | $foreignSet = $App->$method(); |
||
| 634 | // $foreignSet = new $foreignSetClassName($App); |
||
| 635 | $foreignRecords = $foreignSet->select($foreignSet->$foreignSetFieldName->is($recordId)); |
||
| 636 | |||
| 637 | |||
| 638 | if ($foreignRecords->count() > 0) { |
||
| 639 | $relatedRecords[$foreignSetClassName] = $foreignRecords; |
||
| 640 | } |
||
| 641 | } |
||
| 642 | |||
| 643 | return $relatedRecords; |
||
| 644 | } |
||
| 645 | |||
| 646 | |||
| 647 | |||
| 648 | |||
| 649 | |||
| 650 | |||
| 651 | /** |
||
| 652 | * Upload path for record attachments |
||
| 653 | * |
||
| 654 | * @return bab_Path |
||
| 655 | */ |
||
| 656 | public function uploadPath() |
||
| 657 | { |
||
| 658 | $path = $this->App()->uploadPath(); |
||
| 659 | |||
| 660 | if (null === $path) |
||
| 661 | { |
||
| 662 | throw new Exception('Missing upload path information'); |
||
| 663 | return null; |
||
| 664 | } |
||
| 665 | |||
| 666 | $path->push(get_class($this)); |
||
| 667 | $path->push($this->id); |
||
| 668 | |||
| 669 | return $path; |
||
| 670 | } |
||
| 671 | |||
| 672 | |||
| 673 | |||
| 674 | |||
| 675 | |||
| 676 | |||
| 677 | /** |
||
| 678 | * import a value into a traceable record property if the value is not equal |
||
| 679 | * |
||
| 680 | * @param string $name property name |
||
| 681 | * @param mixed $value value to set |
||
| 682 | * |
||
| 683 | * @return int 1 : the value has been modified | 0 : no change |
||
| 684 | */ |
||
| 685 | protected function importProperty($name, $value) |
||
| 693 | } |
||
| 694 | |||
| 695 | |||
| 696 | |||
| 697 | /** |
||
| 698 | * import a value into a tracable record property if the value is not equal, try with multiple date format |
||
| 699 | * this method work for date field 0000-00-00 |
||
| 700 | * |
||
| 701 | * @param string $name property name |
||
| 702 | * @param mixed $value value to set |
||
| 703 | * |
||
| 704 | * @return int 1 : the value has been modified | 0 : no change |
||
| 705 | */ |
||
| 706 | protected function importDate($name, $value) |
||
| 707 | { |
||
| 708 | if (preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/',$value)) { |
||
| 709 | return $this->importProperty($name, $value); |
||
| 710 | } |
||
| 711 | |||
| 712 | // try in DD/MM/YYYY format |
||
| 713 | |||
| 714 | if (preg_match('/(?P<day>[0-9]+)\/(?P<month>[0-9]+)\/(?P<year>[0-9]{2,4})/',$value, $matches)) { |
||
| 715 | |||
| 716 | $value = sprintf('%04d-%02d-%02d', (int) $matches['year'], (int) $matches['month'], (int) $matches['day']); |
||
| 717 | |||
| 718 | return $this->importProperty($name, $value); |
||
| 719 | } |
||
| 720 | |||
| 721 | } |
||
| 722 | |||
| 723 | |||
| 724 | |||
| 725 | |||
| 726 | /** |
||
| 727 | * |
||
| 728 | * @return string[] |
||
| 729 | */ |
||
| 730 | public function getViews() |
||
| 731 | { |
||
| 732 | $App = $this->App(); |
||
| 733 | |||
| 734 | $customSectionSet = $App->CustomSectionSet(); |
||
| 735 | $customSections = $customSectionSet->select($customSectionSet->object->is($this->getClassName())); |
||
| 736 | $customSections->groupBy($customSectionSet->view); |
||
| 737 | |||
| 738 | $views = array(); |
||
| 739 | foreach ($customSections as $customSection) { |
||
| 740 | $views[] = $customSection->view; |
||
| 741 | } |
||
| 742 | |||
| 743 | if (empty($views)) { |
||
| 744 | $views[] = ''; |
||
| 745 | } |
||
| 746 | |||
| 747 | return $views; |
||
| 748 | } |
||
| 749 | |||
| 750 | |||
| 751 | |||
| 752 | /** |
||
| 753 | * Checks if the record is readable by the current user. |
||
| 754 | * @since 1.0.21 |
||
| 755 | * @return bool |
||
| 756 | */ |
||
| 757 | public function isReadable() |
||
| 758 | { |
||
| 759 | $set = $this->getParentSet(); |
||
| 760 | return $set->select($set->isReadable()->_AND_($set->id->is($this->id)))->count() == 1; |
||
| 761 | } |
||
| 762 | |||
| 763 | |||
| 764 | /** |
||
| 765 | * Checks if the record is updatable by the current user. |
||
| 766 | * @since 1.0.21 |
||
| 767 | * @return bool |
||
| 768 | */ |
||
| 769 | public function isUpdatable() |
||
| 770 | { |
||
| 771 | $set = $this->getParentSet(); |
||
| 772 | return $set->select($set->isUpdatable()->_AND_($set->id->is($this->id)))->count() == 1; |
||
| 773 | } |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Checks if the record is deletable by the current user. |
||
| 777 | * @since 1.0.21 |
||
| 778 | * @return bool |
||
| 779 | */ |
||
| 780 | public function isDeletable() |
||
| 784 | } |
||
| 785 | |||
| 786 | |||
| 787 | /** |
||
| 788 | * Checks if the record can be put to the trash by the current user. |
||
| 789 | * @return bool |
||
| 790 | */ |
||
| 791 | public function isRemovable() |
||
| 792 | { |
||
| 793 | $set = $this->getParentSet(); |
||
| 794 | return $set->select($set->isRemovable()->_AND_($set->id->is($this->id)))->count() == 1; |
||
| 795 | } |
||
| 796 | |||
| 797 | |||
| 798 | /** |
||
| 799 | * Ensures that the record is readable by the current user or throws an exception. |
||
| 800 | * @since 1.0.40 |
||
| 801 | * @param string $message |
||
| 802 | * @throws app_AccessException |
||
| 803 | */ |
||
| 804 | public function requireReadable($message = null) |
||
| 805 | { |
||
| 806 | if (!$this->isReadable()) { |
||
| 807 | $App = $this->App(); |
||
| 808 | if (!isset($message)) { |
||
| 809 | $message = $App->translate('Access denied'); |
||
| 810 | } |
||
| 811 | throw new app_AccessException($message); |
||
| 812 | } |
||
| 813 | } |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Ensures that the record is updatable by the current user or throws an exception. |
||
| 817 | * @since 1.0.40 |
||
| 818 | * @param string $message |
||
| 819 | * @throws app_AccessException |
||
| 820 | */ |
||
| 821 | public function requireUpdatable($message = null) |
||
| 822 | { |
||
| 823 | if (!$this->isUpdatable()) { |
||
| 824 | $App = $this->App(); |
||
| 825 | if (!isset($message)) { |
||
| 826 | $message = $App->translate('Access denied'); |
||
| 827 | } |
||
| 828 | throw new app_AccessException($message); |
||
| 829 | } |
||
| 830 | } |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Ensures that the record is deletable by the current user or throws an exception. |
||
| 834 | * @since 1.0.40 |
||
| 835 | * @param string $message |
||
| 836 | * @throws app_AccessException |
||
| 837 | */ |
||
| 838 | public function requireDeletable($message = null) |
||
| 846 | } |
||
| 847 | } |
||
| 848 | } |
||
| 849 | |||
| 850 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.