Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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