| Conditions | 33 |
| Paths | 16 |
| Total Lines | 119 |
| Code Lines | 71 |
| 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 |
||
| 115 | public function convertDataObjectWithoutHeader(DataObject $obj, $fields = null, $relations = null) |
||
| 116 | { |
||
| 117 | $className = $this->sanitiseClassName(get_class($obj)); |
||
| 118 | $id = $obj->ID; |
||
| 119 | $objHref = Director::absoluteURL($this->config()->api_base . "$className/$obj->ID"); |
||
| 120 | |||
| 121 | $xml = "<$className href=\"$objHref.xml\">\n"; |
||
| 122 | foreach ($this->getFieldsForObj($obj) as $fieldName => $fieldType) { |
||
| 123 | // Field filtering |
||
| 124 | if ($fields && !in_array($fieldName, $fields)) { |
||
| 125 | continue; |
||
| 126 | } |
||
| 127 | $fieldValue = $obj->obj($fieldName)->forTemplate(); |
||
| 128 | if (!mb_check_encoding($fieldValue, 'utf-8')) { |
||
| 129 | $fieldValue = "(data is badly encoded)"; |
||
| 130 | } |
||
| 131 | |||
| 132 | if (is_object($fieldValue) && is_subclass_of($fieldValue, 'Object') && $fieldValue->hasMethod('toXML')) { |
||
| 133 | $xml .= $fieldValue->toXML(); |
||
| 134 | } else { |
||
| 135 | if ('HTMLText' == $fieldType) { |
||
| 136 | // Escape HTML values using CDATA |
||
| 137 | $fieldValue = sprintf('<![CDATA[%s]]>', str_replace(']]>', ']]]]><![CDATA[>', $fieldValue)); |
||
| 138 | } else { |
||
| 139 | $fieldValue = Convert::raw2xml($fieldValue); |
||
| 140 | } |
||
| 141 | $mappedFieldName = $this->getFieldAlias(get_class($obj), $fieldName); |
||
| 142 | $xml .= "<$mappedFieldName>$fieldValue</$mappedFieldName>\n"; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($this->relationDepth > 0) { |
||
| 147 | foreach ($obj->hasOne() as $relName => $relClass) { |
||
| 148 | if (!singleton($relClass)->stat('api_access')) { |
||
| 149 | continue; |
||
| 150 | } |
||
| 151 | |||
| 152 | // Field filtering |
||
| 153 | if ($fields && !in_array($relName, $fields)) { |
||
| 154 | continue; |
||
| 155 | } |
||
| 156 | if ($this->customRelations && !in_array($relName, $this->customRelations)) { |
||
| 157 | continue; |
||
| 158 | } |
||
| 159 | |||
| 160 | $fieldName = $relName . 'ID'; |
||
| 161 | if ($obj->$fieldName) { |
||
| 162 | $href = Director::absoluteURL($this->config()->api_base . "$relClass/" . $obj->$fieldName); |
||
| 163 | } else { |
||
| 164 | $href = Director::absoluteURL($this->config()->api_base . "$className/$id/$relName"); |
||
| 165 | } |
||
| 166 | $xml .= "<$relName linktype=\"has_one\" href=\"$href.xml\" id=\"" . $obj->$fieldName |
||
| 167 | . "\"></$relName>\n"; |
||
| 168 | } |
||
| 169 | |||
| 170 | foreach ($obj->hasMany() as $relName => $relClass) { |
||
| 171 | //remove dot notation from relation names |
||
| 172 | $parts = explode('.', $relClass); |
||
| 173 | $relClass = array_shift($parts); |
||
| 174 | if (!singleton($relClass)->stat('api_access')) { |
||
| 175 | continue; |
||
| 176 | } |
||
| 177 | // backslashes in FQCNs kills both URIs and XML |
||
| 178 | $relClass = $this->sanitiseClassName($relClass); |
||
| 179 | |||
| 180 | // Field filtering |
||
| 181 | if ($fields && !in_array($relName, $fields)) { |
||
| 182 | continue; |
||
| 183 | } |
||
| 184 | if ($this->customRelations && !in_array($relName, $this->customRelations)) { |
||
| 185 | continue; |
||
| 186 | } |
||
| 187 | |||
| 188 | $xml .= "<$relName linktype=\"has_many\" href=\"$objHref/$relName.xml\">\n"; |
||
| 189 | $items = $obj->$relName(); |
||
| 190 | if ($items) { |
||
| 191 | foreach ($items as $item) { |
||
| 192 | $href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID"); |
||
| 193 | $xml .= "<$relClass href=\"$href.xml\" id=\"{$item->ID}\"></$relClass>\n"; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | $xml .= "</$relName>\n"; |
||
| 197 | } |
||
| 198 | |||
| 199 | foreach ($obj->manyMany() as $relName => $relClass) { |
||
| 200 | $relClass = RestfulServer::parseRelationClass($relClass); |
||
| 201 | |||
| 202 | //remove dot notation from relation names |
||
| 203 | $parts = explode('.', $relClass); |
||
| 204 | $relClass = array_shift($parts); |
||
| 205 | if (!singleton($relClass)->stat('api_access')) { |
||
| 206 | continue; |
||
| 207 | } |
||
| 208 | // backslashes in FQCNs kills both URIs and XML |
||
| 209 | $relClass = $this->sanitiseClassName($relClass); |
||
| 210 | |||
| 211 | // Field filtering |
||
| 212 | if ($fields && !in_array($relName, $fields)) { |
||
| 213 | continue; |
||
| 214 | } |
||
| 215 | if ($this->customRelations && !in_array($relName, $this->customRelations)) { |
||
| 216 | continue; |
||
| 217 | } |
||
| 218 | |||
| 219 | $xml .= "<$relName linktype=\"many_many\" href=\"$objHref/$relName.xml\">\n"; |
||
| 220 | $items = $obj->$relName(); |
||
| 221 | if ($items) { |
||
| 222 | foreach ($items as $item) { |
||
| 223 | $href = Director::absoluteURL($this->config()->api_base . "$relClass/$item->ID"); |
||
| 224 | $xml .= "<$relClass href=\"$href.xml\" id=\"{$item->ID}\"></$relClass>\n"; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | $xml .= "</$relName>\n"; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | $xml .= "</$className>"; |
||
| 232 | |||
| 233 | return $xml; |
||
| 234 | } |
||
| 267 |