Passed
Push — master ( dff8a4...2556d0 )
by Dispositif
08:19
created

EditSummaryTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 64
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addSummaryTag() 0 4 2
C generateSummary() 0 42 10
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;
11
12
use DateTime;
13
14
trait EditSummaryTrait
15
{
16
    // Beware !! $importantSummary also defined in Application/OuvrageEditWorker.php
17
    public $importantSummary = [];
18
19
    /**
20
     * For substantive or ambiguous modifications done.
21
     *
22
     * @param string $tag
23
     */
24
    private function addSummaryTag(string $tag)
25
    {
26
        if (!in_array($tag, $this->importantSummary)) {
27
            $this->importantSummary[] = $tag;
28
        }
29
    }
30
31
    /**
32
     * todo extract
33
     * Generate wiki edition summary.
34
     * @return string
35
     */
36
    public function generateSummary(): string
37
    {
38
        // Start summary with "WikiBotConfig" when using botFlag, else "*"
39
        $prefix = ($this->botFlag) ? 'bot ' : ''; //🧐 🤖
40
        // add "/!\" when errorWarning
41
        $prefix .= (empty($this->errorWarning)) ? '' : ' ⚠️';
42
        $prefix .= (empty($this->featured_article)) ? '' : ' ☆'; // AdQ, BA
43
44
        // basic modifs
45
        $citeSummary = implode(' ', $this->citationSummary);
46
        // replace by list of modifs to verify by humans
47
        if (!empty($this->importantSummary)) {
48
            $citeSummary = implode(', ', $this->importantSummary);
49
        }
50
51
        $summary = sprintf(
52
            '%s [%s] %s %sx : %s',
53
            trim($prefix),
54
            str_replace('v', '', $this->citationVersion),
55
            trim(self::TASK_NAME),
56
            $this->nbRows,
57
            $citeSummary
58
        );
59
60
        if (!empty($this->importantSummary)) {
61
            $summary .= '...';
62
        }
63
64
        // shrink long summary if no important details to verify
65
        if (empty($this->importantSummary)) {
66
            $length = strlen($summary);
67
            $summary = mb_substr($summary, 0, 80);
68
            $summary .= ($length > strlen($summary)) ? '…' : '';
69
        }
70
71
        // Luck message
72
        if (!$this->luckyState && (new DateTime())->format('H:i') === '11:11') {
73
            $this->luckyState = true;
1 ignored issue
show
Bug Best Practice introduced by
The property luckyState does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
74
            $summary .= self::LUCKY_MESSAGE;
1 ignored issue
show
Bug introduced by
The constant App\Application\EditSummaryTrait::LUCKY_MESSAGE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
75
        }
76
77
        return $summary;
78
    }
79
80
}
81