Conditions | 8 |
Paths | 16 |
Total Lines | 69 |
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 |
||
77 | public function createGeoRSSSitemap($mediaID) { |
||
78 | global $conf; |
||
79 | $namespace = getNS($mediaID); |
||
80 | |||
81 | $idTag = 'tag:' . parse_url(DOKU_URL, PHP_URL_HOST) . ','; |
||
82 | |||
83 | $RSSstart = '<?xml version="1.0" encoding="UTF-8"?>' . DOKU_LF; |
||
84 | $RSSstart .= '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" '; |
||
85 | $RSSstart .= 'xmlns:dc="http://purl.org/dc/elements/1.1/">' . DOKU_LF; |
||
86 | $RSSstart .= '<title>' . $conf['title'] . ' spatial feed</title>' . DOKU_LF; |
||
87 | if(!empty($conf['tagline'])) { |
||
88 | $RSSstart .= '<subtitle>' . $conf['tagline'] . '</subtitle>' . DOKU_LF; |
||
89 | } |
||
90 | $RSSstart .= '<dc:publisher>' . $conf['title'] . '</dc:publisher>' . DOKU_LF; |
||
91 | $RSSstart .= '<link href="' . DOKU_URL . '" />' . DOKU_LF; |
||
92 | $RSSstart .= '<link href="' . ml($mediaID, '', true, '&', true) . '" rel="self" />' . DOKU_LF; |
||
93 | $RSSstart .= '<updated>' . date(DATE_ATOM) . '</updated>' . DOKU_LF; |
||
94 | $RSSstart .= '<id>' . $idTag . date("Y-m-d") . ':' . parse_url(ml($mediaID), PHP_URL_PATH) |
||
95 | . '</id>' . DOKU_LF; |
||
96 | $RSSstart .= '<rights>' . $conf['license'] . '</rights>' . DOKU_LF; |
||
97 | |||
98 | $RSSend = '</feed>' . DOKU_LF; |
||
99 | |||
100 | io_createNamespace($mediaID, 'media'); |
||
101 | @touch(mediaFN($mediaID)); |
||
102 | @chmod(mediaFN($mediaID), $conf['fmode']); |
||
103 | $fh = fopen(mediaFN($mediaID), 'w'); |
||
104 | fwrite($fh, $RSSstart); |
||
105 | |||
106 | foreach($this->spatial_idx as $idxEntry) { |
||
107 | // get list of id's |
||
108 | foreach($idxEntry as $id) { |
||
109 | // for document item in the index |
||
110 | if(strpos($id, 'media__', 0) !== 0) { |
||
111 | if($this->skipPage($id, $namespace)) { |
||
112 | continue; |
||
113 | } |
||
114 | |||
115 | $meta = p_get_metadata($id); |
||
116 | |||
117 | // $desc = p_render('xhtmlsummary', p_get_instructions($meta['description']['abstract']), $info); |
||
118 | $desc = strip_tags($meta['description']['abstract']); |
||
119 | |||
120 | $entry = '<entry>' . DOKU_LF; |
||
121 | $entry .= ' <title>' . $meta['title'] . '</title>' . DOKU_LF; |
||
122 | $entry .= ' <summary>' . $desc . '</summary>' . DOKU_LF; |
||
123 | $entry .= ' <georss:point>' . $meta['geo']['lat'] . ' ' . $meta['geo']['lon'] |
||
124 | . '</georss:point>' . DOKU_LF; |
||
125 | if($meta['geo']['alt']) { |
||
126 | $entry .= ' <georss:elev>' . $meta['geo']['alt'] . '</georss:elev>' . DOKU_LF; |
||
127 | } |
||
128 | $entry .= ' <link href="' . wl($id) . '" rel="alternate" type="text/html" />' . DOKU_LF; |
||
129 | if(empty($meta['creator'])) { |
||
130 | $meta['creator'] = $conf['title']; |
||
131 | } |
||
132 | $entry .= ' <author><name>' . $meta['creator'] . '</name></author>' . DOKU_LF; |
||
133 | $entry .= ' <updated>' . date_iso8601($meta['date']['modified']) . '</updated>' . DOKU_LF; |
||
134 | $entry .= ' <published>' . date_iso8601($meta['date']['created']) . '</published>' . DOKU_LF; |
||
135 | $entry .= ' <id>' . $idTag . date("Y-m-d", $meta['date']['modified']) . ':' |
||
136 | . parse_url(wl($id), PHP_URL_PATH) . '</id>' . DOKU_LF; |
||
137 | $entry .= '</entry>' . DOKU_LF; |
||
138 | fwrite($fh, $entry); |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | |||
143 | fwrite($fh, $RSSend); |
||
144 | return fclose($fh); |
||
145 | } |
||
146 | |||
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.