| Conditions | 10 |
| Paths | 32 |
| Total Lines | 91 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 152 | protected function getXmlVersionOfResult(array $result) |
||
| 153 | { |
||
| 154 | $w = new \XMLWriter(); |
||
| 155 | $w->openMemory(); |
||
| 156 | $w->startDocument('1.0'); |
||
| 157 | |||
| 158 | // sparql (root element) |
||
| 159 | $w->startElement('sparql'); |
||
| 160 | $w->writeAttribute('xmlns', 'http://www.w3.org/2005/sparql-results#'); |
||
| 161 | |||
| 162 | // sparql > head |
||
| 163 | $w->startElement('head'); |
||
| 164 | |||
| 165 | foreach ($result['result']['variables'] as $var) { |
||
| 166 | $w->startElement('variable'); |
||
| 167 | $w->writeAttribute('name', $var); |
||
| 168 | $w->endElement(); |
||
| 169 | } |
||
| 170 | |||
| 171 | // end sparql > head |
||
| 172 | $w->endElement(); |
||
| 173 | |||
| 174 | // sparql > results |
||
| 175 | $w->startElement('results'); |
||
| 176 | |||
| 177 | foreach ($result['result']['rows'] as $row) { |
||
| 178 | /* |
||
| 179 | example: |
||
| 180 | |||
| 181 | <result> |
||
| 182 | <binding name="s"> |
||
| 183 | <uri>http://example/s1</uri> |
||
| 184 | </binding> |
||
| 185 | </result> |
||
| 186 | */ |
||
| 187 | |||
| 188 | // new result element |
||
| 189 | $w->startElement('result'); |
||
| 190 | |||
| 191 | foreach ($result['result']['variables'] as $var) { |
||
| 192 | if (empty($row[$var])) { |
||
| 193 | continue; |
||
| 194 | } |
||
| 195 | |||
| 196 | // sparql > results > result > binding |
||
| 197 | $w->startElement('binding'); |
||
| 198 | $w->writeAttribute('name', $var); |
||
| 199 | |||
| 200 | // if a variable type is set |
||
| 201 | if (isset($row[$var.' type'])) { |
||
| 202 | // uri |
||
| 203 | if ('uri' == $row[$var.' type']) { |
||
| 204 | // example: <uri>http://example/s1</uri> |
||
| 205 | $w->startElement('uri'); |
||
| 206 | $w->text($row[$var]); |
||
| 207 | $w->endElement(); |
||
| 208 | } elseif ('literal' == $row[$var.' type']) { |
||
| 209 | // example: <literal datatype="http://www.w3.org/2001/XMLSchema#integer">9</literal> |
||
| 210 | $w->startElement('literal'); |
||
| 211 | |||
| 212 | // its not part of the result set, but expected later on |
||
| 213 | if (true === ctype_digit($row[$var])) { |
||
| 214 | $w->writeAttribute('datatype', 'http://www.w3.org/2001/XMLSchema#integer'); |
||
| 215 | } |
||
| 216 | |||
| 217 | $w->text($row[$var]); |
||
| 218 | $w->endElement(); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | // end sparql > results > result > binding |
||
| 223 | $w->endElement(); |
||
| 224 | } |
||
| 225 | |||
| 226 | // end result |
||
| 227 | $w->endElement(); |
||
| 228 | } |
||
| 229 | |||
| 230 | // add <result></result> if no data were found |
||
| 231 | if (0 == \count($result['result']['rows'])) { |
||
| 232 | $w->startElement('result'); |
||
| 233 | $w->endElement(); |
||
| 234 | } |
||
| 235 | |||
| 236 | // end sparql > results |
||
| 237 | $w->endElement(); |
||
| 238 | |||
| 239 | // end sparql |
||
| 240 | $w->endElement(); |
||
| 241 | |||
| 242 | return new \SimpleXMLElement($w->outputMemory(true)); |
||
| 243 | } |
||
| 253 |