-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathSubversionPropertiesSniff.php
195 lines (161 loc) · 6.11 KB
/
SubversionPropertiesSniff.php
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
<?php
/**
* Tests that the correct Subversion properties are set.
*
* @author Jack Bates <ms419@freezone.co.uk>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\VersionControl;
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
class SubversionPropertiesSniff implements 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<string, string>
*/
protected const REQUIRED_PROPERTIES = [
'svn:keywords' => 'Author Id Revision',
'svn:eol-style' => 'native',
];
/**
* The Subversion properties that should be set.
*
* @var array<string, string>
*
* @deprecated 4.0.0 Use the SubversionPropertiesSniff::REQUIRED_PROPERTIES constant instead.
*/
protected $properties = self::REQUIRED_PROPERTIES;
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [T_OPEN_TAG];
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return int
*/
public function process(File $phpcsFile, $stackPtr)
{
$path = $phpcsFile->getFilename();
$properties = $this->getProperties($path);
if ($properties === null) {
// Not under version control.
return $phpcsFile->numTokens;
}
$allProperties = ($properties + static::REQUIRED_PROPERTIES);
foreach ($allProperties as $key => $value) {
if (isset($properties[$key]) === true
&& isset(static::REQUIRED_PROPERTIES[$key]) === false
) {
$error = 'Unexpected Subversion property "%s" = "%s"';
$data = [
$key,
$properties[$key],
];
$phpcsFile->addError($error, $stackPtr, 'Unexpected', $data);
continue;
}
if (isset($properties[$key]) === false
&& isset(static::REQUIRED_PROPERTIES[$key]) === true
) {
$error = 'Missing Subversion property "%s" = "%s"';
$data = [
$key,
static::REQUIRED_PROPERTIES[$key],
];
$phpcsFile->addError($error, $stackPtr, 'Missing', $data);
continue;
}
if ($properties[$key] !== null
&& $properties[$key] !== static::REQUIRED_PROPERTIES[$key]
) {
$error = 'Subversion property "%s" = "%s" does not match "%s"';
$data = [
$key,
$properties[$key],
static::REQUIRED_PROPERTIES[$key],
];
$phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
}
}//end foreach
// Ignore the rest of the file.
return $phpcsFile->numTokens;
}//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|null
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If Subversion properties file could
* not be opened.
*/
protected function getProperties($path)
{
$properties = [];
$paths = [];
$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 RuntimeException($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