Passed
Push — master ( 1faa52...d67625 )
by Dispositif
05:49 queued 11s
created

OptimizeISBNTrait   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
dl 0
loc 100
rs 10
c 1
b 0
f 0
wmc 20

1 Method

Rating   Name   Duplication   Size   Complexity  
D processIsbn() 0 92 20
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019/2020 © 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\Domain;
12
13
use Exception;
14
use Throwable;
15
16
/**
17
 * TODO refac : too many conditions
18
 * Trait OptimizeISBNTrait
19
 */
20
trait OptimizeISBNTrait
21
{
22
    /**
23
     * Refac complexity (lines, 20 conditions)
24
     * Validate or correct ISBN.
25
     *
26
     * @throws Exception
27
     */
28
    protected function processIsbn()
29
    {
30
        $isbn = $this->getParam('isbn') ?? '';
0 ignored issues
show
Bug introduced by
It seems like getParam() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

30
        $isbn = $this->/** @scrutinizer ignore-call */ getParam('isbn') ?? '';
Loading history...
31
        if (empty($isbn)) {
32
            return;
33
        }
34
35
        // ISBN-13 à partir de 2007
36
        $year = $this->findBookYear();
0 ignored issues
show
Bug introduced by
It seems like findBookYear() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
        /** @scrutinizer ignore-call */ 
37
        $year = $this->findBookYear();
Loading history...
37
        if ($year !== null && $year < 2007 && 10 === strlen($this->stripIsbn($isbn))) {
0 ignored issues
show
Bug introduced by
It seems like stripIsbn() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

37
        if ($year !== null && $year < 2007 && 10 === strlen($this->/** @scrutinizer ignore-call */ stripIsbn($isbn))) {
Loading history...
38
            // juste mise en forme ISBN-10 pour 'isbn'
39
            try {
40
                $isbnMachine = new IsbnFacade($isbn);
41
                $isbnMachine->validate();
42
                $isbn10pretty = $isbnMachine->format('ISBN-10');
43
                if ($isbn10pretty !== $isbn) {
44
                    $this->setParam('isbn', $isbn10pretty);
0 ignored issues
show
Bug introduced by
It seems like setParam() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

44
                    $this->/** @scrutinizer ignore-call */ 
45
                           setParam('isbn', $isbn10pretty);
Loading history...
45
                    $this->addSummaryLog('ISBN10');
0 ignored issues
show
Bug introduced by
It seems like addSummaryLog() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

45
                    $this->/** @scrutinizer ignore-call */ 
46
                           addSummaryLog('ISBN10');
Loading history...
46
                    //                    $this->notCosmetic = true;
47
                }
48
            } catch (Throwable $e) {
49
                // ISBN not validated
50
                $this->setParam(
51
                    'isbn invalide',
52
                    sprintf('%s %s', $isbn, $e->getMessage() ?? '')
53
                );
54
                $this->addSummaryLog(sprintf('ISBN invalide: %s', $e->getMessage()));
55
                $this->notCosmetic = true;
0 ignored issues
show
Bug Best Practice introduced by
The property notCosmetic does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56
            }
57
58
            return;
59
        }
60
61
        try {
62
            $isbnMachine = new IsbnFacade($isbn);
63
            $isbnMachine->validate();
64
            $isbn13 = $isbnMachine->format('ISBN-13');
65
        } catch (Throwable $e) {
66
            // ISBN not validated
67
            // TODO : bot ISBN invalide (queue, message PD...)
68
            $this->setParam(
69
                'isbn invalide',
70
                sprintf('%s %s', $isbn, $e->getMessage() ?? '')
71
            );
72
            $this->addSummaryLog(sprintf('ISBN invalide: %s', $e->getMessage()));
73
            $this->notCosmetic = true;
74
75
            // TODO log file ISBNinvalide
76
            return;
77
        }
78
79
        // Si $isbn13 et 'isbn2' correspond à ISBN-13 => suppression
80
        if (isset($isbn13)
81
            && $this->hasParamValue('isbn2')
0 ignored issues
show
Bug introduced by
It seems like hasParamValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

81
            && $this->/** @scrutinizer ignore-call */ hasParamValue('isbn2')
Loading history...
82
            && $this->stripIsbn($this->getParam('isbn2')) === $this->stripIsbn($isbn13)
83
        ) {
84
            $this->unsetParam('isbn2');
0 ignored issues
show
Bug introduced by
It seems like unsetParam() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

84
            $this->/** @scrutinizer ignore-call */ 
85
                   unsetParam('isbn2');
Loading history...
85
        }
86
87
        // ISBN-10 ?
88
        $stripIsbn = $this->stripIsbn($isbn);
89
        if (10 === mb_strlen($stripIsbn)) {
90
            // ajout des tirets
91
            $isbn10pretty = $isbn;
92
93
            try {
94
                $isbn10pretty = $isbnMachine->format('ISBN-10');
95
            } catch (Throwable $e) {
96
                unset($e);
97
            }
98
99
            // archivage ISBN-10 dans 'isbn2'
100
            if (!$this->getParam('isbn2')) {
101
                $this->setParam('isbn2', $isbn10pretty);
102
            }
103
            // sinon dans 'isbn3'
104
            if ($this->hasParamValue('isbn2')
105
                && $this->stripIsbn($this->getParam('isbn2')) !== $stripIsbn
106
                && empty($this->getParam('isbn3'))
107
            ) {
108
                $this->setParam('isbn3', $isbn10pretty);
109
            }
110
            // delete 'isbn10' (en attendant modification modèle)
111
            if ($this->hasParamValue('isbn10') && $this->stripIsbn($this->getParam('isbn10')) === $stripIsbn) {
112
                $this->unsetParam('isbn10');
113
            }
114
        }
115
116
        // ISBN correction
117
        if ($isbn13 !== $isbn) {
118
            $this->setParam('isbn', $isbn13);
119
            $this->addSummaryLog('ISBN');
120
            //            $this->notCosmetic = true;
121
        }
122
    }
123
}
124