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