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