getInstalledStandards() public static method

Coding standards are directories located in the CodeSniffer/Standards directory. Valid coding standards include a Sniffs subdirectory.
See also: isInstalledStandard()
public static getInstalledStandards ( boolean $includeGeneric = false, string $standardsDir = '' ) : array
$includeGeneric boolean If true, the special "Generic" coding standard will be included if installed.
$standardsDir string A specific directory to look for standards in. If not specified, PHP_CodeSniffer will look in its default locations.
return array
Beispiel #1
0
 /**
  * Add all sniff unit tests into a test suite.
  *
  * Sniff unit tests are found by recursing through the 'Tests' directory
  * of each installed coding standard.
  *
  * @return PHPUnit_Framework_TestSuite
  */
 public static function suite()
 {
     $suite = new PHPUnit_Framework_TestSuite('PHP CodeSniffer Standards');
     $isInstalled = !is_file(dirname(__FILE__) . '/../../CodeSniffer.php');
     if ($isInstalled === false) {
         // We have not been installed.
         $standardsDir = realpath(dirname(__FILE__) . '/../../CodeSniffer/Standards');
     } else {
         $standardsDir = '';
     }
     $standards = PHP_CodeSniffer::getInstalledStandards(true, $standardsDir);
     foreach ($standards as $standard) {
         if ($isInstalled === false) {
             $standardDir = realpath($standardsDir . '/' . $standard . '/Tests/');
         } else {
             $standardDir = dirname(__FILE__) . '/' . $standard . '/Tests/';
         }
         if (is_dir($standardDir) === false) {
             // No tests for this standard.
             continue;
         }
         $di = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($standardDir));
         foreach ($di as $file) {
             // Skip hidden files.
             if (substr($file->getFilename(), 0, 1) === '.') {
                 continue;
             }
             // Tests must have the extention 'php'.
             $parts = explode('.', $file);
             $ext = array_pop($parts);
             if ($ext !== 'php') {
                 continue;
             }
             $filePath = realpath($file->getPathname());
             if ($isInstalled === false) {
                 $className = str_replace($standardDir . DIRECTORY_SEPARATOR, '', $filePath);
             } else {
                 $className = str_replace(dirname(__FILE__) . DIRECTORY_SEPARATOR, '', $filePath);
             }
             $className = substr($className, 0, -4);
             $className = str_replace(DIRECTORY_SEPARATOR, '_', $className);
             if ($isInstalled === false) {
                 $className = $standard . '_Tests_' . $className;
             }
             $niceName = substr($className, strrpos($className, '_') + 1, -8);
             include_once $filePath;
             $class = new $className($niceName);
             $suite->addTest($class);
         }
         //end foreach
     }
     //end foreach
     return $suite;
 }
 /**
  * Add all sniff unit tests into a test suite.
  *
  * Sniff unit tests are found by recursing through the 'Tests' directory
  * of each installed coding standard.
  *
  * @return PHPUnit_Framework_TestSuite
  */
 public static function suite()
 {
     $suite = new PHPUnit_Framework_TestSuite('PHP CodeSniffer Standards');
     $isInstalled = !is_file(dirname(__FILE__) . '/../../CodeSniffer.php');
     $installedPaths = PHP_CodeSniffer::getInstalledStandardPaths();
     foreach ($installedPaths as $path) {
         $path = realpath($path);
         $origPath = $path;
         $standards = PHP_CodeSniffer::getInstalledStandards(true, $path);
         // If the test is running PEAR installed, the built-in standards
         // are split into different directories; one for the sniffs and
         // a different file system location for tests.
         if ($isInstalled === true && is_dir($path . DIRECTORY_SEPARATOR . 'Generic') === true) {
             $path = dirname(__FILE__);
         }
         foreach ($standards as $standard) {
             $testsDir = $path . DIRECTORY_SEPARATOR . $standard . DIRECTORY_SEPARATOR . 'Tests' . DIRECTORY_SEPARATOR;
             if (is_dir($testsDir) === false) {
                 // No tests for this standard.
                 continue;
             }
             $di = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($testsDir));
             foreach ($di as $file) {
                 // Skip hidden files.
                 if (substr($file->getFilename(), 0, 1) === '.') {
                     continue;
                 }
                 // Tests must have the extension 'php'.
                 $parts = explode('.', $file);
                 $ext = array_pop($parts);
                 if ($ext !== 'php') {
                     continue;
                 }
                 $filePath = $file->getPathname();
                 $className = str_replace($path . DIRECTORY_SEPARATOR, '', $filePath);
                 $className = substr($className, 0, -4);
                 $className = str_replace(DIRECTORY_SEPARATOR, '_', $className);
                 // Include the sniff here so tests can use it in their setup() methods.
                 $parts = explode('_', $className);
                 $sniffPath = $origPath . DIRECTORY_SEPARATOR . $parts[0] . DIRECTORY_SEPARATOR . 'Sniffs' . DIRECTORY_SEPARATOR . $parts[2] . DIRECTORY_SEPARATOR . $parts[3];
                 $sniffPath = substr($sniffPath, 0, -8) . 'Sniff.php';
                 include_once $sniffPath;
                 include_once $filePath;
                 $GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS'][$className] = $path;
                 $suite->addTestSuite($className);
             }
             //end foreach
         }
         //end foreach
     }
     //end foreach
     return $suite;
 }
