Passed
Push — master ( 9e3ea7...ee24dc )
by Dispositif
02:33
created

PredictErrorParameterHandler::handle()   B

Complexity

Conditions 10
Paths 12

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 35
rs 7.6666
cc 10
nc 12
nop 0

How to fix   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\Domain\WikiOptimizer\Handlers;
11
12
use App\Domain\Models\Wiki\WikiTemplateInterface;
13
use App\Domain\Utils\TextUtil;
14
use App\Domain\WikiOptimizer\OptiStatus;
15
16
class PredictErrorParameterHandler implements OptimizeHandlerInterface
17
{
18
    /**
19
     * @var WikiTemplateInterface
20
     */
21
    protected $template;
22
    /**
23
     * @var OptiStatus
24
     */
25
    protected $optiStatus;
26
27
    public function __construct(WikiTemplateInterface $template, OptiStatus $summary)
28
    {
29
        $this->template = $template;
30
        $this->optiStatus = $summary;
31
    }
32
33
    // Correction des parametres rejetés à l'hydratation données.
34
    public function handle()
35
    {
36
        if (empty($this->template->parametersErrorFromHydrate)) {
0 ignored issues
show
Bug introduced by
Accessing parametersErrorFromHydrate on the interface App\Domain\Models\Wiki\WikiTemplateInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
37
            return;
38
        }
39
        $allParamsAndAlias = $this->template->getParamsAndAlias();
0 ignored issues
show
Bug introduced by
The method getParamsAndAlias() does not exist on App\Domain\Models\Wiki\WikiTemplateInterface. Did you maybe mean getParam()? ( Ignorable by Annotation )

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

39
        /** @scrutinizer ignore-call */ 
40
        $allParamsAndAlias = $this->template->getParamsAndAlias();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
41
        foreach ($this->template->parametersErrorFromHydrate as $name => $value) {
42
            if (!is_string($name)) {
43
                // example : 1 => "ouvrage collectif" from |ouvrage collectif|
44
                continue;
45
            }
46
47
            // delete error parameter if no value
48
            if (empty($value)) {
49
                unset($this->template->parametersErrorFromHydrate[$name]);
50
51
                continue;
52
            }
53
54
            $maxDistance = 1;
55
            if (mb_strlen($name) >= 4) {
56
                $maxDistance = 2;
57
            }
58
            if (mb_strlen($name) >= 8) {
59
                $maxDistance = 3;
60
            }
61
62
            $predName = TextUtil::predictCorrectParam($name, $allParamsAndAlias, $maxDistance);
63
            if ($predName && mb_strlen($name) >= 5 && empty($this->template->getParam($predName))) {
64
                $predName = $this->template->getAliasParam($predName);
0 ignored issues
show
Bug introduced by
The method getAliasParam() does not exist on App\Domain\Models\Wiki\WikiTemplateInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to App\Domain\Models\Wiki\WikiTemplateInterface. ( Ignorable by Annotation )

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

64
                /** @scrutinizer ignore-call */ 
65
                $predName = $this->template->getAliasParam($predName);
Loading history...
65
                $this->template->setParam($predName, $value);
66
                $this->optiStatus->addSummaryLog(sprintf('%s⇒%s ?', $name, $predName));
67
                $this->optiStatus->setNotCosmetic(true);
68
                unset($this->template->parametersErrorFromHydrate[$name]);
69
            }
70
        }
71
    }
72
}