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