Passed
Push — master ( fd6b1a...a766ea )
by Dispositif
02:28
created

OuvrageEditSummaryTrait::addSummaryDataOnPageWorkStatus()   F

Complexity

Conditions 16
Paths 8192

Size

Total Lines 66
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
c 0
b 0
f 0
dl 0
loc 66
rs 1.4
cc 16
nc 8192
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\OuvrageEdit;
11
12
use DateTime;
13
14
/**
15
 * Used only by OuvrageEditWorker.
16
 * TODO make it used by PageWorkStatus instead of OuvrageEditWorker ? Or ImportantSummaryCreator ?
17
 */
18
trait OuvrageEditSummaryTrait
19
{
20
    /**
21
     * Generate wiki edition summary.
22
     */
23
    protected function generateFinalSummary(): string
24
    {
25
        $prefix = $this->generatePrefix();
26
        $citeSummary = $this->getCiteSummary();
27
28
        $summary = sprintf(
29
            '%s [%s] %s %sx : %s',
30
            trim($prefix),
31
            str_replace('v', '', $this->pageWorkStatus->citationVersion),
32
            trim(self::TASK_NAME),
33
            $this->pageWorkStatus->nbRows,
34
            $citeSummary
35
        );
36
37
        $summary = $this->shrinkLongSummaryIfNoImportantDetailsToVerify($summary);
38
        $summary = $this->couldAddLuckMessage($summary);
39
        $this->log->notice($summary);
40
41
        return $summary;
42
    }
43
44
    /**
45
     * Shrink long summary if no important details to verify.
46
     */
47
    protected function shrinkLongSummaryIfNoImportantDetailsToVerify(string $summary): string
48
    {
49
        if (empty($this->pageWorkStatus->importantSummary)) {
50
            $length = strlen($summary);
51
            $summary = mb_substr($summary, 0, 80);
52
            $summary .= ($length > strlen($summary)) ? '…' : '';
53
        } else {
54
            $summary .= '…'; // ?
55
        }
56
        return $summary;
57
    }
58
59
    protected function couldAddLuckMessage(string $summary): string
60
    {
61
        if (!$this->pageWorkStatus->luckyState && (new DateTime())->format('H:i') === '11:11') {
62
            $this->pageWorkStatus->luckyState = true;
63
            $summary .= self::LUCKY_MESSAGE;
64
        }
65
66
        return $summary;
67
    }
68
69
    protected function generatePrefix(): string
70
    {
71
        $prefix = ($this->pageWorkStatus->botFlag) ? 'bot ' : '';
72
        $prefix .= (empty($this->pageWorkStatus->errorWarning)) ? '' : ' ⚠️';
73
        $prefix .= (empty($this->pageWorkStatus->featured_article)) ? '' : ' ☆'; // AdQ, BA
74
75
        return $prefix;
76
    }
77
78
    /**
79
     * Generate list of details about current bot edition.
80
     */
81
    protected function getCiteSummary(): string
82
    {
83
        // basic modifs
84
        $citeSummary = implode(' ', $this->pageWorkStatus->citationSummary);
85
        // replaced by list of modifs to verify by humans
86
        if (!empty($this->pageWorkStatus->importantSummary)) {
87
            $citeSummary = implode(', ', $this->pageWorkStatus->importantSummary);
88
        }
89
        return $citeSummary;
90
    }
91
}
92