PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file Standards.php

Documentation is available at Standards.php

  1. <?php
  2. /**
  3.  * Functions for helping process standards.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  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 paths where standards are installed.
  20.      *
  21.      * @return array 
  22.      */
  23.     public static function getInstalledStandardPaths()
  24.     {
  25.         $ds = DIRECTORY_SEPARATOR;
  26.  
  27.         $installedPaths = array(dirname(dirname(__DIR__)).$ds.'src'.$ds.'Standards');
  28.         $configPaths    = Config::getConfigData('installed_paths');
  29.         if ($configPaths !== null{
  30.             $installedPaths array_merge($installedPathsexplode(','$configPaths));
  31.         }
  32.  
  33.         $resolvedInstalledPaths = array();
  34.         foreach ($installedPaths as $installedPath{
  35.             if (substr($installedPath01=== '.'{
  36.                 $installedPath = Common::realPath(__DIR__.$ds.'..'.$ds.'..'.$ds.$installedPath);
  37.             }
  38.  
  39.             $resolvedInstalledPaths[$installedPath;
  40.         }
  41.  
  42.         return $resolvedInstalledPaths;
  43.  
  44.     }//end getInstalledStandardPaths()
  45.  
  46.  
  47.     /**
  48.      * Get a list of all coding standards installed.
  49.      *
  50.      * Coding standards are directories located in the
  51.      * CodeSniffer/Standards directory. Valid coding standards
  52.      * include a Sniffs subdirectory.
  53.      *
  54.      * @param boolean $includeGeneric If true, the special "Generic"
  55.      *                                 coding standard will be included
  56.      *                                 if installed.
  57.      * @param string  $standardsDir   A specific directory to look for standards
  58.      *                                 in. If not specified, PHP_CodeSniffer will
  59.      *                                 look in its default locations.
  60.      *
  61.      * @return array 
  62.      * @see    isInstalledStandard()
  63.      */
  64.     public static function getInstalledStandards(
  65.         $includeGeneric=false,
  66.         $standardsDir=''
  67.     {
  68.         $installedStandards = array();
  69.  
  70.         if ($standardsDir === ''{
  71.             $installedPaths = self::getInstalledStandardPaths();
  72.         else {
  73.             $installedPaths = array($standardsDir);
  74.         }
  75.  
  76.         foreach ($installedPaths as $standardsDir{
  77.             // Check if the installed dir is actually a standard itself.
  78.             $csFile $standardsDir.'/ruleset.xml';
  79.             if (is_file($csFile=== true{
  80.                 $installedStandards[basename($standardsDir);
  81.                 continue;
  82.             }
  83.  
  84.             $di = new \DirectoryIterator($standardsDir);
  85.             foreach ($di as $file{
  86.                 if ($file->isDir(=== true && $file->isDot(=== false{
  87.                     $filename $file->getFilename();
  88.  
  89.                     // Ignore the special "Generic" standard.
  90.                     if ($includeGeneric === false && $filename === 'Generic'{
  91.                         continue;
  92.                     }
  93.  
  94.                     // Valid coding standard dirs include a ruleset.
  95.                     $csFile $file->getPathname().'/ruleset.xml';
  96.                     if (is_file($csFile=== true{
  97.                         $installedStandards[$filename;
  98.                     }
  99.                 }
  100.             }
  101.         }//end foreach
  102.  
  103.         return $installedStandards;
  104.  
  105.     }//end getInstalledStandards()
  106.  
  107.  
  108.     /**
  109.      * Determine if a standard is installed.
  110.      *
  111.      * Coding standards are directories located in the
  112.      * CodeSniffer/Standards directory. Valid coding standards
  113.      * include a ruleset.xml file.
  114.      *
  115.      * @param string $standard The name of the coding standard.
  116.      *
  117.      * @return boolean 
  118.      * @see    getInstalledStandards()
  119.      */
  120.     public static function isInstalledStandard($standard)
  121.     {
  122.         $path = self::getInstalledStandardPath($standard);
  123.         if ($path !== null && strpos($path'ruleset.xml'!== false{
  124.             return true;
  125.         else {
  126.             // This could be a custom standard, installed outside our
  127.             // standards directory.
  128.             $standard = Common::realPath($standard);
  129.  
  130.             // Might be an actual ruleset file itUtil.
  131.             // If it has an XML extension, let's at least try it.
  132.             if (is_file($standard=== true
  133.                 && (substr(strtolower($standard)-4=== '.xml'
  134.                 || substr(strtolower($standard)-9=== '.xml.dist')
  135.             {
  136.                 return true;
  137.             }
  138.  
  139.             // If it is a directory with a ruleset.xml file in it,
  140.             // it is a standard.
  141.             $ruleset rtrim($standard' /\\').DIRECTORY_SEPARATOR.'ruleset.xml';
  142.             if (is_file($ruleset=== true{
  143.                 return true;
  144.             }
  145.         }//end if
  146.  
  147.         return false;
  148.  
  149.     }//end isInstalledStandard()
  150.  
  151.  
  152.     /**
  153.      * Return the path of an installed coding standard.
  154.      *
  155.      * Coding standards are directories located in the
  156.      * CodeSniffer/Standards directory. Valid coding standards
  157.      * include a ruleset.xml file.
  158.      *
  159.      * @param string $standard The name of the coding standard.
  160.      *
  161.      * @return string|null
  162.      */
  163.     public static function getInstalledStandardPath($standard)
  164.     {
  165.         $installedPaths = self::getInstalledStandardPaths();
  166.         foreach ($installedPaths as $installedPath{
  167.             if (basename($installedPath=== $standard{
  168.                 $standardPath $installedPath;
  169.             else {
  170.                 $standardPath $installedPath.DIRECTORY_SEPARATOR.$standard;
  171.             }
  172.  
  173.             $path = Common::realpath($standardPath.DIRECTORY_SEPARATOR.'ruleset.xml');
  174.  
  175.             if (is_file($path=== true{
  176.                 return $path;
  177.             else if (Common::isPharFile($standardPath=== true{
  178.                 $path = Common::realpath($standardPath);
  179.                 if ($path !== false{
  180.                     return $path;
  181.                 }
  182.             }
  183.         }
  184.  
  185.         return null;
  186.  
  187.     }//end getInstalledStandardPath()
  188.  
  189.  
  190.     /**
  191.      * Prints out a list of installed coding standards.
  192.      *
  193.      * @return void 
  194.      */
  195.     public static function printInstalledStandards()
  196.     {
  197.         $installedStandards = self::getInstalledStandards();
  198.         $numStandards       count($installedStandards);
  199.  
  200.         if ($numStandards === 0{
  201.             echo 'No coding standards are installed.'.PHP_EOL;
  202.         else {
  203.             $lastStandard array_pop($installedStandards);
  204.             if ($numStandards === 1{
  205.                 echo "The only coding standard installed is $lastStandard".PHP_EOL;
  206.             else {
  207.                 $standardList  implode(', '$installedStandards);
  208.                 $standardList .= ' and '.$lastStandard;
  209.                 echo 'The installed coding standards are '.$standardList.PHP_EOL;
  210.             }
  211.         }
  212.  
  213.     }//end printInstalledStandards()
  214.  
  215.  
  216. }//end class

Documentation generated on Mon, 11 Mar 2019 14:23:26 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.