| Conditions | 37 |
| Paths | 1827 |
| Total Lines | 150 |
| Code Lines | 81 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 15 | public function execute(array &$param_pool = null) |
||
| 16 | { |
||
| 17 | $author_ids = array(); |
||
| 18 | |||
| 19 | if (is_array($this->dsParamFILTERS) && !empty($this->dsParamFILTERS)) { |
||
| 20 | foreach ($this->dsParamFILTERS as $field => $value) { |
||
|
|
|||
| 21 | if (!is_array($value) && trim($value) === '') { |
||
| 22 | continue; |
||
| 23 | } |
||
| 24 | |||
| 25 | $ret = $this->__processAuthorFilter($field, $value); |
||
| 26 | |||
| 27 | if (empty($ret)) { |
||
| 28 | $author_ids = array(); |
||
| 29 | break; |
||
| 30 | } |
||
| 31 | |||
| 32 | if (empty($author_ids)) { |
||
| 33 | $author_ids = $ret; |
||
| 34 | continue; |
||
| 35 | } |
||
| 36 | |||
| 37 | $author_ids = array_intersect($author_ids, $ret); |
||
| 38 | } |
||
| 39 | |||
| 40 | $authors = AuthorManager::fetchByID(array_values($author_ids)); |
||
| 41 | } else { |
||
| 42 | $authors = AuthorManager::fetch($this->dsParamSORT, $this->dsParamORDER); |
||
| 43 | } |
||
| 44 | |||
| 45 | if ((!is_array($authors) || empty($authors)) && $this->dsParamREDIRECTONEMPTY === 'yes') { |
||
| 46 | throw new FrontendPageNotFoundException; |
||
| 47 | } elseif (!is_array($authors) || empty($authors)) { |
||
| 48 | $result = $this->emptyXMLSet(); |
||
| 49 | |||
| 50 | return $result; |
||
| 51 | } else { |
||
| 52 | if ($this->_negate_result === true) { |
||
| 53 | return $this->negateXMLSet(); |
||
| 54 | } |
||
| 55 | |||
| 56 | if (!$this->_param_output_only) { |
||
| 57 | $result = new XMLElement($this->dsParamROOTELEMENT); |
||
| 58 | } |
||
| 59 | |||
| 60 | $singleParam = false; |
||
| 61 | $key = 'ds-' . $this->dsParamROOTELEMENT; |
||
| 62 | |||
| 63 | if (isset($this->dsParamPARAMOUTPUT)) { |
||
| 64 | if (!is_array($this->dsParamPARAMOUTPUT)) { |
||
| 65 | $this->dsParamPARAMOUTPUT = array($this->dsParamPARAMOUTPUT); |
||
| 66 | } |
||
| 67 | |||
| 68 | $singleParam = count($this->dsParamPARAMOUTPUT) === 1; |
||
| 69 | } |
||
| 70 | |||
| 71 | foreach ($authors as $author) { |
||
| 72 | if (isset($this->dsParamPARAMOUTPUT)) { |
||
| 73 | foreach ($this->dsParamPARAMOUTPUT as $param) { |
||
| 74 | // The new style of paramater is `ds-datasource-handle.field-handle` |
||
| 75 | $param_key = $key . '.' . str_replace(':', '-', $param); |
||
| 76 | |||
| 77 | if (!is_array($param_pool[$param_key])) { |
||
| 78 | $param_pool[$param_key] = array(); |
||
| 79 | } |
||
| 80 | |||
| 81 | $param_pool[$param_key][] = ($param === 'name' ? $author->getFullName() : $author->get($param)); |
||
| 82 | |||
| 83 | if ($singleParam) { |
||
| 84 | if (!is_array($param_pool[$key])) { |
||
| 85 | $param_pool[$key] = array(); |
||
| 86 | } |
||
| 87 | |||
| 88 | $param_pool[$key][] = ($param === 'name' ? $author->getFullName() : $author->get($param)); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($this->_param_output_only) { |
||
| 94 | continue; |
||
| 95 | } |
||
| 96 | |||
| 97 | $xAuthor = new XMLElement('author'); |
||
| 98 | $xAuthor->setAttributeArray(array( |
||
| 99 | 'id' => $author->get('id'), |
||
| 100 | 'user-type' => $author->get('user_type'), |
||
| 101 | 'primary-account' => $author->get('primary') |
||
| 102 | )); |
||
| 103 | |||
| 104 | // No included elements, so just create the Author XML |
||
| 105 | if (!isset($this->dsParamINCLUDEDELEMENTS) || !is_array($this->dsParamINCLUDEDELEMENTS) || empty($this->dsParamINCLUDEDELEMENTS)) { |
||
| 106 | $result->appendChild($xAuthor); |
||
| 107 | } else { |
||
| 108 | // Name |
||
| 109 | if (in_array('name', $this->dsParamINCLUDEDELEMENTS)) { |
||
| 110 | $xAuthor->appendChild( |
||
| 111 | new XMLElement('name', $author->getFullName()) |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | // Username |
||
| 116 | if (in_array('username', $this->dsParamINCLUDEDELEMENTS)) { |
||
| 117 | $xAuthor->appendChild( |
||
| 118 | new XMLElement('username', $author->get('username')) |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | if (in_array('email', $this->dsParamINCLUDEDELEMENTS)) { |
||
| 124 | $xAuthor->appendChild( |
||
| 125 | new XMLElement('email', $author->get('email')) |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | |||
| 129 | // Author Token |
||
| 130 | if (in_array('author-token', $this->dsParamINCLUDEDELEMENTS) && $author->isTokenActive()) { |
||
| 131 | $xAuthor->appendChild( |
||
| 132 | new XMLElement('author-token', $author->createAuthToken()) |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | // Default Area |
||
| 137 | if (in_array('default-area', |
||
| 138 | $this->dsParamINCLUDEDELEMENTS) && !is_null($author->get('default_area')) |
||
| 139 | ) { |
||
| 140 | // Section |
||
| 141 | if ($section = SectionManager::fetch($author->get('default_area'))) { |
||
| 142 | $default_area = new XMLElement('default-area', $section->get('name')); |
||
| 143 | $default_area->setAttributeArray(array( |
||
| 144 | 'id' => $section->get('id'), |
||
| 145 | 'handle' => $section->get('handle'), |
||
| 146 | 'type' => 'section' |
||
| 147 | )); |
||
| 148 | $xAuthor->appendChild($default_area); |
||
| 149 | |||
| 150 | // Pages |
||
| 151 | } else { |
||
| 152 | $default_area = new XMLElement('default-area', $author->get('default_area')); |
||
| 153 | $default_area->setAttribute('type', 'page'); |
||
| 154 | $xAuthor->appendChild($default_area); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | $result->appendChild($xAuthor); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | return $result; |
||
| 164 | } |
||
| 165 | |||
| 186 |
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: