| Conditions | 28 | 
| Paths | 11200 | 
| Total Lines | 198 | 
| Lines | 31 | 
| Ratio | 15.66 % | 
| 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  | 
            ||
| 99 | public function api($query)  | 
            ||
| 100 |     { | 
            ||
| 101 | |||
| 102 | // Ini Vars  | 
            ||
| 103 | $text = $image = $description = '';  | 
            ||
| 104 | |||
| 105 | // Convert Query to Lowercase for Headline  | 
            ||
| 106 | $strtolower = mb_strtolower($query);  | 
            ||
| 107 | |||
| 108 | // Convert Headlie to UTF-8 Uppercase Words  | 
            ||
| 109 | $headline = mb_convert_case($strtolower, MB_CASE_TITLE, 'UTF-8');  | 
            ||
| 110 | |||
| 111 | // If Query is complete Uppercase make also complete Uppercase Headline  | 
            ||
| 112 |         if ($query === strtoupper($query)) {  | 
            ||
| 113 | $headline = mb_strtoupper($query);  | 
            ||
| 114 | }  | 
            ||
| 115 | |||
| 116 | // Replace spaces in Query to Underscore and use Uppercase Words from Headline  | 
            ||
| 117 |         $query = str_replace(' ', '_', $headline); | 
            ||
| 118 | |||
| 119 | // In DEBUG Mode print Query  | 
            ||
| 120 | View Code Duplication |         if ($this->params['DEBUG']=='KEY' || $this->params['DEBUG']=='ALL') { | 
            |
| 121 | echo '<tt><b>Search-Keyword </b><xmp>#'.$query.'#</xmp></tt>';  | 
            ||
| 122 | }  | 
            ||
| 123 | |||
| 124 | // First search the API if betterResults==true  | 
            ||
| 125 |         if ($this->params['betterResults'] == true) { | 
            ||
| 126 | |||
| 127 | // Wikipedia API URL 1 - https://en.wikipedia.org/w/api.php  | 
            ||
| 128 | $url = 'https://'.$this->params['language'].'.wikipedia.org/w/api.php'.  | 
            ||
| 129 | '?action=query&format=json&list=search&srsearch=intitle:'.$query.  | 
            ||
| 130 | '&maxlag=1'; /* stop if wiki server is busy */  | 
            ||
| 131 | |||
| 132 | // If API Call 1 could be reached  | 
            ||
| 133 |             if ($api = $this->getContent($url, $this->params['userAgent'], $this->params['proxy'])) { | 
            ||
| 134 | |||
| 135 | // Decode the 1 Response  | 
            ||
| 136 | $data = json_decode($api, true);  | 
            ||
| 137 | |||
| 138 | // In DEBUG Mode print 1 Response  | 
            ||
| 139 | View Code Duplication |                 if ($this->params['DEBUG']=='API1' || $this->params['DEBUG']=='ALL') { | 
            |
| 140 | echo '<pre><b>Search API-Call (1) Response</b> ';  | 
            ||
| 141 | echo var_dump($data);  | 
            ||
| 142 | echo '</pre>';  | 
            ||
| 143 | }  | 
            ||
| 144 | |||
| 145 | // If there is a search Result  | 
            ||
| 146 |                 if (isset($data['query']['search'][0]['title'])) { | 
            ||
| 147 | |||
| 148 | // Set Headline  | 
            ||
| 149 | $headline = $data['query']['search'][0]['title'];  | 
            ||
| 150 | |||
| 151 | // Set the Query to the first Search Result (and replace Spaces with Underscores)  | 
            ||
| 152 |                     $query = str_replace(' ', '_', $data['query']['search'][0]['title']); | 
            ||
| 153 | |||
| 154 | // In DEBUG Mode print Found Keyword  | 
            ||
| 155 | View Code Duplication |                     if ($this->params['DEBUG']=='KEY' || $this->params['DEBUG']=='ALL') { | 
            |
| 156 | echo '<tt><b>Found Search-Keyword </b><xmp>#'.$query.'#</xmp></tt>';  | 
            ||
| 157 | }  | 
            ||
| 158 | }  | 
            ||
| 159 | |||
| 160 | // If Search Result is a 'Did you mean:' Hint  | 
            ||
| 161 |                 if (isset($data['query']['searchinfo']['suggestion'])) { | 
            ||
| 162 | |||
| 163 | // Set Text Hints depending on selected Language  | 
            ||
| 164 |                     if ($this->params['language'] == 'de') { | 
            ||
| 165 | $suggestionText = 'Meinten Sie: ';  | 
            ||
| 166 | }  | 
            ||
| 167 | else  | 
            ||
| 168 |                     { | 
            ||
| 169 | $suggestionText = 'Did you mean: ';  | 
            ||
| 170 | }  | 
            ||
| 171 | |||
| 172 | // Remove 'q=' Variable=Value Pair from Querystring  | 
            ||
| 173 |                     $QUERY_STRING = preg_replace('/'.('q'?'(\&|)q(\=(.*?)((?=&(?!amp\;))|$)|(.*?)\b)':'(\?.*)').'/i', '', $_SERVER['QUERY_STRING']); | 
            ||
| 174 | |||
| 175 | // Delete 'intitle:' from Suggestion Keyword  | 
            ||
| 176 |                     $suggestion = str_replace('intitle:', '', $data['query']['searchinfo']['suggestion']); | 
            ||
| 177 | |||
| 178 | // Make Suggestion UTF-8 Uppercase Words  | 
            ||
| 179 | $suggestion = mb_convert_case($suggestion, MB_CASE_TITLE, 'UTF-8');  | 
            ||
| 180 | |||
| 181 | // Make HTML Link for Suggestion  | 
            ||
| 182 | $description = $suggestionText.'<a href="?q='.  | 
            ||
| 183 |                          str_replace(' ', '_', $suggestion).$QUERY_STRING.'">'.$suggestion.'</a>';                          | 
            ||
| 184 | }  | 
            ||
| 185 | }  | 
            ||
| 186 | }  | 
            ||
| 187 | |||
| 188 | // Wikipedia API URL 2 - https://en.wikipedia.org/w/api.php  | 
            ||
| 189 | $url = 'https://'.$this->params['language'].  | 
            ||
| 190 | '.wikipedia.org/api/rest_v1/page/summary/'.$query.  | 
            ||
| 191 | '?maxlag=1'; /* stop if wiki server is busy */  | 
            ||
| 192 | |||
| 193 | // If API Call 2 could be reached  | 
            ||
| 194 |         if ($api = $this->getContent($url, $this->params['userAgent'], $this->params['proxy'])) { | 
            ||
| 195 | // Decode the 2 Response  | 
            ||
| 196 | $data = json_decode($api, true);  | 
            ||
| 197 | |||
| 198 | // In DEBUG Mode print 2 Response  | 
            ||
| 199 | View Code Duplication |             if ($this->params['DEBUG']=='API2' || $this->params['DEBUG']=='ALL') { | 
            |
| 200 | echo '<pre><b>Main API-Call (2) Response</b> ';  | 
            ||
| 201 | echo var_dump($data);  | 
            ||
| 202 | echo '</pre>';  | 
            ||
| 203 | }  | 
            ||
| 204 | |||
| 205 | // If there is an Image in the Search Result  | 
            ||
| 206 |             if (isset($data['originalimage']['source'])) { | 
            ||
| 207 | |||
| 208 | // If the DSGVO imageProxy should be use define it  | 
            ||
| 209 | $proxy = '';  | 
            ||
| 210 |                 if ($this->params['imageProxy']==true) { | 
            ||
| 211 | $proxy = 'wiki-image-proxy.php?url=';  | 
            ||
| 212 | }  | 
            ||
| 213 | |||
| 214 | // Build HTML for Image  | 
            ||
| 215 | $image = '<img src="'.$proxy.$data['thumbnail']['source'].'" />';  | 
            ||
| 216 | }  | 
            ||
| 217 | |||
| 218 | // Correct the Text  | 
            ||
| 219 |             $text = str_replace('#', ': ', $data['extract_html']); | 
            ||
| 220 | |||
| 221 | // If there is a Description  | 
            ||
| 222 |             if (isset($data['description'])) { | 
            ||
| 223 | |||
| 224 | // Correct the Description depending on selected Language  | 
            ||
| 225 | $description = str_replace(  | 
            ||
| 226 | array(  | 
            ||
| 227 | 'Wikimedia-Begriffsklärungsseite',  | 
            ||
| 228 | 'Disambiguation page providing links to topics that could be referred to by the same search term'  | 
            ||
| 229 | ),  | 
            ||
| 230 | array(  | 
            ||
| 231 | 'kann sich auf Folgendes beziehen',  | 
            ||
| 232 | 'may refer to the following'  | 
            ||
| 233 | ),  | 
            ||
| 234 | $data['description']  | 
            ||
| 235 | );  | 
            ||
| 236 | |||
| 237 | // Set Keyword to UTF-8 Uppercase Words of Query  | 
            ||
| 238 | $keyword = mb_convert_case($strtolower, MB_CASE_TITLE, 'UTF-8');  | 
            ||
| 239 | |||
| 240 | // Highlight the Query in the Text and Delete some Text  | 
            ||
| 241 | $text = str_replace(  | 
            ||
| 242 | array($keyword, ' may refer to', ' steht für:'),  | 
            ||
| 243 |                     array('<b class="hint">'.$keyword.'</b>', '', ''),  | 
            ||
| 244 | $text  | 
            ||
| 245 | );  | 
            ||
| 246 | }  | 
            ||
| 247 | |||
| 248 | // If there is no Article Text set a Default depending on selected Language  | 
            ||
| 249 | // e.g. q=Leonardo%20di%20caprio&language=de OR q=100&language=de  | 
            ||
| 250 |             if ($text == '') { | 
            ||
| 251 | $description = $image = '';  | 
            ||
| 252 | View Code Duplication |                 if ($this->params['language'] == 'de') { | 
            |
| 253 | $text = 'Zu diesem Stichwort ist kein Artikel vorhanden.';  | 
            ||
| 254 | }  | 
            ||
| 255 |                 else if($text == '') { | 
            ||
| 256 | $text = 'There is no article available for this keyword.';  | 
            ||
| 257 | }  | 
            ||
| 258 | return; // ONLY IF YOU WHANT NO OUTPUT !!  | 
            ||
| 259 | }  | 
            ||
| 260 | }  | 
            ||
| 261 | |||
| 262 | // Build the HTML Output  | 
            ||
| 263 | View Code Duplication |         if ($this->params['language']=='de') { | 
            |
| 264 | $moreAbout = 'Mehr über';  | 
            ||
| 265 | $from = 'bei';  | 
            ||
| 266 | }  | 
            ||
| 267 | else  | 
            ||
| 268 |         { | 
            ||
| 269 | $moreAbout = 'More about';  | 
            ||
| 270 | $from = 'from';  | 
            ||
| 271 | }  | 
            ||
| 272 | |||
| 273 | // Without any Search Result return nothing  | 
            ||
| 274 |         if ($text == '' && $description == '') { | 
            ||
| 275 | return '';  | 
            ||
| 276 | }  | 
            ||
| 277 | |||
| 278 | // With a Search Resuld build a Footer Link  | 
            ||
| 279 |         if ($text != '') { | 
            ||
| 280 | $footer = $moreAbout.' »'.$headline.'« '.$from;  | 
            ||
| 281 | $url = 'https://'.$this->params['language'].'.wikipedia.org/wiki/'.$query;  | 
            ||
| 282 | }  | 
            ||
| 283 |         else if ($description != '') { | 
            ||
| 284 | // Footer Link for Suggestion-Link  | 
            ||
| 285 | $footer = '';  | 
            ||
| 286 | $url = 'https://'.$this->params['language'].'.wikipedia.org/';  | 
            ||
| 287 | }  | 
            ||
| 288 | |||
| 289 | // Use the Template  | 
            ||
| 290 | ob_start();  | 
            ||
| 291 | include 'wiki2tpl.phtm';  | 
            ||
| 292 | |||
| 293 | // Return the HTML  | 
            ||
| 294 | return ob_get_clean();  | 
            ||
| 295 | |||
| 296 | }  | 
            ||
| 297 | |||
| 299 | 
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: