Completed
Push — develop ( a0c664...f8e6eb )
by Tom
04:27
created

InstallSampleData   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 7
c 3
b 1
f 0
lcom 1
cbo 8
dl 8
loc 72
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B runSampleDataInstaller() 0 41 3
B execute() 8 24 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace N98\Magento\Command\Installer\SubCommand;
4
5
use N98\Magento\Command\SubCommand\AbstractSubCommand;
6
use N98\Util\OperatingSystem;
7
use Symfony\Component\Process\ProcessBuilder;
8
9
class InstallSampleData extends AbstractSubCommand
10
{
11
    /**
12
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
13
     */
14
    public function execute()
15
    {
16
        if ($this->input->getOption('noDownload')) {
17
            return false;
18
        }
19
20
        $installationFolder = $this->config->getString('installationFolder');
21
        chdir($installationFolder);
22
23
        $dialog = $this->getCommand()->getHelper('dialog');
24
25 View Code Duplication
        if ($this->input->getOption('installSampleData') !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
            $installSampleData = $this->getCommand()->parseBoolOption($this->input->getOption('installSampleData'));
27
        } else {
28
            $installSampleData = $dialog->askConfirmation(
29
                $this->output,
30
                '<question>Install sample data?</question> <comment>[y]</comment>: '
31
            );
32
        }
33
34
        if ($installSampleData) {
35
            $this->runSampleDataInstaller();
36
        }
37
    }
38
39
    protected function runSampleDataInstaller()
40
    {
41
        $installationArgs = $this->config->getArray('installation_args');
0 ignored issues
show
Unused Code introduced by
$installationArgs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
42
43
        $processBuilder = new ProcessBuilder([
44
            'php',
45
            'bin/magento',
46
            'sampledata:deploy',
47
        ]);
48
49
        if (!OperatingSystem::isWindows()) {
50
            $processBuilder->setPrefix('/usr/bin/env');
51
        }
52
53
        $process = $processBuilder->getProcess();
54
        $process->setTimeout(86400);
55
        $process->start();
56
        $process->wait(function ($type, $buffer) {
57
            $this->output->write($buffer, false);
58
        });
59
60
61
        // @TODO Refactor code duplication
62
        if (!OperatingSystem::isWindows()) {
63
            $processBuilder->setPrefix('/usr/bin/env');
64
        }
65
66
        $processBuilder = new ProcessBuilder(
67
            array(
68
                'php',
69
                'bin/magento',
70
                'setup:upgrade'
71
            )
72
        );
73
        $process = $processBuilder->getProcess();
74
        $process->setTimeout(86400);
75
        $process->start();
76
        $process->wait(function ($type, $buffer) {
77
            $this->output->write($buffer, false);
78
        });
79
    }
80
}
81