| Conditions | 50 |
| Paths | > 20000 |
| Total Lines | 201 |
| Code Lines | 114 |
| Lines | 5 |
| Ratio | 2.49 % |
| Changes | 3 | ||
| 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 |
||
| 46 | public function execute(array &$param_pool = null) |
||
| 47 | { |
||
| 48 | $result = new XMLElement($this->dsParamROOTELEMENT); |
||
| 49 | $this->_param_pool = $param_pool; |
||
| 50 | $where = null; |
||
| 51 | $joins = null; |
||
| 52 | $group = false; |
||
| 53 | |||
| 54 | if (!$section = SectionManager::fetch((int)$this->getSource())) { |
||
| 55 | $about = $this->about(); |
||
| 56 | trigger_error(__('The Section, %s, associated with the Data source, %s, could not be found.', |
||
| 57 | array($this->getSource(), '<code>' . $about['name'] . '</code>')), E_USER_ERROR); |
||
| 58 | } |
||
| 59 | |||
| 60 | $sectioninfo = new XMLElement('section', General::sanitize($section->get('name')), array( |
||
| 61 | 'id' => $section->get('id'), |
||
| 62 | 'handle' => $section->get('handle') |
||
| 63 | )); |
||
| 64 | |||
| 65 | if ($this->_force_empty_result === true) { |
||
| 66 | if ($this->dsParamREDIRECTONREQUIRED === 'yes') { |
||
| 67 | throw new FrontendPageNotFoundException; |
||
| 68 | } |
||
| 69 | |||
| 70 | $this->_force_empty_result = false; //this is so the section info element doesn't disappear. |
||
| 71 | $error = new XMLElement('error', __("Data source not executed, required parameter is missing."), array( |
||
| 72 | 'required-param' => $this->dsParamREQUIREDPARAM |
||
| 73 | )); |
||
| 74 | $result->appendChild($error); |
||
| 75 | $result->prependChild($sectioninfo); |
||
| 76 | |||
| 77 | return $result; |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($this->_negate_result === true) { |
||
| 81 | if ($this->dsParamREDIRECTONFORBIDDEN === 'yes') { |
||
| 82 | throw new FrontendPageNotFoundException; |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->_negate_result = false; //this is so the section info element doesn't disappear. |
||
| 86 | $result = $this->negateXMLSet(); |
||
| 87 | $result->prependChild($sectioninfo); |
||
| 88 | |||
| 89 | return $result; |
||
| 90 | } |
||
| 91 | |||
| 92 | if (is_array($this->dsParamINCLUDEDELEMENTS)) { |
||
| 93 | $include_pagination_element = in_array('system:pagination', $this->dsParamINCLUDEDELEMENTS); |
||
| 94 | } else { |
||
| 95 | $this->dsParamINCLUDEDELEMENTS = array(); |
||
| 96 | } |
||
| 97 | |||
| 98 | if (isset($this->dsParamPARAMOUTPUT) && !is_array($this->dsParamPARAMOUTPUT)) { |
||
| 99 | $this->dsParamPARAMOUTPUT = array($this->dsParamPARAMOUTPUT); |
||
| 100 | } |
||
| 101 | |||
| 102 | $this->_can_process_system_parameters = $this->canProcessSystemParameters(); |
||
| 103 | |||
| 104 | if (!isset($this->dsParamPAGINATERESULTS)) { |
||
| 105 | $this->dsParamPAGINATERESULTS = 'yes'; |
||
| 106 | } |
||
| 107 | |||
| 108 | // Process Filters |
||
| 109 | $this->processFilters($where, $joins, $group); |
||
| 110 | |||
| 111 | // Process Sorting |
||
| 112 | if ($this->dsParamSORT === 'system:id') { |
||
| 113 | EntryManager::setFetchSorting('system:id', $this->dsParamORDER); |
||
| 114 | } elseif ($this->dsParamSORT === 'system:date' || $this->dsParamSORT === 'system:creation-date') { |
||
| 115 | EntryManager::setFetchSorting('system:creation-date', $this->dsParamORDER); |
||
| 116 | } elseif ($this->dsParamSORT === 'system:modification-date') { |
||
| 117 | EntryManager::setFetchSorting('system:modification-date', $this->dsParamORDER); |
||
| 118 | } else { |
||
| 119 | EntryManager::setFetchSorting( |
||
| 120 | FieldManager::fetchFieldIDFromElementName($this->dsParamSORT, $this->getSource()), |
||
| 121 | $this->dsParamORDER |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | |||
| 125 | // combine `INCLUDEDELEMENTS`, `PARAMOUTPUT` and `GROUP` into an |
||
| 126 | // array of field handles to optimise the `EntryManager` queries |
||
| 127 | $datasource_schema = $this->dsParamINCLUDEDELEMENTS; |
||
| 128 | |||
| 129 | if (is_array($this->dsParamPARAMOUTPUT)) { |
||
| 130 | $datasource_schema = array_merge($datasource_schema, $this->dsParamPARAMOUTPUT); |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($this->dsParamGROUP) { |
||
| 134 | $datasource_schema[] = FieldManager::fetchHandleFromID($this->dsParamGROUP); |
||
| 135 | } |
||
| 136 | |||
| 137 | $entries = EntryManager::fetchByPage( |
||
| 138 | ($this->dsParamPAGINATERESULTS === 'yes' && $this->dsParamSTARTPAGE > 0 ? $this->dsParamSTARTPAGE : 1), |
||
| 139 | $this->getSource(), |
||
| 140 | ($this->dsParamPAGINATERESULTS === 'yes' && $this->dsParamLIMIT >= 0 ? $this->dsParamLIMIT : null), |
||
| 141 | $where, |
||
| 142 | $joins, |
||
| 143 | $group, |
||
| 144 | (!$include_pagination_element ? true : false), |
||
| 145 | true, |
||
| 146 | array_unique($datasource_schema) |
||
| 147 | ); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Immediately after building entries allow modification of the Data Source entries array |
||
| 151 | * |
||
| 152 | * @delegate DataSourceEntriesBuilt |
||
| 153 | * @param string $context |
||
| 154 | * '/frontend/' |
||
| 155 | * @param Datasource $datasource |
||
| 156 | * @param array $entries |
||
| 157 | * @param array $filters |
||
| 158 | */ |
||
| 159 | Symphony::ExtensionManager()->notifyMembers('DataSourceEntriesBuilt', '/frontend/', array( |
||
| 160 | 'datasource' => &$this, |
||
| 161 | 'entries' => &$entries, |
||
| 162 | 'filters' => $this->dsParamFILTERS |
||
| 163 | )); |
||
| 164 | |||
| 165 | if (($entries['total-entries'] <= 0 || $include_pagination_element === true) && (!is_array($entries['records']) || empty($entries['records'])) || (int)$this->dsParamSTARTPAGE === 0) { |
||
| 166 | if ($this->dsParamREDIRECTONEMPTY === 'yes') { |
||
| 167 | throw new FrontendPageNotFoundException; |
||
| 168 | } |
||
| 169 | |||
| 170 | $this->_force_empty_result = false; |
||
| 171 | $result = $this->emptyXMLSet(); |
||
| 172 | $result->prependChild($sectioninfo); |
||
| 173 | |||
| 174 | if ($include_pagination_element) { |
||
| 175 | $pagination_element = General::buildPaginationElement(); |
||
| 176 | |||
| 177 | if ($pagination_element instanceof XMLElement && $result instanceof XMLElement) { |
||
| 178 | $result->prependChild($pagination_element); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } else { |
||
| 182 | if (!$this->_param_output_only) { |
||
| 183 | $result->appendChild($sectioninfo); |
||
| 184 | |||
| 185 | if ($include_pagination_element) { |
||
| 186 | $t = ($this->dsParamPAGINATERESULTS === 'yes' && isset($this->dsParamLIMIT) && $this->dsParamLIMIT >= 0 ? $this->dsParamLIMIT : $entries['total-entries']); |
||
| 187 | |||
| 188 | $pagination_element = General::buildPaginationElement( |
||
| 189 | $entries['total-entries'], |
||
| 190 | $entries['total-pages'], |
||
| 191 | $t, |
||
| 192 | ($this->dsParamPAGINATERESULTS === 'yes' && $this->dsParamSTARTPAGE > 0 ? $this->dsParamSTARTPAGE : 1) |
||
| 193 | ); |
||
| 194 | |||
| 195 | if ($pagination_element instanceof XMLElement && $result instanceof XMLElement) { |
||
| 196 | $result->prependChild($pagination_element); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | // If this datasource has a Limit greater than 0 or the Limit is not set |
||
| 202 | if (!isset($this->dsParamLIMIT) || $this->dsParamLIMIT > 0) { |
||
| 203 | if (!isset($this->dsParamASSOCIATEDENTRYCOUNTS) || $this->dsParamASSOCIATEDENTRYCOUNTS === 'yes') { |
||
| 204 | $this->_associated_sections = $section->fetchChildAssociations(); |
||
| 205 | } |
||
| 206 | |||
| 207 | // If the datasource require's GROUPING |
||
| 208 | if (isset($this->dsParamGROUP)) { |
||
| 209 | self::$_fieldPool[$this->dsParamGROUP] = FieldManager::fetch($this->dsParamGROUP); |
||
| 210 | |||
| 211 | if (self::$_fieldPool[$this->dsParamGROUP] === null) { |
||
| 212 | throw new SymphonyErrorPage(vsprintf("The field used for grouping '%s' cannot be found.", |
||
| 213 | $this->dsParamGROUP)); |
||
| 214 | } |
||
| 215 | |||
| 216 | $groups = self::$_fieldPool[$this->dsParamGROUP]->groupRecords($entries['records']); |
||
| 217 | |||
| 218 | foreach ($groups as $element => $group) { |
||
| 219 | foreach ($group as $g) { |
||
| 220 | $result->appendChild( |
||
| 221 | $this->processRecordGroup($element, $g) |
||
| 222 | ); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } else { |
||
| 226 | View Code Duplication | if (isset($entries['records'][0])) { |
|
| 227 | $data = $entries['records'][0]->getData(); |
||
| 228 | $pool = FieldManager::fetch(array_keys($data)); |
||
| 229 | self::$_fieldPool += $pool; |
||
| 230 | } |
||
| 231 | |||
| 232 | foreach ($entries['records'] as $entry) { |
||
| 233 | $xEntry = $this->processEntry($entry); |
||
| 234 | |||
| 235 | if ($xEntry instanceof XMLElement) { |
||
| 236 | $result->appendChild($xEntry); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | $param_pool = $this->_param_pool; |
||
| 244 | |||
| 245 | return $result; |
||
| 246 | } |
||
| 247 | |||
| 646 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: