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
|
|
|
public 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
|
|
|
public 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
|
|
|
/** |
76
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
77
|
|
|
*/ |
78
|
|
|
public function sugaredRimProcessConfig() |
79
|
|
|
{ |
80
|
|
|
foreach ($this->sugaredRimGetConfig() as $key => $value) { |
81
|
|
|
switch ($key) { |
82
|
|
|
case 'presets': |
83
|
|
|
case 'files': |
84
|
|
|
continue 2; |
85
|
|
|
case 'default_standard': |
86
|
|
|
if (is_array($value)) { |
87
|
|
|
$value = implode(',', $value); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (method_exists($this, 'setConfigData')) { |
92
|
|
|
$this->setConfigData($key, $value, true); |
|
|
|
|
93
|
|
|
} else { |
94
|
|
|
\PHP_CodeSniffer::setConfigData($key, $value, true); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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: