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