| Conditions | 12 |
| Paths | 75 |
| Total Lines | 67 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 84 | public static function simplifyGoogleUrl(string $url): string |
||
| 85 | { |
||
| 86 | if (!self::isGoogleBookURL($url)) { |
||
| 87 | // not DomainException for live testing with OuvrageOptimize |
||
| 88 | throw new Exception('not a Google Book URL'); |
||
| 89 | } |
||
| 90 | |||
| 91 | $gooDat = self::parseGoogleBookQuery($url); |
||
| 92 | if (empty($gooDat['id'])) { |
||
| 93 | throw new DomainException("no GoogleBook 'id' in URL"); |
||
| 94 | } |
||
| 95 | if (!preg_match('#[0-9A-Za-z_\-]{12}#', $gooDat['id'])) { |
||
| 96 | throw new DomainException("GoogleBook 'id' malformed"); |
||
| 97 | } |
||
| 98 | |||
| 99 | $dat = []; |
||
| 100 | // keep only a few parameters (+'q' ?) |
||
| 101 | // q : keywords search / dq : quoted phrase search |
||
| 102 | // q can be empty !!!! |
||
| 103 | $keeps = ['id', 'pg', 'printsec', 'q', 'dq']; |
||
| 104 | foreach ($keeps as $keep) { |
||
| 105 | if (isset($gooDat[$keep])) { |
||
| 106 | $dat[$keep] = $gooDat[$keep]; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | // 1 exemple : https://fr.wikipedia.org/w/index.php?title=Foudre_de_Catatumbo&diff=next&oldid=168721836&diffmode=source |
||
| 111 | // 1. mettre URL &dq= pour final |
||
| 112 | // |
||
| 113 | // 2. si q!=dq (changement ultérieur formulaire recherche) alors q= prévaut pour résultat final |
||
| 114 | // 2. mettre URL &q pour final |
||
| 115 | // |
||
| 116 | // 3. Recherche global sur http://books.google.fr => pg= dq= (#q= avec q==dq) |
||
| 117 | // 3. dans ce cas (q==dq), url final avec seulement dq= donne résultat OK |
||
| 118 | // |
||
| 119 | // 4 . if you use a url without &redir_esc=y#v=onepage for a book with "Preview" available, |
||
| 120 | // usually &dq shows the highlighted text in full page view whereas &q shows the snippet view (so you have to |
||
| 121 | // click on the snippet to see the full page). |
||
| 122 | // &dq allows highlighting in books where there is "Preview" available and &pg=PTx is in the URL |
||
| 123 | // |
||
| 124 | // #v=onepage ou #v=snippet |
||
| 125 | if (isset($dat['q']) && isset($dat['dq'])) { |
||
| 126 | // si q==dq alors dq prévaut pour affichage (sinon affichage différent avec url seulement q=) |
||
| 127 | if ($dat['q'] === $dat['dq']) { |
||
| 128 | unset($dat['q']); |
||
| 129 | } // si q!=dq (exemple : nouveaux mots clés dans formulaire recherche) alors q= prévaut pour résultat final |
||
| 130 | else { |
||
| 131 | unset($dat['dq']); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | if (empty($dat['q'])) { |
||
| 135 | unset($dat['q']); |
||
| 136 | } |
||
| 137 | if (empty($dat['dq'])) { |
||
| 138 | unset($dat['dq']); |
||
| 139 | } |
||
| 140 | |||
| 141 | $googleURL = self::DEFAULT_GOOGLEBOOKS_URL; |
||
| 142 | |||
| 143 | // domain .com .fr |
||
| 144 | $gooDomain = self::parseGoogleDomain($url); |
||
| 145 | if ($gooDomain) { |
||
| 146 | $googleURL = str_replace('.com', $gooDomain, $googleURL); |
||
| 147 | } |
||
| 148 | |||
| 149 | // todo http_build_query() process an urlencode, but a not encoded q= value ("fu+bar") is beautiful |
||
| 150 | return $googleURL.'?'.http_build_query($dat); |
||
| 151 | } |
||
| 199 |