Passed
Branch master (ee24dc)
by Dispositif
03:19 queued 39s
created

EditionCitebookHandler::getEditionOrdinalNumber()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 20
rs 9.9332
cc 4
nc 4
nop 1
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\Domain\WikiOptimizer\Handlers;
11
12
/**
13
 * {Cite book}:"edition" [ordinal number] => {ouvrage}::"numéro d'édition" (ou "réimpression" [année])
14
 * {Cite book}:origyear => {ouvrage}:"année première édition"
15
 * https://wstat.fr/template/index.php?title=Ouvrage&query=paramvalue&param=edition&limit=5000&searchtext=.&searchre=1
16
 * Pas mal de corrupted sur "éditions"
17
 * https://wstat.fr/template/index.php?title=Ouvrage&query=paramvalue&param=%C3%A9dition&limit=5000&searchtext=.&searchre=1
18
 * Note : impossible de faire getParam("éditeur-doublon")
19
 */
20
class EditionCitebookHandler extends AbstractOuvrageHandler
21
{
22
    public function handle()
23
    {
24
        // "édition" alias de "éditeur", mais OuvrageTemplateAlias:"édition"=>"numéro d'édition" à cause des doublons
25
        if (!empty($this->ouvrage->getParam("numéro d'édition"))) {
26
            $numeroEdition = $this->ouvrage->getParam("numéro d'édition");
27
            if (empty($this->ouvrage->getParam('éditeur'))
28
                && $this->getEditionOrdinalNumber($numeroEdition) === null
29
                && !$this->isEditionYear($numeroEdition)
0 ignored issues
show
Bug introduced by
It seems like $numeroEdition can also be of type null; however, parameter $str of App\Domain\WikiOptimizer...andler::isEditionYear() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

29
                && !$this->isEditionYear(/** @scrutinizer ignore-type */ $numeroEdition)
Loading history...
30
            ) {
31
                $this->ouvrage->setParam('éditeur', $numeroEdition);
0 ignored issues
show
Bug introduced by
It seems like $numeroEdition can also be of type null; however, parameter $value of App\Domain\Models\Wiki\A...ikiTemplate::setParam() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

31
                $this->ouvrage->setParam('éditeur', /** @scrutinizer ignore-type */ $numeroEdition);
Loading history...
32
                $this->ouvrage->unsetParam("numéro d'édition");
33
                $this->optiStatus->addSummaryLog('±éditeur');
34
            }
35
        }
36
37
        // Correction nom de paramètre selon type de valeur
38
        $this->correctReimpressionByParam("numéro d'édition");
39
        $this->correctReimpressionByParam("éditeur");
40
        $this->correctReimpressionByParam("édition");
41
    }
42
43
    private function getEditionOrdinalNumber(?string $str): ?string
44
    {
45
        if (!$str) {
46
            return null;
47
        }
48
        // {{5e}}
49
        if (preg_match('#^\{\{(\d+)e\}\}$#', $str, $matches)) {
50
            return $matches[1];
51
        }
52
        // "1st ed."
53
        if (preg_match(
54
            '#^(\d+) ?(st|nd|rd|th|e|ème)? ?(ed|ed\.|edition|reprint|published|publication)?$#i',
55
            $str,
56
            $matches
57
        )
58
        ) {
59
            return $matches[1];
60
        }
61
62
        return null;
63
    }
64
65
    private function isEditionYear(string $str): bool
66
    {
67
        return preg_match('#^\d{4}$#', $str) && (int)$str > 1700 && (int)$str < 2025;
68
    }
69
70
    private function correctReimpressionByParam(string $param): void
71
    {
72
        $editionNumber = $this->ouvrage->getParam($param);
73
        if (!empty($editionNumber) && $this->isEditionYear($editionNumber)) {
74
            $this->ouvrage->unsetParam($param);
75
            $this->ouvrage->setParam('réimpression', $editionNumber);
76
            $this->optiStatus->addSummaryLog('+réimpression');
77
            $this->optiStatus->setNotCosmetic(true);
78
79
            return;
80
        }
81
82
        $editionOrdinal = $this->getEditionOrdinalNumber($editionNumber);
83
        if (!empty($editionNumber) && !$this->isEditionYear($editionNumber) && $editionOrdinal) {
84
            $this->ouvrage->unsetParam($param);
85
            $this->ouvrage->setParam("numéro d'édition", $editionOrdinal);
86
            $this->optiStatus->addSummaryLog("±numéro d'édition");
87
            $this->optiStatus->setNotCosmetic(true);
88
        }
89
    }
90
}