Source for file Standards.php
Documentation is available at Standards.php
* Functions for helping process standards.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Util;
use PHP_CodeSniffer\Config;
* Get a list paths where standards are installed.
public static function getInstalledStandardPaths ()
$ds = DIRECTORY_SEPARATOR;
$installedPaths = array (dirname(dirname(__DIR__ )). $ds. 'src'. $ds. 'Standards');
$configPaths = Config ::getConfigData ('installed_paths');
if ($configPaths !== null ) {
$resolvedInstalledPaths = array ();
foreach ($installedPaths as $installedPath) {
if (substr($installedPath, 0 , 1 ) === '.') {
$installedPath = Common ::realPath (__DIR__. $ds. '..'. $ds. '..'. $ds. $installedPath);
$resolvedInstalledPaths[] = $installedPath;
return $resolvedInstalledPaths;
}//end getInstalledStandardPaths()
* Get the details of all coding standards installed.
* Coding standards are directories located in the
* CodeSniffer/Standards directory. Valid coding standards
* include a Sniffs subdirectory.
* The details returned for each standard are:
* - path: the path to the coding standard's main directory
* - name: the name of the coding standard, as sourced from the ruleset.xml file
* - namespace: the namespace used by the coding standard, as sourced from the ruleset.xml file
* If you only need the paths to the installed standards,
* use getInstalledStandardPaths() instead as it performs less work to
* retrieve coding standard names.
* @param boolean $includeGeneric If true, the special "Generic"
* coding standard will be included
* @param string $standardsDir A specific directory to look for standards
* in. If not specified, PHP_CodeSniffer will
* look in its default locations.
* @see getInstalledStandardPaths()
public static function getInstalledStandardDetails (
if ($standardsDir === '') {
$installedPaths = self ::getInstalledStandardPaths ();
$installedPaths = array ($standardsDir);
foreach ($installedPaths as $standardsDir) {
// Check if the installed dir is actually a standard itself.
$csFile = $standardsDir. '/ruleset.xml';
$di = new \DirectoryIterator ($standardsDir);
if ($file->isDir () === true && $file->isDot () === false ) {
$filename = $file->getFilename ();
// Ignore the special "Generic" standard.
if ($includeGeneric === false && $filename === 'Generic') {
// Valid coding standard dirs include a ruleset.
$csFile = $file->getPathname (). '/ruleset.xml';
$installedStandards = array ();
foreach ($rulesets as $rulesetPath) {
if ($ruleset === false ) {
$standardName = (string) $ruleset['name'];
if (isset ($ruleset['namespace']) === true ) {
$namespace = (string) $ruleset['namespace'];
$installedStandards[$dirname] = array (
'namespace' => $namespace,
return $installedStandards;
}//end getInstalledStandardDetails()
* Get a list of all coding standards installed.
* Coding standards are directories located in the
* CodeSniffer/Standards directory. Valid coding standards
* include a Sniffs subdirectory.
* @param boolean $includeGeneric If true, the special "Generic"
* coding standard will be included
* @param string $standardsDir A specific directory to look for standards
* in. If not specified, PHP_CodeSniffer will
* look in its default locations.
* @see isInstalledStandard()
public static function getInstalledStandards (
$installedStandards = array ();
if ($standardsDir === '') {
$installedPaths = self ::getInstalledStandardPaths ();
$installedPaths = array ($standardsDir);
foreach ($installedPaths as $standardsDir) {
// Check if the installed dir is actually a standard itself.
$csFile = $standardsDir. '/ruleset.xml';
$installedStandards[] = basename($standardsDir);
if (is_dir($standardsDir) === false ) {
$di = new \DirectoryIterator ($standardsDir);
if ($file->isDir () === true && $file->isDot () === false ) {
$filename = $file->getFilename ();
// Ignore the special "Generic" standard.
if ($includeGeneric === false && $filename === 'Generic') {
// Valid coding standard dirs include a ruleset.
$csFile = $file->getPathname (). '/ruleset.xml';
$installedStandards[] = $filename;
return $installedStandards;
}//end getInstalledStandards()
* Determine if a standard is installed.
* Coding standards are directories located in the
* CodeSniffer/Standards directory. Valid coding standards
* include a ruleset.xml file.
* @param string $standard The name of the coding standard.
* @see getInstalledStandards()
public static function isInstalledStandard ($standard)
$path = self ::getInstalledStandardPath ($standard);
if ($path !== null && strpos($path, 'ruleset.xml') !== false ) {
// This could be a custom standard, installed outside our
$standard = Common ::realPath ($standard);
// Might be an actual ruleset file itUtil.
// If it has an XML extension, let's at least try it.
// If it is a directory with a ruleset.xml file in it,
$ruleset = rtrim($standard, ' /\\').DIRECTORY_SEPARATOR. 'ruleset.xml';
}//end isInstalledStandard()
* Return the path of an installed coding standard.
* Coding standards are directories located in the
* CodeSniffer/Standards directory. Valid coding standards
* include a ruleset.xml file.
* @param string $standard The name of the coding standard.
public static function getInstalledStandardPath ($standard)
if (strpos($standard, '.') !== false ) {
$installedPaths = self ::getInstalledStandardPaths ();
foreach ($installedPaths as $installedPath) {
$standardPath = $installedPath.DIRECTORY_SEPARATOR. $standard;
if (basename($installedPath) !== $standard) {
$standardPath = $installedPath;
$path = Common ::realpath ($standardPath.DIRECTORY_SEPARATOR. 'ruleset.xml');
} else if (Common ::isPharFile ($standardPath) === true ) {
$path = Common ::realpath ($standardPath);
}//end getInstalledStandardPath()
* Prints out a list of installed coding standards.
public static function printInstalledStandards ()
$installedStandards = self ::getInstalledStandards ();
$numStandards = count($installedStandards);
if ($numStandards === 0 ) {
echo 'No coding standards are installed.'.PHP_EOL;
$lastStandard = array_pop($installedStandards);
if ($numStandards === 1 ) {
echo " The only coding standard installed is $lastStandard".PHP_EOL;
$standardList = implode(', ', $installedStandards);
$standardList .= ' and '. $lastStandard;
echo 'The installed coding standards are '. $standardList.PHP_EOL;
}//end printInstalledStandards()
Documentation generated on Mon, 11 Mar 2019 15:27:47 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|