| Total Complexity | 72 |
| Total Lines | 278 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 1 |
Complex classes like WebMapper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WebMapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class WebMapper implements MapperInterface |
||
| 27 | { |
||
| 28 | use ArrayProcessTrait, WebOGMapperTrait, WebLDMapperTrait; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Logger |
||
| 32 | */ |
||
| 33 | private $log; |
||
| 34 | |||
| 35 | public function __construct(LoggerInterface $log) |
||
| 36 | { |
||
| 37 | $this->log = $log; |
||
|
1 ignored issue
–
show
|
|||
| 38 | } |
||
| 39 | |||
| 40 | public function process($data): array |
||
| 41 | { |
||
| 42 | $dat = $this->processMapping($data); |
||
| 43 | |||
| 44 | return $this->postProcess($dat); |
||
| 45 | } |
||
| 46 | |||
| 47 | protected function processMapping($data): array |
||
| 48 | { |
||
| 49 | if (!empty($data['JSON-LD'])) { |
||
| 50 | if ($this->checkJSONLD($data['JSON-LD'])) { |
||
| 51 | return $this->mapArticleDataFromJSONLD($data['JSON-LD']); |
||
| 52 | } |
||
| 53 | // gestion des multiples objets comme Figaro |
||
| 54 | foreach ($data['JSON-LD'] as $dat) { |
||
| 55 | if (is_array($dat) && $this->checkJSONLD($dat)) { |
||
| 56 | return $this->mapArticleDataFromJSONLD($dat); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | if (!empty($data['meta'])) { |
||
| 61 | // Dublin Core mapping included ;-) |
||
| 62 | return $this->mapLienwebFromOpenGraph($data['meta']); |
||
| 63 | } |
||
| 64 | |||
| 65 | return []; |
||
| 66 | } |
||
| 67 | |||
| 68 | protected function checkJSONLD(array $jsonLD): bool |
||
| 69 | { |
||
| 70 | return isset($jsonLD['headline']) && isset($jsonLD['@type']); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * todo Refac/move domain special mapping |
||
| 75 | * |
||
| 76 | * @param array $dat |
||
| 77 | * |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | protected function postProcess(array $dat): array |
||
| 81 | { |
||
| 82 | $dat = $this->deleteEmptyValueArray($dat); |
||
| 83 | if (isset($dat['langue']) && 'fr' === $dat['langue']) { |
||
| 84 | unset($dat['langue']); |
||
| 85 | } |
||
| 86 | |||
| 87 | if (isset($dat['site']) && $dat['site'] === 'Gallica') { |
||
| 88 | unset($dat['format']); // "vidéo"... |
||
| 89 | } |
||
| 90 | |||
| 91 | return $dat; |
||
| 92 | } |
||
| 93 | |||
| 94 | protected function isAnArticle(?string $str): bool |
||
| 101 | } |
||
| 102 | |||
| 103 | protected function convertURLaccess($data): ?string |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Réduit le nombre d'auteurs si > 3. |
||
| 121 | * En $modeEtAll=true vérification pour "et al.=oui". |
||
| 122 | * TODO : wikifyPressAgency() |
||
| 123 | * |
||
| 124 | * @param string|null $authors |
||
| 125 | * @param bool $modeEtAl |
||
| 126 | * |
||
| 127 | * @return string|null |
||
| 128 | */ |
||
| 129 | protected function authorsEtAl(?string $authors, $modeEtAl = false): ?string |
||
| 130 | { |
||
| 131 | if (empty($authors)) { |
||
| 132 | return null; |
||
| 133 | } |
||
| 134 | // conserve juste les 3 premiers auteurs TODO : refactor |
||
| 135 | // Bob, Martin ; Yul, Bar ; ... ; ... |
||
| 136 | if (preg_match('#([^;]+;[^;]+);[^;]+;.+#', $authors, $matches)) { |
||
| 137 | return ($modeEtAl) ? 'oui' : $matches[1]; |
||
| 138 | } |
||
| 139 | // Bob Martin, Yul Bar, ..., ...,... |
||
| 140 | if (preg_match('#([^,]+,[^,]+),[^,]+,.+#', $authors, $matches)) { |
||
| 141 | return ($modeEtAl) ? 'oui' : $matches[1]; |
||
| 142 | } |
||
| 143 | |||
| 144 | return ($modeEtAl) ? null : $authors; |
||
| 145 | } |
||
| 146 | |||
| 147 | protected function convertDCpage(array $meta): ?string |
||
| 148 | { |
||
| 149 | if (isset($meta['citation_firstpage'])) { |
||
| 150 | $page = $meta['citation_firstpage']; |
||
| 151 | if (isset($meta['citation_lastpage'])) { |
||
| 152 | $page .= '–'.$meta['citation_lastpage']; |
||
| 153 | } |
||
| 154 | |||
| 155 | return (string)$page; |
||
| 156 | } |
||
| 157 | |||
| 158 | return null; |
||
| 159 | } |
||
| 160 | |||
| 161 | protected function clean(?string $str = null): ?string |
||
| 162 | { |
||
| 163 | if ($str === null) { |
||
| 164 | return null; |
||
| 165 | } |
||
| 166 | $str = str_replace([''', "\n", " ", "|"], ["'", '', ' ', '/'], $str); |
||
| 167 | |||
| 168 | return html_entity_decode($str); |
||
| 169 | } |
||
| 170 | |||
| 171 | protected function convertOGtype2format(?string $ogType): ?string |
||
| 172 | { |
||
| 173 | if (empty($ogType)) { |
||
| 174 | return null; |
||
| 175 | } |
||
| 176 | // og:type = default: website / video.movie / video.tv_show video.other / article, book, profile |
||
| 177 | if (strpos($ogType, 'video') !== false) { |
||
| 178 | return 'vidéo'; |
||
| 179 | } |
||
| 180 | if (strpos($ogType, 'book') !== false) { |
||
| 181 | return 'livre'; |
||
| 182 | } |
||
| 183 | |||
| 184 | return null; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * https://developers.facebook.com/docs/internationalization#locales |
||
| 189 | */ |
||
| 190 | protected function convertLangue(?string $lang = null): ?string |
||
| 191 | { |
||
| 192 | if (empty($lang)) { |
||
| 193 | return null; |
||
| 194 | } |
||
| 195 | // en_GB |
||
| 196 | if (preg_match('#^([a-z]{2})_[A-Z]{2}$#', $lang, $matches)) { |
||
| 197 | return $matches[1]; |
||
| 198 | } |
||
| 199 | |||
| 200 | return Language::all2wiki($lang); |
||
| 201 | } |
||
| 202 | |||
| 203 | protected function convertAuteur($data, $indice) |
||
| 204 | { |
||
| 205 | // author=Bob |
||
| 206 | if (isset($data['author']) && is_string($data['author']) && $indice === 1) { |
||
| 207 | return html_entity_decode($data['author']); |
||
| 208 | } |
||
| 209 | |||
| 210 | // author ['name'=>'Bob','@type'=>'Person'] |
||
| 211 | if (0 === $indice |
||
| 212 | && isset($data['author']) |
||
| 213 | && isset($data['author']['name']) |
||
| 214 | && (!isset($data['author']['@type']) |
||
| 215 | || 'Person' === $data['author']['@type']) |
||
| 216 | ) { |
||
| 217 | if (is_string($data['author']['name'])) { |
||
| 218 | return html_entity_decode($data['author']['name']); |
||
| 219 | } |
||
| 220 | |||
| 221 | return html_entity_decode($data['author']['name'][0]); |
||
| 222 | } |
||
| 223 | |||
| 224 | // author [ 0 => ['name'=>'Bob'], 1=> ...] |
||
| 225 | if (isset($data['author']) && isset($data['author'][$indice]) |
||
| 226 | && (!isset($data['author'][$indice]['@type']) |
||
| 227 | || 'Person' === $data['author'][$indice]['@type']) |
||
| 228 | ) { |
||
| 229 | if (isset($data['author'][$indice]['name']) && is_string($data['author'][$indice]['name'])) { |
||
| 230 | return html_entity_decode($data['author'][$indice]['name']); |
||
| 231 | } |
||
| 232 | |||
| 233 | // "author" => [ "@type" => "Person", "name" => [] ] |
||
| 234 | return html_entity_decode($data['author'][$indice]['name'][0]); |
||
| 235 | } |
||
| 236 | |||
| 237 | return null; |
||
| 238 | } |
||
| 239 | |||
| 240 | protected function convertInstitutionnel($data) |
||
| 241 | { |
||
| 242 | if (isset($data['author']) && isset($data['author'][0]) && isset($data['author'][0]['@type']) |
||
| 243 | && 'Person' !== $data['author'][0]['@type'] |
||
| 244 | ) { |
||
| 245 | return html_entity_decode($data['author'][0]['name']); |
||
| 246 | } |
||
| 247 | |||
| 248 | return null; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $str |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | protected function convertDate(?string $str): ?string |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Wikification des noms/acronymes d'agences de presse. |
||
| 280 | * |
||
| 281 | * @param string $str |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | protected function wikifyPressAgency(?string $str): ?string |
||
| 286 | { |
||
| 287 | if (empty($str)) { |
||
| 306 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.