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

Documentation generated on Mon, 11 Mar 2019 15:27:47 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.