• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Standards/Generic/Sniffs/VersionControl/SubversionPropertiesSniff.php
1
<?php
2
/**
3
 * Tests that the correct Subversion properties are set.
4
 *
5
 * @author    Jack Bates <ms419@freezone.co.uk>
6
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 */
9

10
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\VersionControl;
11

12
use PHP_CodeSniffer\Exceptions\RuntimeException;
13
use PHP_CodeSniffer\Files\File;
14
use PHP_CodeSniffer\Sniffs\Sniff;
15

16
class SubversionPropertiesSniff implements Sniff
17
{
18

19
    /**
20
     * The Subversion properties that should be set.
21
     *
22
     * Key of array is the SVN property and the value is the
23
     * exact value the property should have or NULL if the
24
     * property should just be set but the value is not fixed.
25
     *
26
     * @var array<string, string>
27
     */
28
    protected const REQUIRED_PROPERTIES = [
29
        'svn:keywords'  => 'Author Id Revision',
30
        'svn:eol-style' => 'native',
31
    ];
32

33
    /**
34
     * The Subversion properties that should be set.
35
     *
36
     * @var array<string, string>
37
     *
38
     * @deprecated 4.0.0 Use the SubversionPropertiesSniff::REQUIRED_PROPERTIES constant instead.
39
     */
40
    protected $properties = self::REQUIRED_PROPERTIES;
41

42

43
    /**
44
     * Returns an array of tokens this test wants to listen for.
45
     *
46
     * @return array<int|string>
47
     */
48
    public function register()
×
49
    {
50
        return [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\Files\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 int
63
     */
64
    public function process(File $phpcsFile, $stackPtr)
×
65
    {
66
        $path       = $phpcsFile->getFilename();
×
67
        $properties = $this->getProperties($path);
×
68
        if ($properties === null) {
×
69
            // Not under version control.
70
            return $phpcsFile->numTokens;
×
71
        }
72

73
        $allProperties = ($properties + static::REQUIRED_PROPERTIES);
×
74
        foreach ($allProperties as $key => $value) {
×
75
            if (isset($properties[$key]) === true
×
76
                && isset(static::REQUIRED_PROPERTIES[$key]) === false
×
77
            ) {
78
                $error = 'Unexpected Subversion property "%s" = "%s"';
×
79
                $data  = [
80
                    $key,
×
81
                    $properties[$key],
×
82
                ];
83
                $phpcsFile->addError($error, $stackPtr, 'Unexpected', $data);
×
84
                continue;
×
85
            }
86

87
            if (isset($properties[$key]) === false
×
88
                && isset(static::REQUIRED_PROPERTIES[$key]) === true
×
89
            ) {
90
                $error = 'Missing Subversion property "%s" = "%s"';
×
91
                $data  = [
92
                    $key,
×
93
                    static::REQUIRED_PROPERTIES[$key],
×
94
                ];
95
                $phpcsFile->addError($error, $stackPtr, 'Missing', $data);
×
96
                continue;
×
97
            }
98

99
            if ($properties[$key] !== null
×
100
                && $properties[$key] !== static::REQUIRED_PROPERTIES[$key]
×
101
            ) {
102
                $error = 'Subversion property "%s" = "%s" does not match "%s"';
×
103
                $data  = [
104
                    $key,
×
105
                    $properties[$key],
×
106
                    static::REQUIRED_PROPERTIES[$key],
×
107
                ];
108
                $phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
×
109
            }
110
        }//end foreach
111

112
        // Ignore the rest of the file.
113
        return $phpcsFile->numTokens;
×
114

115
    }//end process()
116

117

118
    /**
119
     * Returns the Subversion properties which are actually set on a path.
120
     *
121
     * Returns NULL if the file is not under version control.
122
     *
123
     * @param string $path The path to return Subversion properties on.
124
     *
125
     * @return array|null
126
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If Subversion properties file could
127
     *                                                      not be opened.
128
     */
129
    protected function getProperties($path)
×
130
    {
131
        $properties = [];
×
132

133
        $paths   = [];
×
134
        $paths[] = dirname($path).'/.svn/props/'.basename($path).'.svn-work';
×
135
        $paths[] = dirname($path).'/.svn/prop-base/'.basename($path).'.svn-base';
×
136

137
        $foundPath = false;
×
138
        foreach ($paths as $path) {
×
139
            if (file_exists($path) === true) {
×
140
                $foundPath = true;
×
141

142
                $handle = fopen($path, 'r');
×
143
                if ($handle === false) {
×
144
                    $error = 'Error opening file; could not get Subversion properties';
×
145
                    throw new RuntimeException($error);
×
146
                }
147

148
                while (feof($handle) === false) {
×
149
                    // Read a key length line. Might be END, though.
150
                    $buffer = trim(fgets($handle));
×
151

152
                    // Check for the end of the hash.
153
                    if ($buffer === 'END') {
×
154
                        break;
×
155
                    }
156

157
                    // Now read that much into a buffer.
158
                    $key = fread($handle, substr($buffer, 2));
×
159

160
                    // Suck up extra newline after key data.
161
                    fgetc($handle);
×
162

163
                    // Read a value length line.
164
                    $buffer = trim(fgets($handle));
×
165

166
                    // Now read that much into a buffer.
167
                    $length = substr($buffer, 2);
×
168
                    if ($length === '0') {
×
169
                        // Length of value is ZERO characters, so
170
                        // value is actually empty.
171
                        $value = '';
×
172
                    } else {
173
                        $value = fread($handle, $length);
×
174
                    }
175

176
                    // Suck up extra newline after value data.
177
                    fgetc($handle);
×
178

179
                    $properties[$key] = $value;
×
180
                }//end while
181

182
                fclose($handle);
×
183
            }//end if
184
        }//end foreach
185

186
        if ($foundPath === false) {
×
187
            return null;
×
188
        }
189

190
        return $properties;
×
191

192
    }//end getProperties()
193

194

195
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc