|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Functions for helping process standards. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Greg Sherwood <[email protected]> |
|
6
|
|
|
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
|
7
|
|
|
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace PHP_CodeSniffer\Util; |
|
11
|
|
|
|
|
12
|
|
|
use PHP_CodeSniffer\Config; |
|
13
|
|
|
|
|
14
|
|
|
class Standards |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Get a list paths where standards are installed. |
|
20
|
|
|
* |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function getInstalledStandardPaths() |
|
24
|
|
|
{ |
|
25
|
|
|
$installedPaths = array(Common::realPath(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Standards')); |
|
26
|
|
|
$configPaths = Config::getConfigData('installed_paths'); |
|
27
|
|
|
if ($configPaths !== null) { |
|
28
|
|
|
$installedPaths = array_merge($installedPaths, explode(',', $configPaths)); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$resolvedInstalledPaths = array(); |
|
32
|
|
|
foreach ($installedPaths as $installedPath) { |
|
33
|
|
|
if (substr($installedPath, 0, 1) === '.') { |
|
34
|
|
|
$installedPath = Common::realPath(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.$installedPath); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$resolvedInstalledPaths[] = $installedPath; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $resolvedInstalledPaths; |
|
41
|
|
|
|
|
42
|
|
|
}//end getInstalledStandardPaths() |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get a list of all coding standards installed. |
|
47
|
|
|
* |
|
48
|
|
|
* Coding standards are directories located in the |
|
49
|
|
|
* CodeSniffer/Standards directory. Valid coding standards |
|
50
|
|
|
* include a Sniffs subdirectory. |
|
51
|
|
|
* |
|
52
|
|
|
* @param boolean $includeGeneric If true, the special "Generic" |
|
53
|
|
|
* coding standard will be included |
|
54
|
|
|
* if installed. |
|
55
|
|
|
* @param string $standardsDir A specific directory to look for standards |
|
56
|
|
|
* in. If not specified, PHP_CodeSniffer will |
|
57
|
|
|
* look in its default locations. |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
* @see isInstalledStandard() |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function getInstalledStandards( |
|
63
|
|
|
$includeGeneric=false, |
|
64
|
|
|
$standardsDir='' |
|
65
|
|
|
) { |
|
66
|
|
|
$installedStandards = array(); |
|
67
|
|
|
|
|
68
|
|
|
if ($standardsDir === '') { |
|
69
|
|
|
$installedPaths = self::getInstalledStandardPaths(); |
|
70
|
|
|
} else { |
|
71
|
|
|
$installedPaths = array($standardsDir); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
foreach ($installedPaths as $standardsDir) { |
|
75
|
|
|
// Check if the installed dir is actually a standard itself. |
|
76
|
|
|
$csFile = $standardsDir.'/ruleset.xml'; |
|
77
|
|
|
if (is_file($csFile) === true) { |
|
78
|
|
|
$installedStandards[] = basename($standardsDir); |
|
79
|
|
|
continue; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$di = new \DirectoryIterator($standardsDir); |
|
83
|
|
|
foreach ($di as $file) { |
|
84
|
|
|
if ($file->isDir() === true && $file->isDot() === false) { |
|
85
|
|
|
$filename = $file->getFilename(); |
|
86
|
|
|
|
|
87
|
|
|
// Ignore the special "Generic" standard. |
|
88
|
|
|
if ($includeGeneric === false && $filename === 'Generic') { |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Valid coding standard dirs include a ruleset. |
|
93
|
|
|
$csFile = $file->getPathname().'/ruleset.xml'; |
|
94
|
|
|
if (is_file($csFile) === true) { |
|
95
|
|
|
$installedStandards[] = $filename; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
}//end foreach |
|
100
|
|
|
|
|
101
|
|
|
return $installedStandards; |
|
102
|
|
|
|
|
103
|
|
|
}//end getInstalledStandards() |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Determine if a standard is installed. |
|
108
|
|
|
* |
|
109
|
|
|
* Coding standards are directories located in the |
|
110
|
|
|
* CodeSniffer/Standards directory. Valid coding standards |
|
111
|
|
|
* include a ruleset.xml file. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $standard The name of the coding standard. |
|
114
|
|
|
* |
|
115
|
|
|
* @return boolean |
|
116
|
|
|
* @see getInstalledStandards() |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function isInstalledStandard($standard) |
|
119
|
|
|
{ |
|
120
|
|
|
$path = self::getInstalledStandardPath($standard); |
|
121
|
|
|
if ($path !== null && strpos($path, 'ruleset.xml') !== false) { |
|
122
|
|
|
return true; |
|
123
|
|
|
} else { |
|
124
|
|
|
// This could be a custom standard, installed outside our |
|
125
|
|
|
// standards directory. |
|
126
|
|
|
$standard = Common::realPath($standard); |
|
127
|
|
|
|
|
128
|
|
|
// Might be an actual ruleset file itUtil. |
|
129
|
|
|
// If it has an XML extension, let's at least try it. |
|
130
|
|
|
if (is_file($standard) === true |
|
131
|
|
|
&& (substr(strtolower($standard), -4) === '.xml' |
|
132
|
|
|
|| substr(strtolower($standard), -9) === '.xml.dist') |
|
133
|
|
|
) { |
|
134
|
|
|
return true; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// If it is a directory with a ruleset.xml file in it, |
|
138
|
|
|
// it is a standard. |
|
139
|
|
|
$ruleset = rtrim($standard, ' /\\').DIRECTORY_SEPARATOR.'ruleset.xml'; |
|
140
|
|
|
if (is_file($ruleset) === true) { |
|
141
|
|
|
return true; |
|
142
|
|
|
} |
|
143
|
|
|
}//end if |
|
144
|
|
|
|
|
145
|
|
|
return false; |
|
146
|
|
|
|
|
147
|
|
|
}//end isInstalledStandard() |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Return the path of an installed coding standard. |
|
152
|
|
|
* |
|
153
|
|
|
* Coding standards are directories located in the |
|
154
|
|
|
* CodeSniffer/Standards directory. Valid coding standards |
|
155
|
|
|
* include a ruleset.xml file. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $standard The name of the coding standard. |
|
158
|
|
|
* |
|
159
|
|
|
* @return string|null |
|
160
|
|
|
*/ |
|
161
|
|
|
public static function getInstalledStandardPath($standard) |
|
162
|
|
|
{ |
|
163
|
|
|
$installedPaths = self::getInstalledStandardPaths(); |
|
164
|
|
|
foreach ($installedPaths as $installedPath) { |
|
165
|
|
|
if (basename($installedPath) === $standard) { |
|
166
|
|
|
$standardPath = $installedPath; |
|
167
|
|
|
} else { |
|
168
|
|
|
$standardPath = $installedPath.DIRECTORY_SEPARATOR.$standard; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$path = Common::realpath($standardPath.DIRECTORY_SEPARATOR.'ruleset.xml'); |
|
172
|
|
|
|
|
173
|
|
|
if (is_file($path) === true) { |
|
174
|
|
|
return $path; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return null; |
|
179
|
|
|
|
|
180
|
|
|
}//end getInstalledStandardPath() |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Prints out a list of installed coding standards. |
|
185
|
|
|
* |
|
186
|
|
|
* @return void |
|
187
|
|
|
*/ |
|
188
|
|
|
public static function printInstalledStandards() |
|
189
|
|
|
{ |
|
190
|
|
|
$installedStandards = self::getInstalledStandards(); |
|
191
|
|
|
$numStandards = count($installedStandards); |
|
192
|
|
|
|
|
193
|
|
|
if ($numStandards === 0) { |
|
194
|
|
|
echo 'No coding standards are installed.'.PHP_EOL; |
|
195
|
|
|
} else { |
|
196
|
|
|
$lastStandard = array_pop($installedStandards); |
|
197
|
|
|
if ($numStandards === 1) { |
|
198
|
|
|
echo "The only coding standard installed is $lastStandard".PHP_EOL; |
|
199
|
|
|
} else { |
|
200
|
|
|
$standardList = implode(', ', $installedStandards); |
|
201
|
|
|
$standardList .= ' and '.$lastStandard; |
|
202
|
|
|
echo 'The installed coding standards are '.$standardList.PHP_EOL; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
}//end printInstalledStandards() |
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
}//end class |
|
210
|
|
|
|