| Total Complexity | 45 |
| Total Lines | 166 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AuthorConverterTrait 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 AuthorConverterTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | trait AuthorConverterTrait |
||
| 13 | { |
||
| 14 | protected function convertJsonLDAuthor(array $data, int $index): ?string |
||
| 15 | { |
||
| 16 | // author=Bob |
||
| 17 | // Why restricted to index 1 ?? |
||
| 18 | if (1 === $index && $this->extractAuthorFromString($data)) { |
||
| 19 | return $this->extractAuthorFromString($data); |
||
| 20 | } |
||
| 21 | |||
| 22 | if (isset($data['author']) && is_string($data['author']) && $index === 1) { |
||
| 23 | return html_entity_decode($data['author']); |
||
| 24 | } |
||
| 25 | |||
| 26 | // author ['name'=>'Bob','@type'=>'Person'] |
||
| 27 | $simpleAuthor = $this->extractFromSimpleArray($data, $index); |
||
| 28 | if ($simpleAuthor) { |
||
| 29 | return $simpleAuthor; |
||
| 30 | } |
||
| 31 | |||
| 32 | // author [ 0 => ['name'=>'Bob'], 1=> ...] |
||
| 33 | $extractAuthorFromArray = $this->extractAuthorFromIndexedArray($data, $index); |
||
| 34 | if ($extractAuthorFromArray) { |
||
| 35 | return $extractAuthorFromArray; |
||
| 36 | } |
||
| 37 | |||
| 38 | return null; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * author=Bob |
||
| 43 | */ |
||
| 44 | protected function extractAuthorFromString(array $data): ?string |
||
| 45 | { |
||
| 46 | if (isset($data['author']) && is_string($data['author'])) { |
||
| 47 | return html_entity_decode($data['author']); |
||
| 48 | } |
||
| 49 | |||
| 50 | return null; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * author ['name'=>'Bob','@type'=>'Person'] |
||
| 55 | */ |
||
| 56 | protected function extractFromSimpleArray(array $data, int $index): ?string |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * author [ 0 => ['name'=>'Bob'], 1=> ...] |
||
| 76 | */ |
||
| 77 | protected function extractAuthorFromIndexedArray(array $data, int $index): ?string |
||
| 78 | { |
||
| 79 | if (isset($data['author']) && isset($data['author'][$index]) |
||
| 80 | && (!isset($data['author'][$index]['@type']) |
||
| 81 | || 'Person' === $data['author'][$index]['@type']) |
||
| 82 | ) { |
||
| 83 | if (isset($data['author'][$index]['name']) && is_string($data['author'][$index]['name'])) { |
||
| 84 | return html_entity_decode($data['author'][$index]['name']); |
||
| 85 | } |
||
| 86 | |||
| 87 | // "author" => [ "@type" => "Person", "name" => [] ] |
||
| 88 | if (isset($data['author'][$index]['name'][0])) { |
||
| 89 | return html_entity_decode((string) $data['author'][$index]['name'][0]); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | return null; |
||
| 94 | } |
||
| 95 | |||
| 96 | protected function convertInstitutionnel($data): ?string |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Used by OpenGraphMapper (so also ExternMapper). |
||
| 109 | * If more than 2 authors, reduce to the 2 first names. |
||
| 110 | */ |
||
| 111 | protected function shrinkMultiAuthors(?string $authors): ?string |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * If more than 2 authors, return "oui" for the bibliographic parameter "et al.". |
||
| 130 | */ |
||
| 131 | protected function isAuthorsEtAl(?string $authors): bool |
||
| 132 | { |
||
| 133 | if (empty($authors)) { |
||
| 134 | return false; |
||
| 135 | } |
||
| 136 | return substr_count($authors, ',') >= 2 || substr_count($authors, ';') >= 1; |
||
| 137 | } |
||
| 138 | |||
| 139 | protected function cleanAuthor(?string $str = null): ?string |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Wikification des noms/acronymes d'agences de presse. |
||
| 158 | * Note : utiliser APRES clean() et cleanAuthor() sinon bug "|" |
||
| 159 | */ |
||
| 160 | protected function wikifyPressAgency(?string $str): ?string |
||
| 178 | } |
||
| 179 | } |