| Conditions | 10 |
| Paths | 21 |
| Total Lines | 68 |
| Code Lines | 22 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 174 | public static function simplifyGoogleUrl(string $url): string |
||
| 175 | { |
||
| 176 | if (!self::isGoogleBookURL($url)) { |
||
| 177 | // not DomainException for live testing with OuvrageOptimize |
||
| 178 | throw new Exception('not a Google Book URL'); |
||
| 179 | } |
||
| 180 | |||
| 181 | $gooDat = self::parseGoogleBookQuery($url); |
||
| 182 | if (empty($gooDat['id'])) { |
||
| 183 | throw new DomainException("no GoogleBook 'id' in URL"); |
||
| 184 | } |
||
| 185 | if (!preg_match('#[0-9A-Za-z_\-]{12}#', $gooDat['id'])) { |
||
| 186 | throw new DomainException("GoogleBook 'id' malformed"); |
||
| 187 | } |
||
| 188 | |||
| 189 | // clean encoding q= dq= |
||
| 190 | // if(isset($gooDat['q'])) { |
||
| 191 | // $gooDat['q'] = self::googleUrlEncode($gooDat['q']); |
||
| 192 | // } |
||
| 193 | // if(isset($gooDat['dq'])) { |
||
| 194 | // $gooDat['dq'] = self::googleUrlEncode($gooDat['dq']); |
||
| 195 | // } |
||
| 196 | |||
| 197 | $dat = []; |
||
| 198 | // keep only a few parameters (+'q' ?) |
||
| 199 | // q : keywords search / dq : quoted phrase search |
||
| 200 | $keeps = ['id', 'pg', 'printsec', 'q', 'dq']; |
||
| 201 | foreach ($keeps as $keep) { |
||
| 202 | if (!empty($gooDat[$keep])) { |
||
| 203 | $dat[$keep] = $gooDat[$keep]; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | // 1 exemple : https://fr.wikipedia.org/w/index.php?title=Foudre_de_Catatumbo&diff=next&oldid=168721836&diffmode=source |
||
| 208 | // 1. mettre URL &dq= pour final |
||
| 209 | // |
||
| 210 | // 2. si q!=dq (changement ultérieur formulaire recherche) alors q= prévaut pour résultat final |
||
| 211 | // 2. mettre URL &q pour final |
||
| 212 | // |
||
| 213 | // 3. Recherche global sur http://books.google.fr => pg= dq= (#q= avec q==dq) |
||
| 214 | // 3. dans ce cas (q==dq), url final avec seulement dq= donne résultat OK |
||
| 215 | // |
||
| 216 | // 4 . if you use a url without &redir_esc=y#v=onepage for a book with "Preview" available, |
||
| 217 | // usually &dq shows the highlighted text in full page view whereas &q shows the snippet view (so you have to |
||
| 218 | // click on the snippet to see the full page). |
||
| 219 | // &dq allows highlighting in books where there is "Preview" available and &pg=PTx is in the URL |
||
| 220 | // |
||
| 221 | // #v=onepage ou #v=snippet |
||
| 222 | if (isset($dat['q']) && isset($dat['dq'])) { |
||
| 223 | // si q==dq alors dq prévaut pour affichage (sinon affichage différent avec url seulement q=) |
||
| 224 | if ($dat['q'] === $dat['dq']) { |
||
| 225 | unset($dat['q']); |
||
| 226 | } // si q!=dq (exemple : nouveaux mots clés dans formulaire recherche) alors q= prévaut pour résultat final |
||
| 227 | else { |
||
| 228 | unset($dat['dq']); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | $googleURL = self::DEFAULT_GOOGLEBOOK_URL; |
||
| 233 | |||
| 234 | // domain .com .fr |
||
| 235 | $gooDomain = self::parseGoogleDomain($url); |
||
| 236 | if ($gooDomain) { |
||
| 237 | $googleURL = str_replace('.com', $gooDomain, $googleURL); |
||
| 238 | } |
||
| 239 | |||
| 240 | // todo http_build_query process an urlencode, but a not encoded q= value ("fu+bar") is beautiful |
||
| 241 | return $googleURL.'?'.http_build_query($dat); |
||
| 242 | } |
||
| 297 |