Passed
Branch dev (1a31b5)
by Dispositif
03:03
created

RefBotWorker::hasWarning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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
11
namespace App\Application;
12
13
14
use App\Domain\Utils\WikiTextUtil;
15
use Exception;
16
17
abstract class RefBotWorker extends AbstractBotTaskWorker
18
{
19
    const TASK_BOT_FLAG = false;
20
21
    protected $warning = false;
22
    protected $botFlagOnPage;
23
24
    /**
25
     * @inheritDoc
26
     * @throws Exception
27
     */
28
    protected function processDomain(string $title, ?string $text): ?string
29
    {
30
        $this->taskName = static::TASK_NAME;
31
        $this->botFlagOnPage = static::TASK_BOT_FLAG;
32
33
        return $this->processText($text);
34
    }
35
36
    /**
37
     * @param $text
38
     *
39
     * @return string|string[]
40
     * @throws Exception
41
     */
42
    public function processText($text)
43
    {
44
        $refs = WikiTextUtil::extractAllRefs($text);
45
        if (empty($refs)) {
46
            return $text;
47
        }
48
49
        foreach ($refs as $ref) {
50
            $refContent = WikiTextUtil::stripFinalPoint(trim($ref[1]));
51
52
            $newRefContent = $this->processRefContent($refContent);
1 ignored issue
show
Bug introduced by
The method processRefContent() does not exist on App\Application\RefBotWorker. Since it exists in all sub-types, consider adding an abstract or default implementation to App\Application\RefBotWorker. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
            /** @scrutinizer ignore-call */ 
53
            $newRefContent = $this->processRefContent($refContent);
Loading history...
53
54
            $text = $this->replaceRefInText($ref, $newRefContent, $text);
55
        }
56
57
        return $text;
58
    }
59
60
    protected function replaceRefInText(array $ref, string $replace, string $text)
61
    {
62
        if (WikiTextUtil::stripFinalPoint(trim($replace)) === WikiTextUtil::stripFinalPoint(trim($ref[1]))) {
63
            //            echo Color::BG_LIGHT_GRAY."xx".Color::NORMAL." ".$ref[1]."\n";
64
            return $text;
65
        }
66
67
        $replace .= '.'; // ending point
68
        $result = str_replace($ref[1], $replace, $ref[0]);
69
        $this->log->debug(Color::BG_LIGHT_RED."--".Color::NORMAL." ".$ref[0]."\n");
70
        $this->log->debug(Color::BG_LIGHT_GREEN."++".Color::NORMAL." $result \n\n");
71
72
        return str_replace($ref[0], $result, $text);
73
    }
74
75
    public function hasWarning(): bool
76
    {
77
        return (bool)$this->warning;
78
    }
79
}
80