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