Passed
Branch master (309757)
by Dispositif
03:20 queued 54s
created

OuvrageEditErrorReport   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 22
dl 0
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getReport() 0 13 2
A deleteAllReports() 0 16 1
A countErrorInText() 0 10 3
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Application\OuvrageEdit;
11
12
class OuvrageEditErrorReport
13
{
14
    /**
15
     * Liste les erreurs reportées.
16
     * 0 => "|editor=JT Staley, MP Bryant, N Pfennig, and JG Holt, eds. <!--PARAMETRE 'editor' N'EXISTE PAS -->"
17
     * 1 => "|editor=DR Boone and RW Castenholz, eds. <!--PARAMETRE 'editor' N'EXISTE PAS -->".
18
     *
19
     * @param string $text
20
     *
21
     * @return array|null
22
     */
23
    public function getReport(string $text): ?array
24
    {
25
        if (preg_match_all(
26
                '#\\* <span style="background:\#FCDFE8"><nowiki>([^\n]+)</nowiki></span>\n#',
27
                $text,
28
                $matches
29
            ) > 0
30
        ) {
31
            // FIXED zizibot : des '<nowiki>' supplémentaires ajoutés à quelques rapports
32
            return str_replace('<nowiki>', '', $matches[1]);
33
        }
34
35
        return null;
36
    }
37
38
    public function countErrorInText(array $errors, string $text): int
39
    {
40
        $found = 0;
41
        foreach ($errors as $error) {
42
            if (false !== mb_strpos($text, $error)) {
43
                ++$found;
44
            }
45
        }
46
47
        return $found;
48
    }
49
50
    /**
51
     * Delete the previous bot errorReporting message from text.
52
     *
53
     * @param string      $text
54
     * @param string|null $botName
55
     *
56
     * @return string
57
     */
58
    public function deleteAllReports(string $text, ?string $botName = 'CodexBot'): string
59
    {
60
        $pattern = sprintf(
61
            '#== Ouvrage avec erreur de paramètre ==(.*)Le robot \[\[Utilisateur:%s\|%s\]\] \(\[\[Discussion utilisateur:%s\|[^\]]+\]\]\) [0-9a-zéà: ]+ \([A-Z]+\)[\n]*#s',
62
            $botName,
63
            $botName,
64
            $botName
65
        );
66
        // option s : dot matches new lines
67
        $text = preg_replace(
68
            $pattern,
69
            '',
70
            $text
71
        );
72
73
        return $text;
74
    }
75
}
76