comparison vendor/squizlabs/php_codesniffer/src/Util/Standards.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php
2 /**
3 * Functions for helping process standards.
4 *
5 * @author Greg Sherwood <gsherwood@squiz.net>
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 $ds = DIRECTORY_SEPARATOR;
26
27 $installedPaths = [dirname(dirname(__DIR__)).$ds.'src'.$ds.'Standards'];
28 $configPaths = Config::getConfigData('installed_paths');
29 if ($configPaths !== null) {
30 $installedPaths = array_merge($installedPaths, explode(',', $configPaths));
31 }
32
33 $resolvedInstalledPaths = [];
34 foreach ($installedPaths as $installedPath) {
35 if (substr($installedPath, 0, 1) === '.') {
36 $installedPath = Common::realPath(__DIR__.$ds.'..'.$ds.'..'.$ds.$installedPath);
37 }
38
39 $resolvedInstalledPaths[] = $installedPath;
40 }
41
42 return $resolvedInstalledPaths;
43
44 }//end getInstalledStandardPaths()
45
46
47 /**
48 * Get the details of all coding standards installed.
49 *
50 * Coding standards are directories located in the
51 * CodeSniffer/Standards directory. Valid coding standards
52 * include a Sniffs subdirectory.
53 *
54 * The details returned for each standard are:
55 * - path: the path to the coding standard's main directory
56 * - name: the name of the coding standard, as sourced from the ruleset.xml file
57 * - namespace: the namespace used by the coding standard, as sourced from the ruleset.xml file
58 *
59 * If you only need the paths to the installed standards,
60 * use getInstalledStandardPaths() instead as it performs less work to
61 * retrieve coding standard names.
62 *
63 * @param boolean $includeGeneric If true, the special "Generic"
64 * coding standard will be included
65 * if installed.
66 * @param string $standardsDir A specific directory to look for standards
67 * in. If not specified, PHP_CodeSniffer will
68 * look in its default locations.
69 *
70 * @return array
71 * @see getInstalledStandardPaths()
72 */
73 public static function getInstalledStandardDetails(
74 $includeGeneric=false,
75 $standardsDir=''
76 ) {
77 $rulesets = [];
78
79 if ($standardsDir === '') {
80 $installedPaths = self::getInstalledStandardPaths();
81 } else {
82 $installedPaths = [$standardsDir];
83 }
84
85 foreach ($installedPaths as $standardsDir) {
86 // Check if the installed dir is actually a standard itself.
87 $csFile = $standardsDir.'/ruleset.xml';
88 if (is_file($csFile) === true) {
89 $rulesets[] = $csFile;
90 continue;
91 }
92
93 if (is_dir($standardsDir) === false) {
94 continue;
95 }
96
97 $di = new \DirectoryIterator($standardsDir);
98 foreach ($di as $file) {
99 if ($file->isDir() === true && $file->isDot() === false) {
100 $filename = $file->getFilename();
101
102 // Ignore the special "Generic" standard.
103 if ($includeGeneric === false && $filename === 'Generic') {
104 continue;
105 }
106
107 // Valid coding standard dirs include a ruleset.
108 $csFile = $file->getPathname().'/ruleset.xml';
109 if (is_file($csFile) === true) {
110 $rulesets[] = $csFile;
111 }
112 }
113 }
114 }//end foreach
115
116 $installedStandards = [];
117
118 foreach ($rulesets as $rulesetPath) {
119 $ruleset = simplexml_load_string(file_get_contents($rulesetPath));
120 if ($ruleset === false) {
121 continue;
122 }
123
124 $standardName = (string) $ruleset['name'];
125 $dirname = basename(dirname($rulesetPath));
126
127 if (isset($ruleset['namespace']) === true) {
128 $namespace = (string) $ruleset['namespace'];
129 } else {
130 $namespace = $dirname;
131 }
132
133 $installedStandards[$dirname] = [
134 'path' => dirname($rulesetPath),
135 'name' => $standardName,
136 'namespace' => $namespace,
137 ];
138 }//end foreach
139
140 return $installedStandards;
141
142 }//end getInstalledStandardDetails()
143
144
145 /**
146 * Get a list of all coding standards installed.
147 *
148 * Coding standards are directories located in the
149 * CodeSniffer/Standards directory. Valid coding standards
150 * include a Sniffs subdirectory.
151 *
152 * @param boolean $includeGeneric If true, the special "Generic"
153 * coding standard will be included
154 * if installed.
155 * @param string $standardsDir A specific directory to look for standards
156 * in. If not specified, PHP_CodeSniffer will
157 * look in its default locations.
158 *
159 * @return array
160 * @see isInstalledStandard()
161 */
162 public static function getInstalledStandards(
163 $includeGeneric=false,
164 $standardsDir=''
165 ) {
166 $installedStandards = [];
167
168 if ($standardsDir === '') {
169 $installedPaths = self::getInstalledStandardPaths();
170 } else {
171 $installedPaths = [$standardsDir];
172 }
173
174 foreach ($installedPaths as $standardsDir) {
175 // Check if the installed dir is actually a standard itself.
176 $csFile = $standardsDir.'/ruleset.xml';
177 if (is_file($csFile) === true) {
178 $installedStandards[] = basename($standardsDir);
179 continue;
180 }
181
182 if (is_dir($standardsDir) === false) {
183 // Doesn't exist.
184 continue;
185 }
186
187 $di = new \DirectoryIterator($standardsDir);
188 foreach ($di as $file) {
189 if ($file->isDir() === true && $file->isDot() === false) {
190 $filename = $file->getFilename();
191
192 // Ignore the special "Generic" standard.
193 if ($includeGeneric === false && $filename === 'Generic') {
194 continue;
195 }
196
197 // Valid coding standard dirs include a ruleset.
198 $csFile = $file->getPathname().'/ruleset.xml';
199 if (is_file($csFile) === true) {
200 $installedStandards[] = $filename;
201 }
202 }
203 }
204 }//end foreach
205
206 return $installedStandards;
207
208 }//end getInstalledStandards()
209
210
211 /**
212 * Determine if a standard is installed.
213 *
214 * Coding standards are directories located in the
215 * CodeSniffer/Standards directory. Valid coding standards
216 * include a ruleset.xml file.
217 *
218 * @param string $standard The name of the coding standard.
219 *
220 * @return boolean
221 * @see getInstalledStandards()
222 */
223 public static function isInstalledStandard($standard)
224 {
225 $path = self::getInstalledStandardPath($standard);
226 if ($path !== null && strpos($path, 'ruleset.xml') !== false) {
227 return true;
228 } else {
229 // This could be a custom standard, installed outside our
230 // standards directory.
231 $standard = Common::realPath($standard);
232
233 // Might be an actual ruleset file itUtil.
234 // If it has an XML extension, let's at least try it.
235 if (is_file($standard) === true
236 && (substr(strtolower($standard), -4) === '.xml'
237 || substr(strtolower($standard), -9) === '.xml.dist')
238 ) {
239 return true;
240 }
241
242 // If it is a directory with a ruleset.xml file in it,
243 // it is a standard.
244 $ruleset = rtrim($standard, ' /\\').DIRECTORY_SEPARATOR.'ruleset.xml';
245 if (is_file($ruleset) === true) {
246 return true;
247 }
248 }//end if
249
250 return false;
251
252 }//end isInstalledStandard()
253
254
255 /**
256 * Return the path of an installed coding standard.
257 *
258 * Coding standards are directories located in the
259 * CodeSniffer/Standards directory. Valid coding standards
260 * include a ruleset.xml file.
261 *
262 * @param string $standard The name of the coding standard.
263 *
264 * @return string|null
265 */
266 public static function getInstalledStandardPath($standard)
267 {
268 if (strpos($standard, '.') !== false) {
269 return null;
270 }
271
272 $installedPaths = self::getInstalledStandardPaths();
273 foreach ($installedPaths as $installedPath) {
274 $standardPath = $installedPath.DIRECTORY_SEPARATOR.$standard;
275 if (file_exists($standardPath) === false) {
276 if (basename($installedPath) !== $standard) {
277 continue;
278 }
279
280 $standardPath = $installedPath;
281 }
282
283 $path = Common::realpath($standardPath.DIRECTORY_SEPARATOR.'ruleset.xml');
284
285 if (is_file($path) === true) {
286 return $path;
287 } else if (Common::isPharFile($standardPath) === true) {
288 $path = Common::realpath($standardPath);
289 if ($path !== false) {
290 return $path;
291 }
292 }
293 }//end foreach
294
295 return null;
296
297 }//end getInstalledStandardPath()
298
299
300 /**
301 * Prints out a list of installed coding standards.
302 *
303 * @return void
304 */
305 public static function printInstalledStandards()
306 {
307 $installedStandards = self::getInstalledStandards();
308 $numStandards = count($installedStandards);
309
310 if ($numStandards === 0) {
311 echo 'No coding standards are installed.'.PHP_EOL;
312 } else {
313 $lastStandard = array_pop($installedStandards);
314 if ($numStandards === 1) {
315 echo "The only coding standard installed is $lastStandard".PHP_EOL;
316 } else {
317 $standardList = implode(', ', $installedStandards);
318 $standardList .= ' and '.$lastStandard;
319 echo 'The installed coding standards are '.$standardList.PHP_EOL;
320 }
321 }
322
323 }//end printInstalledStandards()
324
325
326 }//end class