Completed
Push — master ( 268d4c...a8995e )
by Michael
01:52
created

FixCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
4
5
namespace SugaredRim\PhpCsFixer\Console\Command;
6
7
use ReflectionMethod;
8
use ReflectionProperty;
9
use Schnittstabil\Get;
10
use SugaredRim\PhpCsFixer\Config;
11
use SugaredRim\PhpCsFixer\ConfigFactory;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use PhpCsFixer\ConfigInterface;
17
use PhpCsFixer\Console\Command\FixCommand as FixCmd;
18
use PhpCsFixer\Fixer;
19
use PhpCsFixer\ToolInfo;
20
use Symfony\Component\Console\Input\InputArgument;
21
22
class FixCommand extends Command
23
{
24
    const COMMAND_NAME = 'fix';
25
26
    /**
27
     * @var FixCmd
28
     */
29
    protected $fixCmd;
30
31
    /**
32
     * @var ConfigFactory
33
     */
34
    protected $configFactory;
35
36
    /**
37
     * @var Config
38
     */
39
    protected $defaultConfig;
40
41
    public function __construct(ConfigFactory $configFactory = null, FixCmd $fixCmd = null)
42
    {
43
        if ($configFactory === null) {
44
            $configFactory = $this->buildDefaultConfigFactory();
45
        }
46
        $this->configFactory = $configFactory;
47
48
        if ($fixCmd === null) {
49
            $fixCmd = $this->buildDefaultFixCommand();
50
        }
51
        $this->fixCmd = $fixCmd;
52
53
        parent::__construct(self::COMMAND_NAME);
54
    }
55
56
    /**
57
     * @return FixCmd
58
     */
59
    protected function buildDefaultFixCommand()
60
    {
61
        return new FixCmd(new ToolInfo);
62
    }
63
64
    /**
65
     * @return ConfigFactory
66
     */
67
    protected function buildDefaultConfigFactory()
68
    {
69
        return new ConfigFactory;
70
    }
71
72
    /**
73
     * @param InputInterface $input
74
     * @param string $name
75
     * @param bool $default
76
     */
77
    protected function parseOptionalOptionValue(&$input, $name, $default)
78
    {
79
        $values = $input->getOption($name);
80
81
        if (count($values) === 0) {
82
            // no option; use default
83
            $input->setOption($name, Get\value($name, $this->defaultConfig, $default));
84
85
            return;
86
        }
87
88
        $value = end($values);
89
90
        if ($value === null) {
91
            // option w/o value
92
            $value = true;
93
        }
94
95
        $input->setOption($name, filter_var($value, FILTER_VALIDATE_BOOLEAN));
96
    }
97
98
    protected function configure()
99
    {
100
        parent::configure();
101
        $this->setDefinition([
102
            new InputArgument('path', InputArgument::IS_ARRAY, 'The path.'),
103
            new InputOption('path-mode', '', InputOption::VALUE_REQUIRED, 'Specify path mode (can be override or intersection).', 'override'),
104
            new InputOption('allow-risky', '', InputOption::VALUE_REQUIRED, 'Are risky fixers allowed (can be yes or no).'),
105
            new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The path to a .php_cs file.'),
106
            new InputOption('dry-run', '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Only shows which files would have been modified.'),
107
            new InputOption('rules', '', InputOption::VALUE_REQUIRED, 'The rules.'),
108
            new InputOption('using-cache', '', InputOption::VALUE_REQUIRED, 'Does cache should be used (can be yes or no).'),
109
            new InputOption('cache-file', '', InputOption::VALUE_REQUIRED, 'The path to the cache file.'),
110
            new InputOption('diff', '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Also produce diff for each file.'),
111
            new InputOption('diff-format', '', InputOption::VALUE_REQUIRED, 'Specify diff format.'),
112
            new InputOption('format', '', InputOption::VALUE_REQUIRED, 'To output results in other formats.'),
113
            new InputOption('stop-on-violation', '', InputOption::VALUE_NONE, 'Stop execution on first violation.'),
114
            new InputOption('show-progress', '', InputOption::VALUE_REQUIRED, 'Type of progress indicator (none, run-in, estimating, estimating-max or dots).'),
115
            new InputOption('namespace', null, InputOption::VALUE_REQUIRED, 'composer.json/extra namespace', 'sugared-rim/php-cs-fixer'),
116
        ])->setDescription('Fixes a directory or a file.');
117
    }
118
119
    protected function initialize(InputInterface $input, OutputInterface $output)
120
    {
121
        $namespace = $input->getOption('namespace');
122
        $this->defaultConfig = call_user_func($this->configFactory, $namespace);
123
124
        $fixCmdDefaultConfig = new ReflectionProperty(FixCmd::class, 'defaultConfig');
125
        $fixCmdDefaultConfig->setAccessible(true);
126
        $fixCmdDefaultConfig->setValue($this->fixCmd, $this->defaultConfig);
127
128
        $this->parseOptionalOptionValue($input, 'diff', false);
129
        $this->parseOptionalOptionValue($input, 'dry-run', false);
130
131
        parent::initialize($input, $output);
132
    }
133
134
    protected function execute(InputInterface $input, OutputInterface $output)
135
    {
136
        $fixCmdConfigure = new ReflectionMethod(FixCmd::class, 'execute');
137
        $fixCmdConfigure->setAccessible(true);
138
        return $fixCmdConfigure->invoke($this->fixCmd, $input, $output);
139
    }
140
}
141