PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / extra / php-codesniffer-php-token / Util / Standards.php

Standards.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at src/extra/php-codesniffer-php-token/Util/Standards.php

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