| Conditions | 7 |
| Paths | 8 |
| Total Lines | 70 |
| 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 |
||
| 175 | public function createKMLSitemap($mediaID) { |
||
| 176 | global $conf; |
||
| 177 | $namespace = getNS($mediaID); |
||
| 178 | |||
| 179 | $KMLstart = '<?xml version="1.0" encoding="UTF-8"?>' . DOKU_LF; |
||
| 180 | $KMLstart .= '<kml xmlns="http://www.opengis.net/kml/2.2" '; |
||
| 181 | $KMLstart .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '; |
||
| 182 | $KMLstart .= 'xmlns:atom="http://www.w3.org/2005/Atom"'; |
||
| 183 | $KMLstart .= ' xsi:schemaLocation="http://www.opengis.net/kml/2.2 '; |
||
| 184 | $KMLstart .= 'http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd">' . DOKU_LF; |
||
| 185 | $KMLstart .= '<Document id="root_doc">' . DOKU_LF; |
||
| 186 | $KMLstart .= '<name>' . $conf['title'] . ' spatial sitemap</name>' . DOKU_LF; |
||
| 187 | $KMLstart .= '<atom:link href="' . DOKU_URL . '" rel="related" type="text/html" />' . DOKU_LF; |
||
| 188 | $KMLstart .= '<!-- atom:updated>' . date(DATE_ATOM) . '</atom:updated -->' . DOKU_LF; |
||
| 189 | $KMLstart .= '<Style id="icon"><IconStyle><color>ffffffff</color><scale>1</scale>'; |
||
| 190 | $KMLstart .= '<Icon><href>' |
||
| 191 | . DOKU_BASE . 'lib/plugins/spatialhelper/wikiitem.png</href></Icon></IconStyle></Style>' . DOKU_LF; |
||
| 192 | |||
| 193 | $KMLend = '</Document>' . DOKU_LF . '</kml>'; |
||
| 194 | |||
| 195 | io_createNamespace($mediaID, 'media'); |
||
| 196 | @touch(mediaFN($mediaID)); |
||
| 197 | @chmod(mediaFN($mediaID), $conf['fmode']); |
||
| 198 | |||
| 199 | $fh = fopen(mediaFN($mediaID), 'w'); |
||
| 200 | fwrite($fh, $KMLstart); |
||
| 201 | |||
| 202 | foreach($this->spatial_idx as $idxEntry) { |
||
| 203 | // get list of id's |
||
| 204 | foreach($idxEntry as $id) { |
||
| 205 | // for document item in the index |
||
| 206 | if(strpos($id, 'media__', 0) !== 0) { |
||
| 207 | if($this->skipPage($id, $namespace)) { |
||
| 208 | continue; |
||
| 209 | } |
||
| 210 | |||
| 211 | $meta = p_get_metadata($id); |
||
| 212 | |||
| 213 | // $desc = p_render('xhtmlsummary', p_get_instructions($meta['description']['abstract']), $info); |
||
| 214 | $desc = '<p>' . strip_tags($meta['description']['abstract']) . '</p>'; |
||
| 215 | $desc .= '<p><a href="' . wl($id, '', true) . '">' . $meta['title'] . '</a></p>'; |
||
| 216 | |||
| 217 | // create an entry and store it |
||
| 218 | $plcm = '<Placemark id="crc32-' . hash('crc32', $id) . '">' . DOKU_LF; |
||
| 219 | $plcm .= ' <name>' . $meta['title'] . '</name>' . DOKU_LF; |
||
| 220 | // TODO escape quotes in: title="' . $meta['title'] . '" |
||
| 221 | $plcm .= ' <atom:link href="' . wl($id, '' . true) . '" rel="alternate" type="text/html" />' |
||
| 222 | . DOKU_LF; |
||
| 223 | if(!empty($meta['creator'])) { |
||
| 224 | $entry .= ' <atom:author><atom:name>' . $meta['creator'] . '</atom:name></atom:author>' |
||
| 225 | . DOKU_LF; |
||
| 226 | } |
||
| 227 | |||
| 228 | $plcm .= ' <description><![CDATA[' . $desc . ']]></description>' . DOKU_LF; |
||
| 229 | $plcm .= ' <styleUrl>#icon</styleUrl>' . DOKU_LF; |
||
| 230 | |||
| 231 | $plcm .= ' <Point><coordinates>' . $meta['geo']['lon'] . ',' . $meta['geo']['lat']; |
||
| 232 | if($meta['geo']['alt']) { |
||
| 233 | $plcm .= ',' . $meta['geo']['alt']; |
||
| 234 | } |
||
| 235 | $plcm .= '</coordinates></Point>' . DOKU_LF; |
||
| 236 | $plcm .= '</Placemark>' . DOKU_LF; |
||
| 237 | |||
| 238 | fwrite($fh, $plcm); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | fwrite($fh, $KMLend); |
||
| 243 | return fclose($fh); |
||
| 244 | } |
||
| 245 | } |
||
| 246 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.