Beispiel #3
0
 /**
  * Show the available coding standards
  *
  * @param array $options
  */
 public function showCodestyleStandards($options = [])
 {
     $phpcs = new \PHP_CodeSniffer();
     $standards = $phpcs->getInstalledStandards();
     sort($standards);
     if (!$options['no-ansi']) {
         array_walk($standards, function (&$value) {
             $value = "<fg=green>{$value}</fg=green>";
         });
     }
     $last = array_pop($standards);
     $this->say("Installed coding standards are " . implode(', ', $standards) . " and " . $last);
 }
 /**
  * Add all sniff unit tests into a test suite.
  *
  * Sniff unit tests are found by recursing through the 'Tests' directory
  * of each installed coding standard.
  *
  * @return PHPUnit_Framework_TestSuite
  */
 public static function suite()
 {
     $suite = new PHPUnit_Framework_TestSuite('PHP CodeSniffer Standards');
     $isInstalled = !is_file(dirname(__FILE__) . '/../../CodeSniffer.php');
     if ($isInstalled === false) {
         // We have not been installed.
         $standardsDir = realpath(dirname(__FILE__) . '/../../CodeSniffer/Standards');
     } else {
         $standardsDir = '';
     }
     $standards = PHP_CodeSniffer::getInstalledStandards(true, $standardsDir);
     foreach ($standards as $standard) {
         if ($isInstalled === false) {
             $standardDir = $standardsDir . DIRECTORY_SEPARATOR . $standard . DIRECTORY_SEPARATOR . 'Tests' . DIRECTORY_SEPARATOR;
         } else {
             $standardDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . $standard . DIRECTORY_SEPARATOR . 'Tests' . DIRECTORY_SEPARATOR;
         }
         if (is_dir($standardDir) === false) {
             // No tests for this standard.
             continue;
         }
         // Locate the actual directory that contains the standard's tests.
         // This is individual to each standard as they could be symlinked in.
         $baseDir = dirname(dirname($standardDir));
         $di = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($standardDir));
         foreach ($di as $file) {
             // Skip hidden files.
             if (substr($file->getFilename(), 0, 1) === '.') {
                 continue;
             }
             // Tests must have the extension 'php'.
             $parts = explode('.', $file);
             $ext = array_pop($parts);
             if ($ext !== 'php') {
                 continue;
             }
             $filePath = $file->getPathname();
             $className = str_replace($baseDir . DIRECTORY_SEPARATOR, '', $filePath);
             $className = substr($className, 0, -4);
             $className = str_replace(DIRECTORY_SEPARATOR, '_', $className);
             include_once $filePath;
             $class = new $className('getErrorList');
             $suite->addTest($class);
         }
         //end foreach
     }
     //end foreach
     return $suite;
 }
 /**
  * Executes PHP code sniffer against PhingFile or a FileSet
  */
 public function main()
 {
     if (!class_exists('PHP_CodeSniffer')) {
         @(include_once 'PHP/CodeSniffer.php');
         if (!class_exists('PHP_CodeSniffer')) {
             throw new BuildException("This task requires the PHP_CodeSniffer package installed and available on the include path", $this->getLocation());
         }
     }
     /**
      * Determine PHP_CodeSniffer version number
      */
     if (!$this->skipversioncheck) {
         if (defined('PHP_CodeSniffer::VERSION')) {
             preg_match('/\\d\\.\\d\\.\\d/', PHP_CodeSniffer::VERSION, $version);
         } else {
             preg_match('/\\d\\.\\d\\.\\d/', shell_exec('phpcs --version'), $version);
         }
         if (version_compare($version[0], '1.2.2') < 0) {
             throw new BuildException('PhpCodeSnifferTask requires PHP_CodeSniffer version >= 1.2.2', $this->getLocation());
         }
     }
     if (!isset($this->file) and count($this->filesets) == 0) {
         throw new BuildException("Missing either a nested fileset or attribute 'file' set");
     }
     if (count($this->formatters) == 0) {
         // turn legacy format attribute into formatter
         $fmt = new PhpCodeSnifferTask_FormatterElement();
         $fmt->setType($this->format);
         $fmt->setUseFile(false);
         $this->formatters[] = $fmt;
     }
     $fileList = array();
     if (!isset($this->file)) {
         $project = $this->getProject();
         foreach ($this->filesets as $fs) {
             $ds = $fs->getDirectoryScanner($project);
             $files = $ds->getIncludedFiles();
             $dir = $fs->getDir($this->project)->getAbsolutePath();
             foreach ($files as $file) {
                 $fileList[] = $dir . DIRECTORY_SEPARATOR . $file;
             }
         }
     } else {
         $fileList[] = $this->file->getPath();
     }
     $cwd = getcwd();
     // Save command line arguments because it confuses PHPCS (version 1.3.0)
     $oldArgs = $_SERVER['argv'];
     $_SERVER['argv'] = array();
     $_SERVER['argc'] = 0;
     include_once 'phing/tasks/ext/phpcs/PhpCodeSnifferTask_Wrapper.php';
     $codeSniffer = new PhpCodeSnifferTask_Wrapper($this->verbosity, $this->tabWidth, $this->encoding);
     $codeSniffer->setAllowedFileExtensions($this->allowedFileExtensions);
     if ($this->allowedTypes) {
         PhpCodeSnifferTask_Wrapper::$allowedTypes = $this->allowedTypes;
     }
     if (is_array($this->ignorePatterns)) {
         $codeSniffer->setIgnorePatterns($this->ignorePatterns);
     }
     foreach ($this->configData as $configData) {
         $codeSniffer->setConfigData($configData->getName(), $configData->getValue(), true);
     }
     /*
      * Verifying if standard is installed only after setting config data.
      * Custom standard paths could be provided via installed_paths config parameter.
      */
     foreach ($this->standards as $standard) {
         if (PHP_CodeSniffer::isInstalledStandard($standard) === false) {
             // They didn't select a valid coding standard, so help them
             // out by letting them know which standards are installed.
             $installedStandards = PHP_CodeSniffer::getInstalledStandards();
             $numStandards = count($installedStandards);
             $errMsg = '';
             if ($numStandards === 0) {
                 $errMsg = 'No coding standards are installed.';
             } else {
                 $lastStandard = array_pop($installedStandards);
                 if ($numStandards === 1) {
                     $errMsg = 'The only coding standard installed is ' . $lastStandard;
                 } else {
                     $standardList = implode(', ', $installedStandards);
                     $standardList .= ' and ' . $lastStandard;
                     $errMsg = 'The installed coding standards are ' . $standardList;
                 }
             }
             throw new BuildException('ERROR: the "' . $standard . '" coding standard is not installed. ' . $errMsg, $this->getLocation());
         }
     }
     if (!$this->showWarnings) {
         $codeSniffer->cli->warningSeverity = 0;
     }
     // nasty integration hack
     $values = $codeSniffer->cli->getDefaults();
     $_SERVER['argv'] = array('t');
     $_SERVER['argc'] = 1;
     foreach ($this->formatters as $fe) {
         $output = $fe->getUseFile() ? $fe->getOutFile() : null;
         $_SERVER['argv'][] = '--report-' . $fe->getType() . '=' . $output;
         $_SERVER['argc']++;
     }
     $codeSniffer->process($fileList, $this->standards, $this->sniffs, $this->noSubdirectories);
     $_SERVER['argv'] = array();
     $_SERVER['argc'] = 0;
     $this->printErrorReport($codeSniffer);
     // generate the documentation
     if ($this->docGenerator !== '' && $this->docFile !== null) {
         ob_start();
         $codeSniffer->generateDocs($this->standards, $this->sniffs, $this->docGenerator);
         $output = ob_get_contents();
         ob_end_clean();
         // write to file
         $outputFile = $this->docFile->getPath();
         $check = file_put_contents($outputFile, $output);
         if (is_bool($check) && !$check) {
             throw new BuildException('Error writing doc to ' . $outputFile);
         }
     } elseif ($this->docGenerator !== '' && $this->docFile === null) {
         $codeSniffer->generateDocs($this->standards, $this->sniffs, $this->docGenerator);
     }
     if ($this->haltonerror && $codeSniffer->reporting->totalErrors > 0) {
         throw new BuildException('phpcodesniffer detected ' . $codeSniffer->reporting->totalErrors . ' error' . ($codeSniffer->reporting->totalErrors > 1 ? 's' : ''));
     }
     if ($this->haltonwarning && $codeSniffer->reporting->totalWarnings > 0) {
         throw new BuildException('phpcodesniffer detected ' . $codeSniffer->reporting->totalWarnings . ' warning' . ($codeSniffer->reporting->totalWarnings > 1 ? 's' : ''));
     }
     $_SERVER['argv'] = $oldArgs;
     $_SERVER['argc'] = count($oldArgs);
     chdir($cwd);
 }
