Passed
Push — master ( 5eccc7...1faa52 )
by Dispositif
02:45
created

ExternConverterTrait   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Test Coverage

Coverage 81.25%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 93
dl 0
loc 229
ccs 78
cts 96
cp 0.8125
rs 5.5199
c 1
b 0
f 0
wmc 56

11 Methods

Rating   Name   Duplication   Size   Complexity  
A wikifyPressAgency() 0 19 3
A convertInstitutionnel() 0 9 5
A convertDate() 0 20 4
A convertLangue() 0 11 3
A convertOGtype2format() 0 14 4
B convertURLaccess() 0 14 7
B authorsEtAl() 0 16 7
A convertDCpage() 0 12 3
A isAnArticle() 0 7 2
C convertAuteur() 0 35 16
A clean() 0 20 2

How to fix   Complexity   

Complex Class

Complex classes like ExternConverterTrait 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 ExternConverterTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019/2020 © Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
11
namespace App\Domain\Publisher;
12
13
use App\Domain\Enums\Language;
14
use DateTime;
15
use Exception;
16
17
trait ExternConverterTrait
18
{
19 5
    protected function isAnArticle(?string $str): bool
20
    {
21 5
        if (in_array($str, ['article', 'journalArticle'])) {
22 5
            return true;
23
        }
24
25
        return false;
26
    }
27
28 5
    protected function convertURLaccess($data): ?string
29
    {
30
        // NYT, Figaro
31 5
        if (isset($data['isAccessibleForFree'])) {
32 2
            return $data['isAccessibleForFree'] ? 'ouvert' : 'limité';
33
        }
34 5
        if (isset($data['DC.rights'])) {
35 1
            return (in_array($data['DC.rights'], ['free', 'public domain'])) ? 'ouvert' : 'limité';
36
        }
37 4
        if (isset($data['og:article:content_tier'])) {
38 1
            return ($data['og:article:content_tier'] === 'free') ? 'ouvert' : 'limité';
39
        }
40
41 4
        return null;
42
    }
43
44
    /**
45
     * Réduit le nombre d'auteurs si > 3.
46
     * En $modeEtAll=true vérification pour "et al.=oui".
47
     * TODO : wikifyPressAgency()
48
     *
49
     * @param string|null $authors
50
     * @param bool        $modeEtAl
51
     *
52
     * @return string|null
53
     */
54 5
    protected function authorsEtAl(?string $authors, $modeEtAl = false): ?string
55
    {
56 5
        if (empty($authors)) {
57 3
            return null;
58
        }
59
        // conserve juste les 3 premiers auteurs TODO : refactor
60
        // Bob, Martin ; Yul, Bar ; ... ; ...
61 2
        if (preg_match('#([^;]+;[^;]+);[^;]+;.+#', $authors, $matches)) {
62 1
            return ($modeEtAl) ? 'oui' : $matches[1];
63
        }
64
        // Bob Martin, Yul Bar, ..., ...,...
65 1
        if (preg_match('#([^,]+,[^,]+),[^,]+,.+#', $authors, $matches)) {
66 1
            return ($modeEtAl) ? 'oui' : $matches[1];
67
        }
68
69
        return ($modeEtAl) ? null : $authors;
70
    }
71
72 5
    protected function convertDCpage(array $meta): ?string
73
    {
74 5
        if (isset($meta['citation_firstpage'])) {
75 2
            $page = $meta['citation_firstpage'];
76 2
            if (isset($meta['citation_lastpage'])) {
77 1
                $page .= '–'.$meta['citation_lastpage'];
78
            }
79
80 2
            return (string)$page;
81
        }
82
83 3
        return null;
84
    }
85
86
    // TODO encodage + normalizer
87 5
    public function clean(?string $str = null): ?string
88
    {
89 5
        if ($str === null) {
90 5
            return null;
91
        }
92 5
        $str = str_replace(
93 5
            ['&#39;', '&#039;', '&apos;', "\n", "&#10;", "|", "&eacute;"],
94
            [
95 5
                "'",
96
                "'",
97
                "'",
98
                '',
99
                ' ',
100
                '/',
101
                "é",
102
            ],
103 5
            $str
104
        );
105
106 5
        return html_entity_decode($str);
107
    }
108
109 5
    protected function convertOGtype2format(?string $ogType): ?string
110
    {
111 5
        if (empty($ogType)) {
112
            return null;
113
        }
114
        // og:type = default: website / video.movie / video.tv_show video.other / article, book, profile
115 5
        if (strpos($ogType, 'video') !== false) {
116
            return 'vidéo';
117
        }
118 5
        if (strpos($ogType, 'book') !== false) {
119
            return 'livre';
120
        }
121
122 5
        return null;
123
    }
124
125
    /**
126
     * https://developers.facebook.com/docs/internationalization#locales
127
     * @param string|null $lang
128
     *
129
     * @return string|null
130
     */
131 5
    protected function convertLangue(?string $lang = null): ?string
132
    {
133 5
        if (empty($lang)) {
134 2
            return null;
135
        }
136
        // en_GB
137 3
        if (preg_match('#^([a-z]{2})_[A-Z]{2}$#', $lang, $matches)) {
138 2
            return $matches[1];
139
        }
140
141 1
        return Language::all2wiki($lang);
142
    }
143
144 3
    protected function convertAuteur($data, $indice)
145
    {
146
        // author=Bob
147 3
        if (isset($data['author']) && is_string($data['author']) && $indice === 1) {
148
            return html_entity_decode($data['author']);
149
        }
150
151
        // author ['name'=>'Bob','@type'=>'Person']
152 3
        if (0 === $indice
153 3
            && isset($data['author'])
154 3
            && isset($data['author']['name'])
155
            && (!isset($data['author']['@type'])
156 3
                || 'Person' === $data['author']['@type'])
157
        ) {
158
            if (is_string($data['author']['name'])) {
159
                return html_entity_decode($data['author']['name']);
160
            }
161
162
            return html_entity_decode($data['author']['name'][0]);
163
        }
164
165
        // author [ 0 => ['name'=>'Bob'], 1=> ...]
166 3
        if (isset($data['author']) && isset($data['author'][$indice])
167 2
            && (!isset($data['author'][$indice]['@type'])
168 3
                || 'Person' === $data['author'][$indice]['@type'])
169
        ) {
170 2
            if (isset($data['author'][$indice]['name']) && is_string($data['author'][$indice]['name'])) {
171 2
                return html_entity_decode($data['author'][$indice]['name']);
172
            }
173
174
            // "author" => [ "@type" => "Person", "name" => [] ]
175
            return html_entity_decode($data['author'][$indice]['name'][0]);
176
        }
177
178 3
        return null;
179
    }
180
181 3
    protected function convertInstitutionnel($data)
182
    {
183 3
        if (isset($data['author']) && isset($data['author'][0]) && isset($data['author'][0]['@type'])
184 3
            && 'Person' !== $data['author'][0]['@type']
185
        ) {
186
            return html_entity_decode($data['author'][0]['name']);
187
        }
188
189 3
        return null;
190
    }
191
192
    /**
193
     * @param string $str
194
     *
195
     * @return string
196
     * @throws Exception
197
     */
198 5
    protected function convertDate(?string $str): ?string
199
    {
200 5
        if (empty($str)) {
201
            return null;
202
        }
203
204
        // "2012"
205 5
        if (preg_match('#^[12][0-9]{3}$#', $str)) {
206
            return $str;
207
        }
208
209
        try {
210 5
            $date = new DateTime($str);
211
        } catch (Exception $e) {
212
            dump('EXCEPTION DATE');
213
214
            return $str;
215
        }
216
217 5
        return $date->format('d-m-Y');
218
    }
219
220
    /**
221
     * Wikification des noms/acronymes d'agences de presse.
222
     *
223
     * @param string $str
224
     *
225
     * @return string
226
     */
227 5
    protected function wikifyPressAgency(?string $str): ?string
228
    {
229 5
        if (empty($str)) {
230 3
            return null;
231
        }
232
        // skip potential wikilinks
233 4
        if (strpos($str, '[') !== false) {
234
            return $str;
235
        }
236 4
        $str = preg_replace('#\b(AFP)\b#i', '[[Agence France-Presse|AFP]]', $str);
237 4
        $str = str_replace('Reuters', '[[Reuters]]', $str);
238 4
        $str = str_replace('Associated Press', '[[Associated Press]]', $str);
239 4
        $str = preg_replace('#\b(PA)\b#', '[[Press Association|PA]]', $str);
240 4
        $str = preg_replace('#\b(AP)\b#', '[[Associated Press|AP]]', $str);
241 4
        $str = str_replace('Xinhua', '[[Xinhua]]', $str);
242 4
        $str = preg_replace('#\b(ATS)\b#', '[[Agence télégraphique suisse|ATS]]', $str);
243 4
        $str = preg_replace('#\b(PC|CP)\b#', '[[La Presse canadienne|PC]]', $str);
244
245 4
        return $str;
246
    }
247
248
}
249