Passed
Push — master ( a1929c...27d925 )
by Dispositif
02:28
created

RefBotWorker::processText()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 9.9666
ccs 0
cts 11
cp 0
crap 12
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
11
namespace App\Application;
12
13
14
use App\Domain\Utils\WikiTextUtil;
15
use Codedungeon\PHPCliColors\Color;
16
use Exception;
17
18
abstract class RefBotWorker extends AbstractBotTaskWorker
19
{
20
    public const TASK_BOT_FLAG = false;
21
22
    protected $warning = false;
23
24
    public function hasWarning(): bool
25
    {
26
        return (bool)$this->warning;
27
    }
28
29
    /**
30
     * @inheritDoc
31
     * @throws Exception
32
     */
33
    protected function processWithDomainWorker(string $title, string $text): ?string
34
    {
35
        return $this->processText($text);
36
    }
37
38
    /**
39
     * @param $text
40
     *
41
     * @return string|string[]
42
     * @throws Exception
43
     */
44
    public function processText($text)
45
    {
46
        $refs = WikiTextUtil::extractRefsAndListOfLinks($text);
47
        if ($refs === []) {
48
            $this->log->debug('empty extractRefsAndListOfLinks');
49
50
            return $text;
51
        }
52
53
        foreach ($refs as $ref) {
54
            $refContent = WikiTextUtil::stripFinalPoint(trim($ref[1]));
55
56
            $newRefContent = $this->processRefContent($refContent);
57
58
            $text = $this->replaceRefInText($ref, $newRefContent, $text);
59
        }
60
61
        return $text;
62
    }
63
64
    public abstract function processRefContent($refContent): string;
65
66
    protected function replaceRefInText(array $ref, string $replace, string $text)
67
    {
68
        // Pas de changement
69
        if (WikiTextUtil::stripFinalPoint(trim($replace)) === WikiTextUtil::stripFinalPoint(trim($ref[1]))) {
70
            return $text;
71
        }
72
        // Ajout point final si </ref> détecté
73
        if (preg_match('#</ref>#', $ref[0])) {
74
            $replace .= '.'; // ending point
75
        }
76
        $result = str_replace($ref[1], $replace, $ref[0]);
77
        $this->log->debug(Color::BG_LIGHT_RED."--".Color::NORMAL." ".$ref[0]."\n");
78
        $this->log->debug(Color::BG_LIGHT_GREEN."++".Color::NORMAL." $result \n\n");
79
80
        return str_replace($ref[0], $result, $text);
81
    }
82
}
83