annotate vendor/squizlabs/php_codesniffer/src/Util/Standards.php @ 5:12f9dff5fda9 tip

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