| Conditions | 33 |
| Paths | 864 |
| Total Lines | 168 |
| Code Lines | 94 |
| Lines | 29 |
| Ratio | 17.26 % |
| Changes | 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 |
||
| 28 | public function bind_to_object($object, $handler_id, $data) |
||
| 29 | { |
||
| 30 | $buttons = array(); |
||
| 31 | // Show view toolbar button, if the user hasn't configured to use straight the edit mode |
||
| 32 | if ($data['default_mode'] === 'view') |
||
| 33 | { |
||
| 34 | $buttons[] = array |
||
| 35 | ( |
||
| 36 | MIDCOM_TOOLBAR_URL => $this->_generate_url('view', $object), |
||
| 37 | MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('view', 'midcom'), |
||
| 38 | MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/view.png', |
||
| 39 | MIDCOM_TOOLBAR_ACCESSKEY => 'v', |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | |||
| 43 | if ( !is_a($object, 'midcom_db_style') |
||
| 44 | && !is_a($object, 'midcom_db_element') |
||
| 45 | && !is_a($object, 'midcom_db_snippetdir') |
||
| 46 | && !is_a($object, 'midcom_db_snippet') |
||
| 47 | && !is_a($object, 'midcom_db_page') |
||
| 48 | && !is_a($object, 'midcom_db_pageelement') |
||
| 49 | && !is_a($object, 'midcom_db_parameter') |
||
| 50 | && substr($object->__mgdschema_class_name__, 0, 23) != 'org_routamc_positioning' |
||
| 51 | && substr($object->__mgdschema_class_name__, 0, 14) != 'net_nemein_tag') |
||
| 52 | { |
||
| 53 | $link = midcom::get()->permalinks->resolve_permalink($object->guid); |
||
| 54 | View Code Duplication | if ($link) |
|
| 55 | { |
||
| 56 | $buttons[] = array |
||
| 57 | ( |
||
| 58 | MIDCOM_TOOLBAR_URL => $link, |
||
| 59 | MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('view on site', 'midgard.admin.asgard'), |
||
| 60 | MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_internet.png', |
||
| 61 | ); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | View Code Duplication | if ($object->can_do('midgard:update')) |
|
| 66 | { |
||
| 67 | $buttons[] = array |
||
| 68 | ( |
||
| 69 | MIDCOM_TOOLBAR_URL => $this->_generate_url('edit', $object), |
||
| 70 | MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('edit', 'midcom'), |
||
| 71 | MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/edit.png', |
||
| 72 | MIDCOM_TOOLBAR_ACCESSKEY => 'e', |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | |||
| 76 | if ($object->can_do('midgard:create')) |
||
| 77 | { |
||
| 78 | $url = (midcom_helper_reflector_tree::get_child_objects($object)) ? 'copy/tree' : 'copy'; |
||
| 79 | $buttons[] = array |
||
| 80 | ( |
||
| 81 | MIDCOM_TOOLBAR_URL => $this->_generate_url($url, $object), |
||
| 82 | MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('copy', 'midcom'), |
||
| 83 | MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/editcopy.png', |
||
| 84 | ); |
||
| 85 | } |
||
| 86 | |||
| 87 | if ($object->can_do('midgard:update')) |
||
| 88 | { |
||
| 89 | $buttons = array_merge($buttons, $this->get_toolbar_update_items($object)); |
||
| 90 | } |
||
| 91 | |||
| 92 | if ($object->can_do('midgard:create')) |
||
| 93 | { |
||
| 94 | // Find out what types of children the object can have and show create buttons for them |
||
| 95 | $child_types = $data['tree_reflector']->get_child_classes(); |
||
| 96 | if (!is_array($child_types)) |
||
| 97 | { |
||
| 98 | debug_add("\$data['tree_reflector']->get_child_classes() failed critically, recasting \$child_types as array", MIDCOM_LOG_WARN); |
||
| 99 | $child_types = array(); |
||
| 100 | } |
||
| 101 | foreach ($child_types as $type) |
||
| 102 | { |
||
| 103 | $display_button = true; |
||
| 104 | if (is_a($object, 'midcom_db_topic')) |
||
| 105 | { |
||
| 106 | // With topics we should check for component before populating create buttons as so many types can be children of topics |
||
| 107 | switch ($type) |
||
| 108 | { |
||
| 109 | case 'midgard_topic': |
||
| 110 | case 'midgard_article': |
||
| 111 | // Articles and topics can always be created |
||
| 112 | break; |
||
| 113 | default: |
||
| 114 | $midcom_dba_classname = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($type); |
||
| 115 | if (!$midcom_dba_classname) |
||
| 116 | { |
||
| 117 | $display_button = false; |
||
| 118 | break; |
||
| 119 | } |
||
| 120 | $component = midcom::get()->dbclassloader->get_component_for_class($type); |
||
| 121 | if ($component != $object->component) |
||
| 122 | { |
||
| 123 | $display_button = false; |
||
| 124 | } |
||
| 125 | break; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | else if (is_a($object, 'midcom_db_article')) |
||
| 129 | { |
||
| 130 | try |
||
| 131 | { |
||
| 132 | $topic = new midcom_db_topic($object->topic); |
||
| 133 | // With articles we should check for topic component before populating create buttons as so many types can be children of topics |
||
| 134 | switch ($type) |
||
| 135 | { |
||
| 136 | case 'midgard_article': |
||
| 137 | // Articles can always be created |
||
| 138 | break; |
||
| 139 | default: |
||
| 140 | $component = midcom::get()->dbclassloader->get_component_for_class($type); |
||
| 141 | if ($component != $topic->component) |
||
| 142 | { |
||
| 143 | $display_button = false; |
||
| 144 | } |
||
| 145 | break; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | catch (midcom_error $e) |
||
| 149 | { |
||
| 150 | $e->log(); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | if (!$display_button) |
||
| 155 | { |
||
| 156 | // Skip this type |
||
| 157 | continue; |
||
| 158 | } |
||
| 159 | |||
| 160 | $buttons[] = array |
||
| 161 | ( |
||
| 162 | MIDCOM_TOOLBAR_URL => $this->_generate_url('create/' . $type, $object), |
||
| 163 | MIDCOM_TOOLBAR_LABEL => sprintf(midcom::get()->i18n->get_string('create %s', 'midcom'), midgard_admin_asgard_plugin::get_type_label($type)), |
||
| 164 | MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/' . $data['tree_reflector']->get_create_icon($type), |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | View Code Duplication | if ($object->can_do('midgard:delete')) |
|
| 170 | { |
||
| 171 | $buttons[] = array |
||
| 172 | ( |
||
| 173 | MIDCOM_TOOLBAR_URL => $this->_generate_url('delete', $object), |
||
| 174 | MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('delete', 'midcom'), |
||
| 175 | MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/trash.png', |
||
| 176 | MIDCOM_TOOLBAR_ACCESSKEY => 'd', |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | |||
| 180 | if ( midcom::get()->config->get('midcom_services_rcs_enable') |
||
| 181 | && $object->can_do('midgard:update') |
||
| 182 | && $object->_use_rcs) |
||
| 183 | { |
||
| 184 | $buttons[] = array |
||
| 185 | ( |
||
| 186 | MIDCOM_TOOLBAR_URL => $this->_generate_url('rcs', $object), |
||
| 187 | MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('show history', 'midgard.admin.asgard'), |
||
| 188 | MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/history.png', |
||
| 189 | MIDCOM_TOOLBAR_ENABLED => (substr($handler_id, 0, 25) !== '____mfa-asgard-object_rcs'), |
||
| 190 | MIDCOM_TOOLBAR_ACCESSKEY => 'h', |
||
| 191 | ); |
||
| 192 | } |
||
| 193 | $this->add_items($buttons); |
||
| 194 | $this->_disable_active_item($handler_id, $object, $data); |
||
| 195 | } |
||
| 196 | |||
| 302 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.