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

ErrorReport::deleteAllReports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 2
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 © 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
namespace App\Application;
11
12
class ErrorReport
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 1
    public function getReport(string $text): ?array
24
    {
25 1
        if (preg_match_all(
26 1
                '#\\* <span style="background:\#FCDFE8"><nowiki>([^\n]+)</nowiki></span>\n#',
27 1
                $text,
28 1
                $matches
29 1
            ) > 0
30
        ) {
31
            // FIXED zizibot : des '<nowiki>' supplémentaires ajoutés à quelques rapports
32 1
            return str_replace('<nowiki>', '', $matches[1]);
33
        }
34
35
        return null;
36
    }
37
38 1
    public function countErrorInText(array $errors, string $text): int
39
    {
40 1
        $found = 0;
41 1
        foreach ($errors as $error) {
42 1
            if (false !== mb_strpos($text, $error)) {
43 1
                ++$found;
44
            }
45
        }
46
47 1
        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 1
    public function deleteAllReports(string $text, ?string $botName = 'CodexBot'): string
59
    {
60 1
        $pattern = sprintf(
61 1
            '#== Ouvrage avec erreur de paramètre ==(.*)Le robot \[\[Utilisateur:%s\|%s\]\] \(\[\[Discussion utilisateur:%s\|discuter\]\]\) [0-9a-zéà: ]+ \([A-Z]+\)[\n]*#s',
62 1
            $botName,
63 1
            $botName,
64 1
            $botName
65
        );
66
        // option s : dot matches new lines
67 1
        $text = preg_replace(
68 1
            $pattern,
69 1
            '',
70 1
            $text
71
        );
72
73 1
        return $text;
74
    }
75
}
76