3
* Generic_Sniffs_VersionControl_SubversionPropertiesSniff.
8
* @package PHP_CodeSniffer
9
* @author Jack Bates <ms419@freezone.co.uk>
10
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
11
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12
* @link http://pear.php.net/package/PHP_CodeSniffer
16
* Generic_Sniffs_VersionControl_SubversionPropertiesSniff.
18
* Tests that the correct Subversion properties are set.
21
* @package PHP_CodeSniffer
22
* @author Jack Bates <ms419@freezone.co.uk>
23
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
24
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
25
* @version Release: 1.3.4
26
* @link http://pear.php.net/package/PHP_CodeSniffer
28
class Generic_Sniffs_VersionControl_SubversionPropertiesSniff implements PHP_CodeSniffer_Sniff
32
* The Subversion properties that should be set.
34
* Key of array is the SVN property and the value is the
35
* exact value the property should have or NULL if the
36
* property should just be set but the value is not fixed.
40
protected $properties = array(
41
'svn:keywords' => 'Author Id Revision',
42
'svn:eol-style' => 'native',
47
* Returns an array of tokens this test wants to listen for.
51
public function register()
53
return array(T_OPEN_TAG);
59
* Processes this test, when one of its tokens is encountered.
61
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
62
* @param int $stackPtr The position of the current token
63
* in the stack passed in $tokens.
67
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
69
$tokens = $phpcsFile->getTokens();
71
// Make sure this is the first PHP open tag so we don't process the
73
$prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1));
74
if ($prevOpenTag !== false) {
78
$path = $phpcsFile->getFileName();
79
$properties = $this->getProperties($path);
80
if ($properties === null) {
81
// Not under version control.
85
$properties += $this->properties;
86
foreach ($properties as $key => $value) {
87
if (isset($properties[$key]) === true
88
&& isset($this->properties[$key]) === false
90
$error = 'Unexpected Subversion property "%s" = "%s"';
95
$phpcsFile->addError($error, $stackPtr, 'Unexpected', $data);
99
if (isset($properties[$key]) === false
100
&& isset($this->properties[$key]) === true
102
$error = 'Missing Subversion property "%s" = "%s"';
105
$this->properties[$key],
107
$phpcsFile->addError($error, $stackPtr, 'Missing', $data);
111
if ($properties[$key] !== null
112
&& $properties[$key] !== $this->properties[$key]
114
$error = 'Subversion property "%s" = "%s" does not match "%s"';
118
$this->properties[$key],
120
$phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
128
* Returns the Subversion properties which are actually set on a path.
130
* Returns NULL if the file is not under version control.
132
* @param string $path The path to return Subversion properties on.
135
* @throws PHP_CodeSniffer_Exception If Subversion properties file could
138
protected function getProperties($path)
140
$properties = array();
143
$paths[] = dirname($path).'/.svn/props/'.basename($path).'.svn-work';
144
$paths[] = dirname($path).'/.svn/prop-base/'.basename($path).'.svn-base';
147
foreach ($paths as $path) {
148
if (file_exists($path) === true) {
151
$handle = fopen($path, 'r');
152
if ($handle === false) {
153
$error = 'Error opening file; could not get Subversion properties';
154
throw new PHP_CodeSniffer_Exception($error);
157
while (feof($handle) === false) {
158
// Read a key length line. Might be END, though.
159
$buffer = trim(fgets($handle));
161
// Check for the end of the hash.
162
if ($buffer === 'END') {
166
// Now read that much into a buffer.
167
$key = fread($handle, substr($buffer, 2));
169
// Suck up extra newline after key data.
172
// Read a value length line.
173
$buffer = trim(fgets($handle));
175
// Now read that much into a buffer.
176
$length = substr($buffer, 2);
177
if ($length === '0') {
178
// Length of value is ZERO characters, so
179
// value is actually empty.
182
$value = fread($handle, $length);
185
// Suck up extra newline after value data.
188
$properties[$key] = $value;
195
if ($foundPath === false) {
201
}//end getProperties()