Completed
Push — symfony-console ( 5f68e3...c5adef )
by Arnaud
04:56
created

Command::getBuilder()   B

Complexity

Conditions 6
Paths 29

Size

Total Lines 31

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
dl 31
loc 31
rs 8.8017
c 0
b 0
f 0
cc 6
nc 29
nop 3
1
<?php
2
3
namespace Cecil\Command;
4
5
use Cecil\Builder;
6
use Symfony\Component\Console\Command\Command as BaseCommand;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Yaml\Exception\ParseException;
10
use Symfony\Component\Yaml\Yaml;
11
12
class Command extends BaseCommand
13
{
14
    const CONFIG_FILE = 'config.yml';
15
16
    /**
17
     * @var string
18
     */
19
    protected $path;
20
    /**
21
     * @var Builder
22
     */
23
    protected $builder;
24
25
    /**
26
     * {@inheritDoc}
27
     */
28
    protected function initialize(InputInterface $input, OutputInterface $output)
29
    {
30
        $this->path = $input->getArgument('path');
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getArgument('path') can also be of type array<integer,string>. However, the property $path is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
31
        $this->path = realpath($this->path);
32
        $this->path = str_replace(DIRECTORY_SEPARATOR, '/', $this->path);
33
34
        parent::initialize($input, $output);
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getPath()
41
    {
42
        return $this->path;
43
    }
44
45
    /**
46
     * @param OutputInterface $output
47
     * @param array $config
48
     * @param array $options
49
     *
50
     * @return Builder
51
     */
52 View Code Duplication
    public function getBuilder(
53
        OutputInterface $output,
54
        array $config = ['debug' => false],
55
        array $options = ['verbosity' => Builder::VERBOSITY_NORMAL]
56
    ) {
57
        if (!file_exists($this->getPath() . '/' . self::CONFIG_FILE)) {
58
            throw new \Exception(sprintf('Config file not found in "%s"!', $this->getPath()));
59
        }
60
        // verbosity: verbose
61
        if ($options['verbosity'] == Builder::VERBOSITY_VERBOSE) {
62
            $this->verbose = true;
0 ignored issues
show
Bug introduced by
The property verbose does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
63
        }
64
        // verbosity: quiet
65
        if ($options['verbosity'] == Builder::VERBOSITY_QUIET) {
66
            $this->quiet = true;
0 ignored issues
show
Bug introduced by
The property quiet does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
        }
68
69
        try {
70
            $configFile = Yaml::parse(file_get_contents($this->getPath() . '/' . self::CONFIG_FILE));
71
            $config = array_replace_recursive($configFile, $config);
72
            $this->builder = (new Builder($config, $this->messageCallback($output)))
73
                ->setSourceDir($this->getPath())
74
                ->setDestinationDir($this->getPath());
75
        } catch (ParseException $e) {
76
            throw new \Exception(sprintf('Config file parse error: %s', $e->getMessage()));
77
        } catch (\Exception $e) {
78
            throw new \Exception(sprintf($e->getMessage()));
79
        }
80
81
        return $this->builder;
82
    }
83
84
    /**
85
     * Custom message callback function.
86
     *
87
     * @param OutputInterface $output
88
     */
89
    public function messageCallback(OutputInterface $output)
90
    {
91
        return function ($code, $message = '', $itemsCount = 0, $itemsMax = 0) use ($output) {
92
            if ($this->quiet) {
93
                return;
94
            } else {
95
                if (strpos($code, '_PROGRESS') !== false) {
96
                    if ($this->verbose) {
97
                        if ($itemsCount > 0) {
98
                            //$this->wlDone(sprintf('(%u/%u) %s', $itemsCount, $itemsMax, $message));
99
                            $output->writeln(sprintf('(%u/%u) %s', $itemsCount, $itemsMax, $message));
100
101
                            return;
102
                        }
103
                        //$this->wlDone("$message");
104
                        $output->writeln("$message");
105 View Code Duplication
                    } else {
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...
106
                        if (isset($itemsCount) && $itemsMax > 0) {
107
                            //$this->printProgressBar($itemsCount, $itemsMax, $message);
108
                            $output->writeln("$message");
109
                        } else {
110
                            //$this->wl($message);
111
                            $output->writeln("$message");
112
                        }
113
                    }
114
                } elseif (strpos($code, '_ERROR') !== false) {
115
                    //$this->wlError($message);
116
                    $output->writeln("$message");
117
                } elseif ($code == 'TIME') {
118
                    //$this->wl($message);
119
                    $output->writeln("$message");
120
                } else {
121
                    //$this->wlAnnonce($message);
122
                    $output->writeln("$message");
123
                }
124
            }
125
        };
126
    }
127
}
128