| Conditions | 41 |
| Paths | > 20000 |
| Total Lines | 286 |
| Code Lines | 118 |
| Lines | 62 |
| Ratio | 21.68 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 701 | public function __actionEdit() |
||
| 702 | { |
||
| 703 | if ($this->_context['action'] !== 'new' && !$page_id = (int)$this->_context['id']) { |
||
| 704 | redirect(SYMPHONY_URL . '/blueprints/pages/'); |
||
| 705 | } |
||
| 706 | |||
| 707 | $parent_link_suffix = null; |
||
| 708 | |||
| 709 | View Code Duplication | if (isset($_REQUEST['parent']) && is_numeric($_REQUEST['parent'])) { |
|
| 710 | $parent_link_suffix = '?parent=' . $_REQUEST['parent']; |
||
| 711 | } |
||
| 712 | |||
| 713 | if (@array_key_exists('delete', $_POST['action'])) { |
||
| 714 | $this->__actionDelete($page_id, SYMPHONY_URL . '/blueprints/pages/' . $parent_link_suffix); |
||
| 715 | } |
||
| 716 | |||
| 717 | if (@array_key_exists('save', $_POST['action'])) { |
||
| 718 | $fields = $_POST['fields']; |
||
| 719 | $this->_errors = array(); |
||
| 720 | $autogenerated_handle = false; |
||
| 721 | |||
| 722 | View Code Duplication | if (!isset($fields['title']) || trim($fields['title']) === '') { |
|
| 723 | $this->_errors['title'] = __('This is a required field'); |
||
| 724 | } |
||
| 725 | |||
| 726 | if (trim($fields['type']) !== '' && preg_match('/(index|404|403)/i', $fields['type'])) { |
||
| 727 | $types = preg_split('/\s*,\s*/', strtolower($fields['type']), -1, PREG_SPLIT_NO_EMPTY); |
||
| 728 | |||
| 729 | if (in_array('index', $types) && PageManager::hasPageTypeBeenUsed($page_id, 'index')) { |
||
| 730 | $this->_errors['type'] = __('An index type page already exists.'); |
||
| 731 | } elseif (in_array('404', $types) && PageManager::hasPageTypeBeenUsed($page_id, '404')) { |
||
| 732 | $this->_errors['type'] = __('A 404 type page already exists.'); |
||
| 733 | } elseif (in_array('403', $types) && PageManager::hasPageTypeBeenUsed($page_id, '403')) { |
||
| 734 | $this->_errors['type'] = __('A 403 type page already exists.'); |
||
| 735 | } |
||
| 736 | } |
||
| 737 | |||
| 738 | if (trim($fields['handle']) === '') { |
||
| 739 | $fields['handle'] = $fields['title']; |
||
| 740 | $autogenerated_handle = true; |
||
| 741 | } |
||
| 742 | |||
| 743 | $fields['handle'] = PageManager::createHandle($fields['handle']); |
||
| 744 | |||
| 745 | if (empty($fields['handle']) && !isset($this->_errors['title'])) { |
||
| 746 | $this->_errors['handle'] = __('Please ensure handle contains at least one Latin-based character.'); |
||
| 747 | } |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Just after the Symphony validation has run, allows Developers |
||
| 751 | * to run custom validation logic on a Page |
||
| 752 | * |
||
| 753 | * @delegate PagePostValidate |
||
| 754 | * @since Symphony 2.2 |
||
| 755 | * @param string $context |
||
| 756 | * '/blueprints/pages/' |
||
| 757 | * @param array $fields |
||
| 758 | * The `$_POST['fields']` array. This should be read-only and not changed |
||
| 759 | * through this delegate. |
||
| 760 | * @param array $errors |
||
| 761 | * An associative array of errors, with the key matching a key in the |
||
| 762 | * `$fields` array, and the value being the string of the error. `$errors` |
||
| 763 | * is passed by reference. |
||
| 764 | */ |
||
| 765 | Symphony::ExtensionManager()->notifyMembers('PagePostValidate', '/blueprints/pages/', |
||
| 766 | array('fields' => $fields, 'errors' => &$errors)); |
||
| 767 | |||
| 768 | if (empty($this->_errors)) { |
||
| 769 | $autogenerated_handle = false; |
||
| 770 | |||
| 771 | if ($fields['params']) { |
||
| 772 | $fields['params'] = trim(preg_replace('@\/{2,}@', '/', $fields['params']), '/'); |
||
| 773 | } |
||
| 774 | |||
| 775 | // Clean up type list |
||
| 776 | $types = preg_split('/\s*,\s*/', $fields['type'], -1, PREG_SPLIT_NO_EMPTY); |
||
| 777 | $types = @array_map('trim', $types); |
||
| 778 | unset($fields['type']); |
||
| 779 | |||
| 780 | $fields = array_filter($fields); |
||
| 781 | |||
| 782 | $fields['parent'] = ($fields['parent'] !== __('None') ? $fields['parent'] : null); |
||
| 783 | $fields['data_sources'] = is_array($fields['data_sources']) ? implode(',', |
||
| 784 | $fields['data_sources']) : null; |
||
| 785 | $fields['events'] = is_array($fields['events']) ? implode(',', $fields['events']) : null; |
||
| 786 | $fields['path'] = null; |
||
| 787 | |||
| 788 | if ($fields['parent']) { |
||
| 789 | $fields['path'] = PageManager::resolvePagePath((integer)$fields['parent']); |
||
| 790 | } |
||
| 791 | |||
| 792 | // Check for duplicates: |
||
| 793 | $current = PageManager::fetchPageByID($page_id); |
||
| 794 | |||
| 795 | if (empty($current)) { |
||
| 796 | $fields['sortorder'] = PageManager::fetchNextSortOrder(); |
||
| 797 | } |
||
| 798 | |||
| 799 | $where = array(); |
||
| 800 | |||
| 801 | if (!empty($current)) { |
||
| 802 | $where[] = "p.id != {$page_id}"; |
||
| 803 | } |
||
| 804 | |||
| 805 | $where[] = "p.handle = '" . $fields['handle'] . "'"; |
||
| 806 | $where[] = (is_null($fields['path'])) |
||
| 807 | ? "p.path IS null" |
||
| 808 | : "p.path = '" . $fields['path'] . "'"; |
||
| 809 | $duplicate = PageManager::fetch(false, array('*'), $where); |
||
| 810 | |||
| 811 | // If duplicate |
||
| 812 | if (!empty($duplicate)) { |
||
| 813 | if ($autogenerated_handle) { |
||
| 814 | $this->_errors['title'] = __('A page with that title already exists'); |
||
| 815 | } else { |
||
| 816 | $this->_errors['handle'] = __('A page with that handle already exists'); |
||
| 817 | } |
||
| 818 | |||
| 819 | // Create or move files: |
||
| 820 | } else { |
||
| 821 | // New page? |
||
| 822 | if (empty($current)) { |
||
| 823 | $file_created = PageManager::createPageFiles( |
||
| 824 | $fields['path'], |
||
| 825 | $fields['handle'] |
||
| 826 | ); |
||
| 827 | |||
| 828 | // Existing page, potentially rename files |
||
| 829 | } else { |
||
| 830 | $file_created = PageManager::createPageFiles( |
||
| 831 | $fields['path'], |
||
| 832 | $fields['handle'], |
||
| 833 | $current['path'], |
||
| 834 | $current['handle'] |
||
| 835 | ); |
||
| 836 | } |
||
| 837 | |||
| 838 | // If the file wasn't created, it's usually permissions related |
||
| 839 | if (!$file_created) { |
||
| 840 | $redirect = null; |
||
| 841 | |||
| 842 | return $this->pageAlert( |
||
| 843 | __('Page Template could not be written to disk.') |
||
| 844 | . ' ' . __('Please check permissions on %s.', array('<code>/workspace/pages</code>')), |
||
| 845 | Alert::ERROR |
||
| 846 | ); |
||
| 847 | } |
||
| 848 | |||
| 849 | // Insert the new data: |
||
| 850 | if (empty($current)) { |
||
| 851 | /** |
||
| 852 | * Just prior to creating a new Page record in `tbl_pages`, provided |
||
| 853 | * with the `$fields` associative array. Use with caution, as no |
||
| 854 | * duplicate page checks are run after this delegate has fired |
||
| 855 | * |
||
| 856 | * @delegate PagePreCreate |
||
| 857 | * @since Symphony 2.2 |
||
| 858 | * @param string $context |
||
| 859 | * '/blueprints/pages/' |
||
| 860 | * @param array $fields |
||
| 861 | * The `$_POST['fields']` array passed by reference |
||
| 862 | */ |
||
| 863 | Symphony::ExtensionManager()->notifyMembers('PagePreCreate', '/blueprints/pages/', |
||
| 864 | array('fields' => &$fields)); |
||
| 865 | |||
| 866 | View Code Duplication | if (!$page_id = PageManager::add($fields)) { |
|
| 867 | $this->pageAlert( |
||
| 868 | __('Unknown errors occurred while attempting to save.') |
||
| 869 | . '<a href="' . SYMPHONY_URL . '/system/log/">' |
||
| 870 | . __('Check your activity log') |
||
| 871 | . '</a>.', |
||
| 872 | Alert::ERROR |
||
| 873 | ); |
||
| 874 | } else { |
||
| 875 | /** |
||
| 876 | * Just after the creation of a new page in `tbl_pages` |
||
| 877 | * |
||
| 878 | * @delegate PagePostCreate |
||
| 879 | * @since Symphony 2.2 |
||
| 880 | * @param string $context |
||
| 881 | * '/blueprints/pages/' |
||
| 882 | * @param integer $page_id |
||
| 883 | * The ID of the newly created Page |
||
| 884 | * @param array $fields |
||
| 885 | * An associative array of data that was just saved for this page |
||
| 886 | */ |
||
| 887 | Symphony::ExtensionManager()->notifyMembers('PagePostCreate', '/blueprints/pages/', |
||
| 888 | array('page_id' => $page_id, 'fields' => &$fields)); |
||
| 889 | |||
| 890 | $redirect = "/blueprints/pages/edit/{$page_id}/created/{$parent_link_suffix}"; |
||
| 891 | } |
||
| 892 | |||
| 893 | // Update existing: |
||
| 894 | } else { |
||
| 895 | /** |
||
| 896 | * Just prior to updating a Page record in `tbl_pages`, provided |
||
| 897 | * with the `$fields` associative array. Use with caution, as no |
||
| 898 | * duplicate page checks are run after this delegate has fired |
||
| 899 | * |
||
| 900 | * @delegate PagePreEdit |
||
| 901 | * @since Symphony 2.2 |
||
| 902 | * @param string $context |
||
| 903 | * '/blueprints/pages/' |
||
| 904 | * @param integer $page_id |
||
| 905 | * The ID of the Page that is about to be updated |
||
| 906 | * @param array $fields |
||
| 907 | * The `$_POST['fields']` array passed by reference |
||
| 908 | */ |
||
| 909 | Symphony::ExtensionManager()->notifyMembers('PagePreEdit', '/blueprints/pages/', |
||
| 910 | array('page_id' => $page_id, 'fields' => &$fields)); |
||
| 911 | |||
| 912 | View Code Duplication | if (!PageManager::edit($page_id, $fields, true)) { |
|
| 913 | return $this->pageAlert( |
||
| 914 | __('Unknown errors occurred while attempting to save.') |
||
| 915 | . '<a href="' . SYMPHONY_URL . '/system/log/">' |
||
| 916 | . __('Check your activity log') |
||
| 917 | . '</a>.', |
||
| 918 | Alert::ERROR |
||
| 919 | ); |
||
| 920 | } else { |
||
| 921 | /** |
||
| 922 | * Just after updating a page in `tbl_pages` |
||
| 923 | * |
||
| 924 | * @delegate PagePostEdit |
||
| 925 | * @since Symphony 2.2 |
||
| 926 | * @param string $context |
||
| 927 | * '/blueprints/pages/' |
||
| 928 | * @param integer $page_id |
||
| 929 | * The ID of the Page that was just updated |
||
| 930 | * @param array $fields |
||
| 931 | * An associative array of data that was just saved for this page |
||
| 932 | */ |
||
| 933 | Symphony::ExtensionManager()->notifyMembers('PagePostEdit', '/blueprints/pages/', |
||
| 934 | array('page_id' => $page_id, 'fields' => $fields)); |
||
| 935 | |||
| 936 | $redirect = "/blueprints/pages/edit/{$page_id}/saved/{$parent_link_suffix}"; |
||
| 937 | } |
||
| 938 | } |
||
| 939 | } |
||
| 940 | |||
| 941 | // Only proceed if there was no errors saving/creating the page |
||
| 942 | if (empty($this->_errors)) { |
||
| 943 | /** |
||
| 944 | * Just before the page's types are saved into `tbl_pages_types`. |
||
| 945 | * Use with caution as no further processing is done on the `$types` |
||
| 946 | * array to prevent duplicate `$types` from occurring (ie. two index |
||
| 947 | * page types). Your logic can use the PageManger::hasPageTypeBeenUsed |
||
| 948 | * function to perform this logic. |
||
| 949 | * |
||
| 950 | * @delegate PageTypePreCreate |
||
| 951 | * @since Symphony 2.2 |
||
| 952 | * @see toolkit.PageManager#hasPageTypeBeenUsed |
||
| 953 | * @param string $context |
||
| 954 | * '/blueprints/pages/' |
||
| 955 | * @param integer $page_id |
||
| 956 | * The ID of the Page that was just created or updated |
||
| 957 | * @param array $types |
||
| 958 | * An associative array of the types for this page passed by reference. |
||
| 959 | */ |
||
| 960 | Symphony::ExtensionManager()->notifyMembers('PageTypePreCreate', '/blueprints/pages/', |
||
| 961 | array('page_id' => $page_id, 'types' => &$types)); |
||
| 962 | |||
| 963 | // Assign page types: |
||
| 964 | PageManager::addPageTypesToPage($page_id, $types); |
||
| 965 | |||
| 966 | // Find and update children: |
||
| 967 | if ($this->_context['action'] === 'edit') { |
||
| 968 | PageManager::editPageChildren($page_id, $fields['path'] . '/' . $fields['handle']); |
||
| 969 | } |
||
| 970 | |||
| 971 | if ($redirect) { |
||
| 972 | redirect(SYMPHONY_URL . $redirect); |
||
| 973 | } |
||
| 974 | } |
||
| 975 | } |
||
| 976 | |||
| 977 | // If there was any errors, either with pre processing or because of a |
||
| 978 | // duplicate page, return. |
||
| 979 | View Code Duplication | if (is_array($this->_errors) && !empty($this->_errors)) { |
|
| 980 | return $this->pageAlert( |
||
| 981 | __('An error occurred while processing this form. See below for details.'), |
||
| 982 | Alert::ERROR |
||
| 983 | ); |
||
| 984 | } |
||
| 985 | } |
||
| 986 | } |
||
| 987 | } |
||
| 988 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.