Conditions | 13 |
Paths | 24 |
Total Lines | 66 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
91 | public function convertGBurl2OuvrageCitation(string $url): string |
||
92 | { |
||
93 | if (!GoogleLivresTemplate::isGoogleBookURL($url)) { |
||
94 | throw new \DomainException('Pas de URL Google Books'); |
||
95 | } |
||
96 | |||
97 | $gooDat = GoogleLivresTemplate::parseGoogleBookQuery($url); |
||
98 | if (empty($gooDat['id'])) { |
||
99 | throw new \DomainException('Pas de ID Google Books'); |
||
100 | } |
||
101 | |||
102 | try { |
||
103 | $ouvrage = $this->generateOuvrageFromGoogleData($gooDat['id']); |
||
104 | } catch (\Throwable $e) { |
||
105 | // ID n'existe pas sur Google Books |
||
106 | if (strpos($e->getMessage(), '404 Not Found') |
||
107 | && strpos($e->getMessage(), '"message": "The volume ID could n') |
||
108 | ) { |
||
109 | return sprintf( |
||
110 | '{{lien brisé |url= %s |titre= %s |brisé le=%s}}', |
||
111 | $url, |
||
112 | 'Ouvrage inexistant sur Google Books', |
||
113 | date('d-m-Y') |
||
114 | ); |
||
115 | } |
||
116 | throw $e; |
||
117 | } |
||
118 | |||
119 | |||
120 | $cleanUrl = GoogleLivresTemplate::simplifyGoogleUrl($url); |
||
121 | $ouvrage->unsetParam('présentation en ligne'); |
||
122 | $ouvrage->setParam('lire en ligne', $cleanUrl); |
||
123 | $ouvrage->userSeparator = ' |'; |
||
124 | |||
125 | // Si titre absent |
||
126 | if (empty($ouvrage->getParam('titre'))) { |
||
127 | throw new \DomainException("Ouvrage sans titre (data Google?)"); |
||
128 | } |
||
129 | |||
130 | // Google page => 'passage' |
||
131 | if (!empty($gooDat['pg'])) { |
||
132 | if (preg_match('#(?:PA|PT)([0-9]+)$#', $gooDat['pg'], $matches)) { |
||
133 | // Exclusion de page=1, page=2 (vue par défaut sur Google Book) |
||
134 | if (intval($matches[1]) >= 3) { |
||
135 | $page = $matches[1]; |
||
136 | } |
||
137 | } |
||
138 | // conversion chiffres Romain pour PR |
||
139 | if (preg_match('#PR([0-9]+)$#', $gooDat['pg'], $matches)) { |
||
140 | // Exclusion de page=1, page=2 (vue par défaut sur Google Book) |
||
141 | if (intval($matches[1]) >= 3) { |
||
142 | $page = NumberUtil::arab2roman(intval($matches[1]), true); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | if (!empty($page)) { |
||
147 | $ouvrage->setParam('passage', $page); |
||
148 | // ajout commentaire '<!-- utile? -->' ? |
||
149 | } |
||
150 | } |
||
151 | |||
152 | $optimizer = new OuvrageOptimize($ouvrage); |
||
153 | $optimizer->doTasks(); |
||
154 | $ouvrage2 = $optimizer->getOuvrage(); |
||
155 | |||
156 | return $ouvrage2->serialize(); |
||
157 | } |
||
218 |