Beispiel #6
0
 /**
  * Prints out a list of installed coding standards.
  *
  * @return void
  */
 public function printInstalledStandards()
 {
     $installedStandards = PHP_CodeSniffer::getInstalledStandards();
     $numStandards = count($installedStandards);
     if ($numStandards === 0) {
         echo 'No coding standards are installed.' . PHP_EOL;
     } else {
         $lastStandard = array_pop($installedStandards);
         if ($numStandards === 1) {
             echo "The only coding standard installed is {$lastStandard}" . PHP_EOL;
         } else {
             $standardList = implode(', ', $installedStandards);
             $standardList .= ' and ' . $lastStandard;
             echo 'The installed coding standards are ' . $standardList . PHP_EOL;
         }
     }
 }
 /**
  * Sets the coding standard to test for
  *
  * @param string $standard The coding standard
  *
  * @return void
  */
 public function setStandard($standard)
 {
     if (!class_exists('PHP_CodeSniffer')) {
         include_once 'PHP/CodeSniffer.php';
     }
     if (PHP_CodeSniffer::isInstalledStandard($standard) === false) {
         // They didn't select a valid coding standard, so help them
         // out by letting them know which standards are installed.
         $installedStandards = PHP_CodeSniffer::getInstalledStandards();
         $numStandards = count($installedStandards);
         $errMsg = '';
         if ($numStandards === 0) {
             $errMsg = 'No coding standards are installed.';
         } else {
             $lastStandard = array_pop($installedStandards);
             if ($numStandards === 1) {
                 $errMsg = 'The only coding standard installed is ' . $lastStandard;
             } else {
                 $standardList = implode(', ', $installedStandards);
                 $standardList .= ' and ' . $lastStandard;
                 $errMsg = 'The installed coding standards are ' . $standardList;
             }
         }
         throw new BuildException('ERROR: the "' . $standard . '" coding standard is not installed. ' . $errMsg, $this->getLocation());
     }
     $this->standard = $standard;
 }
Beispiel #8
0
 /**
  * Convert the passed standards into valid standards.
  *
  * Checks things like default values and case.
  *
  * @param array $standards The standards to validate.
  *
  * @return array
  */
 public function validateStandard($standards)
 {
     if ($standards === null) {
         // They did not supply a standard to use.
         // Try to get the default from the config system.
         $standard = PHP_CodeSniffer::getConfigData('default_standard');
         if ($standard === null) {
             // Product default standard.
             $standard = 'PEAR';
         }
         return array($standard);
     }
     $cleaned = array();
     // Check if the standard name is valid, or if the case is invalid.
     $installedStandards = PHP_CodeSniffer::getInstalledStandards();
     foreach ($standards as $standard) {
         foreach ($installedStandards as $validStandard) {
             if (strtolower($standard) === strtolower($validStandard)) {
                 $standard = $validStandard;
                 break;
             }
         }
         $cleaned[] = $standard;
     }
     return $cleaned;
 }
 /**
  * CodeSnifferShell::_standards()
  *
  * @return array
  */
 protected function _standards()
 {
     include_once 'PHP/CodeSniffer.php';
     return PHP_CodeSniffer::getInstalledStandards();
 }