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

PHPCSStandards / PHP_CodeSniffer / 17663621563

12 Sep 2025 03:25AM UTC coverage: 78.786%. Remained the same
17663621563

push

github

web-flow
Merge pull request #1243 from PHPCSStandards/phpcs-4.x/feature/155-normalize-some-code-style-rules-5

CS: normalize code style rules [5]

294 of 308 new or added lines in 191 files covered. (95.45%)

2354 existing lines in 130 files now uncovered.

19732 of 25045 relevant lines covered (78.79%)

96.47 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

53

54
    /**
55
     * Processes this test, when one of its tokens is encountered.
56
     *
57
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
58
     * @param int                         $stackPtr  The position of the current token
59
     *                                               in the stack passed in $tokens.
60
     *
61
     * @return int
62
     */
UNCOV
63
    public function process(File $phpcsFile, int $stackPtr)
×
64
    {
UNCOV
65
        $path       = $phpcsFile->getFilename();
×
66
        $properties = $this->getProperties($path);
×
67
        if ($properties === null) {
×
68
            // Not under version control.
UNCOV
69
            return $phpcsFile->numTokens;
×
70
        }
71

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

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

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

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

115

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

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

135
        $foundPath = false;
×
UNCOV
136
        foreach ($paths as $path) {
×
137
            if (file_exists($path) === true) {
×
138
                $foundPath = true;
×
139

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

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

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

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

158
                    // Suck up extra newline after key data.
UNCOV
159
                    fgetc($handle);
×
160

161
                    // Read a value length line.
UNCOV
162
                    $buffer = trim(fgets($handle));
×
163

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

174
                    // Suck up extra newline after value data.
UNCOV
175
                    fgetc($handle);
×
176

177
                    $properties[$key] = $value;
×
178
                }//end while
179

UNCOV
180
                fclose($handle);
×
181
            }//end if
182
        }//end foreach
183

UNCOV
184
        if ($foundPath === false) {
×
UNCOV
185
            return null;
×
186
        }
187

UNCOV
188
        return $properties;
×
189
    }
190
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc