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.         // Make sure this is the first PHP open tag so we don't process the
  60.         // same file twice.
  61.         $prevOpenTag $phpcsFile->findPrevious(T_OPEN_TAG($stackPtr - 1));
  62.         if ($prevOpenTag !== false{
  63.             return;
  64.         }
  65.  
  66.         $path       $phpcsFile->getFileName();
  67.         $properties $this->getProperties($path);
  68.         if ($properties === null{
  69.             // Not under version control.
  70.             return;
  71.         }
  72.  
  73.         $allProperties ($properties $this->properties);
  74.         foreach ($allProperties as $key => $value{
  75.             if (isset($properties[$key]=== true
  76.                 && isset($this->properties[$key]=== false
  77.             {
  78.                 $error 'Unexpected Subversion property "%s" = "%s"';
  79.                 $data  = array(
  80.                           $key,
  81.                           $properties[$key],
  82.                          );
  83.                 $phpcsFile->addError($error$stackPtr'Unexpected'$data);
  84.                 continue;
  85.             }
  86.  
  87.             if (isset($properties[$key]=== false
  88.                 && isset($this->properties[$key]=== true
  89.             {
  90.                 $error 'Missing Subversion property "%s" = "%s"';
  91.                 $data  = array(
  92.                           $key,
  93.                           $this->properties[$key],
  94.                          );
  95.                 $phpcsFile->addError($error$stackPtr'Missing'$data);
  96.                 continue;
  97.             }
  98.  
  99.             if ($properties[$key!== null
  100.                 && $properties[$key!== $this->properties[$key]
  101.             {
  102.                 $error 'Subversion property "%s" = "%s" does not match "%s"';
  103.                 $data  = array(
  104.                           $key,
  105.                           $properties[$key],
  106.                           $this->properties[$key],
  107.                          );
  108.                 $phpcsFile->addError($error$stackPtr'NoMatch'$data);
  109.             }
  110.         }//end foreach
  111.  
  112.     }//end process()
  113.  
  114.  
  115.     /**
  116.      * Returns the Subversion properties which are actually set on a path.
  117.      *
  118.      * Returns NULL if the file is not under version control.
  119.      *
  120.      * @param string $path The path to return Subversion properties on.
  121.      *
  122.      * @return array 
  123.      * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If Subversion properties file could
  124.      *                                                       not be opened.
  125.      */
  126.     protected function getProperties($path)
  127.     {
  128.         $properties = array();
  129.  
  130.         $paths   = array();
  131.         $paths[dirname($path).'/.svn/props/'.basename($path).'.svn-work';
  132.         $paths[dirname($path).'/.svn/prop-base/'.basename($path).'.svn-base';
  133.  
  134.         $foundPath = false;
  135.         foreach ($paths as $path{
  136.             if (file_exists($path=== true{
  137.                 $foundPath = true;
  138.  
  139.                 $handle fopen($path'r');
  140.                 if ($handle === false{
  141.                     $error 'Error opening file; could not get Subversion properties';
  142.                     throw new RuntimeException($error);
  143.                 }
  144.  
  145.                 while (feof($handle=== false{
  146.                     // Read a key length line. Might be END, though.
  147.                     $buffer trim(fgets($handle));
  148.  
  149.                     // Check for the end of the hash.
  150.                     if ($buffer === 'END'{
  151.                         break;
  152.                     }
  153.  
  154.                     // Now read that much into a buffer.
  155.                     $key fread($handlesubstr($buffer2));
  156.  
  157.                     // Suck up extra newline after key data.
  158.                     fgetc($handle);
  159.  
  160.                     // Read a value length line.
  161.                     $buffer trim(fgets($handle));
  162.  
  163.                     // Now read that much into a buffer.
  164.                     $length substr($buffer2);
  165.                     if ($length === '0'{
  166.                         // Length of value is ZERO characters, so
  167.                         // value is actually empty.
  168.                         $value '';
  169.                     else {
  170.                         $value fread($handle$length);
  171.                     }
  172.  
  173.                     // Suck up extra newline after value data.
  174.                     fgetc($handle);
  175.  
  176.                     $properties[$key$value;
  177.                 }//end while
  178.  
  179.                 fclose($handle);
  180.             }//end if
  181.         }//end foreach
  182.  
  183.         if ($foundPath === false{
  184.             return null;
  185.         }
  186.  
  187.         return $properties;
  188.  
  189.     }//end getProperties()
  190.  
  191.  
  192. }//end class

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