Subversion Repositories Applications.framework

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 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,v 1.1 2009/01/05 01:07:47 squiz Exp $
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.0RC1
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(
51
                T_OPEN_TAG,
52
               );
53
 
54
    }//end register()
55
 
56
 
57
    /**
58
     * Processes this test, when one of its tokens is encountered.
59
     *
60
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
61
     * @param int                  $stackPtr  The position of the current token
62
     *                                        in the stack passed in $tokens.
63
     *
64
     * @return void
65
     */
66
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
67
    {
68
        $tokens = $phpcsFile->getTokens();
69
 
70
        // Make sure this is the first PHP open tag so we don't process the
71
        // same file twice.
72
        $prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1));
73
        if ($prevOpenTag !== false) {
74
            return;
75
        }
76
 
77
        $path       = $phpcsFile->getFileName();
78
        $properties = $this->properties($path);
79
 
80
        foreach (($properties + $this->properties) as $key => $value) {
81
            if (isset($properties[$key]) === true
82
                && isset($this->properties[$key]) === false
83
            ) {
84
                $error = 'Unexpected Subversion property "'.$key.'" = "'.$properties[$key].'"';
85
                $phpcsFile->addError($error, $stackPtr);
86
                continue;
87
            }
88
 
89
            if (isset($properties[$key]) === false
90
                && isset($this->properties[$key]) === true
91
            ) {
92
                $error = 'Missing Subversion property "'.$key.'" = "'.$this->properties[$key].'"';
93
                $phpcsFile->addError($error, $stackPtr);
94
                continue;
95
            }
96
 
97
            if ($properties[$key] !== $this->properties[$key]) {
98
                $error = 'Subversion property "'.$key.'" = "'.$properties[$key].'" does not match "'.$this->properties[$key].'"';
99
                $phpcsFile->addError($error, $stackPtr);
100
            }
101
        }
102
 
103
    }//end process()
104
 
105
 
106
    /**
107
     * Returns the Subversion properties which are actually set on a path.
108
     *
109
     * @param string $path The path to return Subversion properties on.
110
     *
111
     * @return array
112
     * @throws PHP_CodeSniffer_Exception If Subversion properties file could
113
     *                                   not be opened.
114
     */
115
    protected function properties($path)
116
    {
117
        $properties = array();
118
 
119
        $paths   = array();
120
        $paths[] = dirname($path).'/.svn/props/'.basename($path).'.svn-work';
121
        $paths[] = dirname($path).'/.svn/prop-base/'.basename($path).'.svn-base';
122
 
123
        foreach ($paths as $path) {
124
            if (true === file_exists($path)) {
125
                if (false === $handle = fopen($path, 'r')) {
126
                    $error = 'Error opening file; could not get Subversion properties';
127
                    throw new PHP_CodeSniffer_Exception($error);
128
                }
129
 
130
                while (!feof($handle)) {
131
 
132
                    // Read a key length line. Might be END, though.
133
                    $buffer = fgets($handle);
134
 
135
                    // Check for the end of the hash.
136
                    if ("END\n" === $buffer) {
137
                        break;
138
                    }
139
 
140
                    // Now read that much into a buffer.
141
                    $key = fread($handle, substr($buffer, 2));
142
 
143
                    // Suck up extra newline after key data.
144
                    fgetc($handle);
145
 
146
                    // Read a value length line.
147
                    $buffer = fgets($handle);
148
 
149
                    // Now read that much into a buffer.
150
                    $value = fread($handle, substr($buffer, 2));
151
 
152
                    // Suck up extra newline after value data.
153
                    fgetc($handle);
154
 
155
                    $properties[$key] = $value;
156
                }//end while
157
 
158
                fclose($handle);
159
            }//end if
160
        }//end foreach
161
 
162
        return $properties;
163
 
164
    }//end properties()
165
 
166
 
167
}//end class
168
 
169
?>