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