Test Failed
Pull Request — master (#9)
by Yo
02:08
created

InitRepositoryProcessor::process()   C

Complexity

Conditions 11
Paths 18

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 42
ccs 0
cts 36
cp 0
rs 5.2653
c 1
b 0
f 0
cc 11
eloc 25
nc 18
nop 0
crap 132

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
namespace Yoanm\DefaultPhpRepository\Processor;
3
4
use Symfony\Component\Console\Question\ConfirmationQuestion;
5
use Yoanm\DefaultPhpRepository\Helper\CommandHelper;
6
use Yoanm\DefaultPhpRepository\Model\FolderTemplate;
7
use Yoanm\DefaultPhpRepository\Model\Template;
8
9
/**
10
 * Class InitRepositoryProcessor
11
 */
12
class InitRepositoryProcessor
13
{
14
    /** @var CommandHelper */
15
    private $helper;
16
    /** @var bool */
17
    private $skipExisting;
18
    /** @var bool */
19
    private $forceOverride;
20
    /** @var array */
21
    private $idList = [];
22
23
    /**
24
     * @param CommandHelper $helper
25
     * @param bool          $skipExisting
26
     * @param bool          $forceOverride
27
     * @param array         $idList
28
     */
29
    public function __construct(
30
        CommandHelper $helper,
31
        $skipExisting = true,
32
        $forceOverride = false,
33
        $idList = []
34
    ) {
35
        $this->helper = $helper;
36
37
        $this->skipExisting = $skipExisting;
38
        $this->forceOverride = $forceOverride;
39
        $this->idList = $idList;
40
    }
41
42
    public function process()
43
    {
44
        $currentType = null;
45
        foreach ($this->helper->getTemplateList() as $template) {
46
            if (count($this->idList) && !in_array($template->getId(), $this->idList)) {
47
                continue;
48
            }
49
            $this->helper->displayHeader($template, $currentType);
50
51
            $currentType = $this->helper->resolveCurrentType($template);
52
53
            $this->displayTemplate($template);
54
55
            if ($template instanceof FolderTemplate) {
56
57
                $targetExist = false;
58
                $process = false;
59
60
                foreach ($template->getFileList() as $subTemplate) {
61
                    $targetExist = $this->helper->targetExist($subTemplate);
62
                    $process = $this->processOrNot($targetExist);
63
                    if (false === $process) {
64
                        break;
65
                    }
66
                }
67
68
                if (true === $process) {
69
                    foreach ($template->getFileList() as $subTemplate) {
70
                        $this->helper->dump($subTemplate);
71
                    }
72
                }
73
            } else {
74
                $targetExist = $this->helper->targetExist($template);
75
                if (true === $this->processOrNot($targetExist)) {
76
                    $this->helper->dump($template);
77
                }
78
            }
79
            if (false === $targetExist) {
80
                $this->helper->display('<info>Done</info>');
81
            }
82
        }
83
    }
84
85
    /**
86
     * @param Template $template
87
     */
88
    protected function displayTemplate(Template $template)
89
    {
90
        $this->helper->display(sprintf('<comment>%s</comment>', $template->getId()), 2, false);
91
        $this->helper->display(sprintf(' - <info>./%s</info> : ', $template->getTarget()), 0, false);
92
    }
93
94
    /**
95
     * @param Template $template
0 ignored issues
show
Bug introduced by
There is no parameter named $template. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
96
     * @param bool     $targetExist
97
     *
98
     * @return bool
99
     */
100
    protected function processOrNot($targetExist)
101
    {
102
        $process = true;
103
        if ($targetExist) {
104
            if (false === $this->forceOverride && true === $this->skipExisting) {
105
                $this->helper->display('<comment>Skipped !</comment>');
106
                $process = false;
107
            } else {
108
                $process = false;
109
                if (true === $this->forceOverride) {
110
                    $process = true;
111
                    $this->helper->display('<comment>Overriden !</comment>');
112
                } elseif (
113
                    $this->helper->ask(
114
                        new ConfirmationQuestion('<question>Overwrite ? [n]</question>', false)
115
                    )
116
                ) {
117
                    $process = true;
118
                }
119
            }
120
        }
121
122
        return $process;
123
    }
124
}
125