Test Failed
Pull Request — master (#9)
by Yo
03:11 queued 01:13
created

InitCommandProcessor::validateDump()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 20
cp 0
rs 8.7624
c 0
b 0
f 0
cc 6
eloc 15
nc 5
nop 1
crap 42
1
<?php
2
namespace Yoanm\DefaultPhpRepository\Processor;
3
4
use Symfony\Component\Console\Helper\QuestionHelper;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Question\ConfirmationQuestion;
8
use Symfony\Component\Filesystem\Filesystem;
9
use Yoanm\DefaultPhpRepository\Helper\TemplateHelper;
10
use Yoanm\DefaultPhpRepository\Model\FolderTemplate;
11
use Yoanm\DefaultPhpRepository\Model\Template;
12
13
/**
14
 * Class InitCommandProcessor
15
 */
16
class InitCommandProcessor extends CommandProcessor
17
{
18
    /** @var TemplateHelper */
19
    private $templateHelper;
20
    /** @var Filesystem */
21
    private $fileSystem;
22
    /** @var string */
23
    private $rootPath;
24
    /** @var bool */
25
    private $skipExisting;
26
    /** @var bool */
27
    private $forceOverride;
28
    /** @var array */
29
    private $idList = [];
30
31
    /**
32
     * @param QuestionHelper     $questionHelper
33
     * @param InputInterface     $input
34
     * @param OutputInterface    $output
35
     * @param TemplateHelper     $templateHelper
36
     * @param Template[]         $templateList
37
     * @param bool               $skipExisting
38
     * @param bool               $forceOverride
39
     * @param array              $idList
40
     */
41
    public function __construct(
42
        QuestionHelper $questionHelper,
43
        InputInterface $input,
44
        OutputInterface $output,
45
        TemplateHelper $templateHelper,
46
        array $templateList,
47
        $skipExisting = true,
48
        $forceOverride = false,
49
        $idList = [],
50
        $rootPath = '.'
51
    ) {
52
        parent::__construct($questionHelper, $input, $output, $templateList);
53
54
        $this->templateHelper = $templateHelper;
55
        $this->rootPath = realpath($rootPath);
56
        $this->fileSystem = new Filesystem();
57
58
        $this->skipExisting = $skipExisting;
59
        $this->forceOverride = $forceOverride;
60
        $this->idList = $idList;
61
    }
62
63
    public function process()
64
    {
65
        $currentType = null;
66
        foreach ($this->getTemplateList() as $template) {
67
            if (count($this->idList) && !in_array($template->getId(), $this->idList)) {
68
                continue;
69
            }
70
            $this->displayHeader($template, $currentType);
71
72
            $currentType = $this->resolveCurrentType($template);
73
            $fileExist = false;
74
            if ($template instanceof FolderTemplate) {
75
                $this->displayTemplate($template);
76
                foreach ($template->getFileList() as $subTemplate) {
77
                    $targetPath = sprintf('%s/%s', $this->rootPath, $subTemplate->getTarget());
78
                    list($fileExist, $process) = $this->validateDump($targetPath);
79
                    if (false === $process) {
80
                        break;
81
                    }
82
                }
83
                if (true === $process) {
0 ignored issues
show
Bug introduced by
The variable $process does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
84
                    foreach ($template->getFileList() as $subTemplate) {
85
                        $targetPath = sprintf('%s/%s', $this->rootPath, $subTemplate->getTarget());
86
                        $this->dumpTemplate($subTemplate, $targetPath);
87
                    }
88
                }
89
            } else {
90
                $this->displayTemplate($template);
91
                $targetPath = sprintf('%s/%s', $this->rootPath, $template->getTarget());
92
                list($fileExist, $process) = $this->validateDump($targetPath);
93
                if (true === $process) {
94
                    $this->dumpTemplate($template, $targetPath);
95
                }
96
            }
97
            if (false === $fileExist) {
98
                $this->display('<info>Done</info>');
99
            }
100
        }
101
    }
102
103
    /**
104
     * @param Template $template
105
     */
106
    protected function displayTemplate(Template $template)
107
    {
108
        $this->display(sprintf('<comment>%s</comment>', $template->getId()), 2, false);
109
        $targetRelativePath = sprintf('./%s', $template->getTarget());
110
        $this->display(sprintf(' - <info>%s</info> : ', $targetRelativePath), 0, false);
111
    }
112
113
    /**
114
     * @param Template $template
115
     * @param string           $path
116
     */
117
    protected function dumpTemplate(Template $template, $path)
118
    {
119
        $templateId = $template->getSource();
120
        if (null !== $template->getNamespace()) {
121
            $templateId = sprintf('@%s/%s', $template->getNamespace(), $template->getSource());
122
        }
123
        $this->templateHelper->dumpTemplate($templateId, $path);
124
    }
125
126
    /**
127
     * @param string $target
128
     * @return array
129
     */
130
    protected function validateDump($target)
131
    {
132
        $fileExist = $this->fileSystem->exists($target);
133
        $process = true;
134
        if ($fileExist) {
135
            if (false === $this->forceOverride && true === $this->skipExisting) {
136
                $this->display('<comment>Skipped !</comment>');
137
                $process = false;
138
            } else {
139
                $process = false;
140
                if (true === $this->forceOverride) {
141
                    $this->display('<comment>Overriden !</comment>');
142
                    $process = true;
143
                } elseif ($this->doOverwrite()) {
144
                    $process = true;
145
                }
146
            }
147
        }
148
149
        return [$fileExist, $process];
150
    }
151
152
    /**@return bool
153
     */
154
    protected function doOverwrite()
155
    {
156
        return $this->ask(new ConfirmationQuestion('<question>Overwrite ? [n]</question>', false));
157
    }
158
}
159