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