Completed
Push — master ( d5b8a7...cdd9b5 )
by Dispositif
02:37
created

ErrorReport::getReport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
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
    const BOTNAME = 'ZiziBot';
15
16
    /**
17
     * Liste les erreurs reportées.
18
     * 0 => "|editor=JT Staley, MP Bryant, N Pfennig, and JG Holt, eds. <!--PARAMETRE 'editor' N'EXISTE PAS -->"
19
     * 1 => "|editor=DR Boone and RW Castenholz, eds. <!--PARAMETRE 'editor' N'EXISTE PAS -->"
20
     *
21
     * @param string $text
22
     *
23
     * @return array|null
24
     */
25
    public function getReport(string $text): ?array
26
    {
27
        if (preg_match_all(
28
                '#\\* <span style="background:\#FCDFE8"><nowiki>([^\n]+)</nowiki></span>\n#',
29
                $text,
30
                $matches
31
            ) > 0
32
        ) {
33
            return $matches[1];
34
        }
35
36
        return null;
37
    }
38
39
    public function countErrorInText(array $errors, string $text): int
40
    {
41
        $found = 0;
42
        foreach ($errors as $error) {
43
            if (mb_strpos($text, $error) !== false) {
44
                $found++;
45
            }
46
        }
47
48
        return $found;
49
    }
50
51
    /**
52
     * Delete the previous bot errorReporting message from text.
53
     *
54
     * @param string $text
55
     *
56
     * @return string
57
     */
58
    public function deleteAllReports(string $text): string
59
    {
60
        $pattern = sprintf(
61
            '#== Ouvrage avec erreur de paramètre ==(.*)Le robot \[\[Utilisateur:%s\|%s\]\] \(\[\[Discussion utilisateur:%s\|discuter\]\]\) [0-9a-zéà: ]+ \(CET\)[\n]*#s',
62
            self::BOTNAME,
63
            self::BOTNAME,
64
            self::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
}
77