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

Source for file SubversionPropertiesSniff.php

Documentation is available at SubversionPropertiesSniff.php

  1. <?php
  2. /**
  3.  * Tests that the correct Subversion properties are set.
  4.  *
  5.  * @author    Jack Bates <ms419@freezone.co.uk>
  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\Standards\Generic\Sniffs\VersionControl;
  11.  
  12. use PHP_CodeSniffer\Exceptions\RuntimeException;
  13. use PHP_CodeSniffer\Sniffs\Sniff;
  14. use PHP_CodeSniffer\Files\File;
  15.  
  16. class SubversionPropertiesSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * The Subversion properties that should be set.
  21.      *
  22.      * Key of array is the SVN property and the value is the
  23.      * exact value the property should have or NULL if the
  24.      * property should just be set but the value is not fixed.
  25.      *
  26.      * @var array 
  27.      */
  28.     protected $properties = array(
  29.                              'svn:keywords'  => 'Author Id Revision',
  30.                              'svn:eol-style' => 'native',
  31.                             );
  32.  
  33.  
  34.     /**
  35.      * Returns an array of tokens this test wants to listen for.
  36.      *
  37.      * @return array 
  38.      */
  39.     public function register()
  40.     {
  41.         return array(T_OPEN_TAG);
  42.  
  43.     }//end register()
  44.  
  45.  
  46.     /**
  47.      * Processes this test, when one of its tokens is encountered.
  48.      *
  49.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  50.      * @param int                         $stackPtr  The position of the current token
  51.      *                                                in the stack passed in $tokens.
  52.      *
  53.      * @return void 
  54.      */
  55.     public function process(File $phpcsFile$stackPtr)
  56.     {
  57.         $tokens $phpcsFile->getTokens();
  58.  
  59.         $path       $phpcsFile->getFileName();
  60.         $properties $this->getProperties($path);
  61.         if ($properties === null{
  62.             // Not under version control.
  63.             return ($phpcsFile->numTokens + 1);
  64.         }
  65.  
  66.         $allProperties ($properties $this->properties);
  67.         foreach ($allProperties as $key => $value{
  68.             if (isset($properties[$key]=== true
  69.                 && isset($this->properties[$key]=== false
  70.             {
  71.                 $error 'Unexpected Subversion property "%s" = "%s"';
  72.                 $data  = array(
  73.                           $key,
  74.                           $properties[$key],
  75.                          );
  76.                 $phpcsFile->addError($error$stackPtr'Unexpected'$data);
  77.                 continue;
  78.             }
  79.  
  80.             if (isset($properties[$key]=== false
  81.                 && isset($this->properties[$key]=== true
  82.             {
  83.                 $error 'Missing Subversion property "%s" = "%s"';
  84.                 $data  = array(
  85.                           $key,
  86.                           $this->properties[$key],
  87.                          );
  88.                 $phpcsFile->addError($error$stackPtr'Missing'$data);
  89.                 continue;
  90.             }
  91.  
  92.             if ($properties[$key!== null
  93.                 && $properties[$key!== $this->properties[$key]
  94.             {
  95.                 $error 'Subversion property "%s" = "%s" does not match "%s"';
  96.                 $data  = array(
  97.                           $key,
  98.                           $properties[$key],
  99.                           $this->properties[$key],
  100.                          );
  101.                 $phpcsFile->addError($error$stackPtr'NoMatch'$data);
  102.             }
  103.         }//end foreach
  104.  
  105.         // Ignore the rest of the file.
  106.         return ($phpcsFile->numTokens + 1);
  107.  
  108.     }//end process()
  109.  
  110.  
  111.     /**
  112.      * Returns the Subversion properties which are actually set on a path.
  113.      *
  114.      * Returns NULL if the file is not under version control.
  115.      *
  116.      * @param string $path The path to return Subversion properties on.
  117.      *
  118.      * @return array 
  119.      * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If Subversion properties file could
  120.      *                                                       not be opened.
  121.      */
  122.     protected function getProperties($path)
  123.     {
  124.         $properties = array();
  125.  
  126.         $paths   = array();
  127.         $paths[dirname($path).'/.svn/props/'.basename($path).'.svn-work';
  128.         $paths[dirname($path).'/.svn/prop-base/'.basename($path).'.svn-base';
  129.  
  130.         $foundPath = false;
  131.         foreach ($paths as $path{
  132.             if (file_exists($path=== true{
  133.                 $foundPath = true;
  134.  
  135.                 $handle fopen($path'r');
  136.                 if ($handle === false{
  137.                     $error 'Error opening file; could not get Subversion properties';
  138.                     throw new RuntimeException($error);
  139.                 }
  140.  
  141.                 while (feof($handle=== false{
  142.                     // Read a key length line. Might be END, though.
  143.                     $buffer trim(fgets($handle));
  144.  
  145.                     // Check for the end of the hash.
  146.                     if ($buffer === 'END'{
  147.                         break;
  148.                     }
  149.  
  150.                     // Now read that much into a buffer.
  151.                     $key fread($handlesubstr($buffer2));
  152.  
  153.                     // Suck up extra newline after key data.
  154.                     fgetc($handle);
  155.  
  156.                     // Read a value length line.
  157.                     $buffer trim(fgets($handle));
  158.  
  159.                     // Now read that much into a buffer.
  160.                     $length substr($buffer2);
  161.                     if ($length === '0'{
  162.                         // Length of value is ZERO characters, so
  163.                         // value is actually empty.
  164.                         $value '';
  165.                     else {
  166.                         $value fread($handle$length);
  167.                     }
  168.  
  169.                     // Suck up extra newline after value data.
  170.                     fgetc($handle);
  171.  
  172.                     $properties[$key$value;
  173.                 }//end while
  174.  
  175.                 fclose($handle);
  176.             }//end if
  177.         }//end foreach
  178.  
  179.         if ($foundPath === false{
  180.             return null;
  181.         }
  182.  
  183.         return $properties;
  184.  
  185.     }//end getProperties()
  186.  
  187.  
  188. }//end class

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