|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SugaredRim\PHP\CodeSniffer; |
|
4
|
|
|
|
|
5
|
|
|
use Schnittstabil\ComposerExtra\ComposerExtra; |
|
6
|
|
|
use Schnittstabil\FinderByConfig\FinderByConfig; |
|
7
|
|
|
|
|
8
|
|
|
trait SugaredRimConfigAwareTrait |
|
9
|
|
|
{ |
|
10
|
|
|
protected $sugaredRimNamespace = 'sugared-rim/php_codesniffer'; |
|
11
|
|
|
protected $sugaredRimfinderByConfig; |
|
12
|
|
|
protected $sugaredRimDefaultConfig; |
|
13
|
|
|
protected $sugaredRimConfig; |
|
14
|
|
|
|
|
15
|
|
|
protected function sugaredRimSetFinderByConfig(callable $finderByConfig = null) |
|
16
|
|
|
{ |
|
17
|
|
|
if ($finderByConfig === null) { |
|
18
|
|
|
$finderByConfig = new FinderByConfig(); |
|
19
|
|
|
} |
|
20
|
|
|
$this->sugaredRimfinderByConfig = $finderByConfig; |
|
21
|
|
|
|
|
22
|
|
|
$this->sugaredRimDefaultConfig = new \stdClass(); |
|
23
|
|
|
$this->sugaredRimDefaultConfig->presets = [ |
|
24
|
|
|
'SugaredRim\\PHP\\CodeSniffer\\DefaultPreset::get', |
|
25
|
|
|
]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function sugaredRimGetConfig($path = null, $default = null) |
|
29
|
|
|
{ |
|
30
|
|
|
if ($this->sugaredRimConfig === null) { |
|
31
|
|
|
$this->sugaredRimConfig = new ComposerExtra( |
|
32
|
|
|
$this->sugaredRimNamespace, |
|
33
|
|
|
$this->sugaredRimDefaultConfig, |
|
34
|
|
|
'presets' |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if ($path === null) { |
|
39
|
|
|
$path = array(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $this->sugaredRimConfig->get($path, $default); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function processLongArgument($arg, $pos) |
|
46
|
|
|
{ |
|
47
|
|
|
if (substr($arg, 0, 10) === 'namespace=') { |
|
48
|
|
|
$this->sugaredRimNamespace = substr($arg, 10); |
|
49
|
|
|
|
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
parent::processLongArgument($arg, $pos); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function setCommandLineValues($args) |
|
57
|
|
|
{ |
|
58
|
|
|
parent::setCommandLineValues($args); |
|
59
|
|
|
|
|
60
|
|
|
if (isset($this->values) && !empty($this->values['files'])) { |
|
|
|
|
|
|
61
|
|
|
// v2.* |
|
62
|
|
|
return; |
|
63
|
|
|
} elseif (isset($this->files) && !empty($this->files)) { |
|
|
|
|
|
|
64
|
|
|
// v3.* |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$files = $this->sugaredRimGetConfig('files', []); |
|
69
|
|
|
|
|
70
|
|
|
foreach (call_user_func($this->sugaredRimfinderByConfig, $files) as $file) { |
|
71
|
|
|
$this->processUnknownArgument($file->getPathname(), -1); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function sugaredRimProcessConfig() |
|
76
|
|
|
{ |
|
77
|
|
|
foreach ($this->sugaredRimGetConfig() as $key => $value) { |
|
78
|
|
|
switch ($key) { |
|
79
|
|
|
case 'presets': |
|
80
|
|
|
case 'files': |
|
81
|
|
|
continue 2; |
|
82
|
|
|
case 'default_standard': |
|
83
|
|
|
if (is_array($value)) { |
|
84
|
|
|
$value = implode(',', $value); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->sugaredRimSetConfigData($key, $value); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function sugaredRimSetConfigData($key, $value) |
|
96
|
|
|
{ |
|
97
|
|
|
if (method_exists($this, 'setConfigData')) { |
|
98
|
|
|
$this->setConfigData($key, $value, true); |
|
|
|
|
|
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
\PHP_CodeSniffer::setConfigData($key, $value, true); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
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: