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

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