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

PHPCSStandards / PHP_CodeSniffer / 13887072476

16 Mar 2025 08:25PM UTC coverage: 78.682%. Remained the same
13887072476

Pull #882

github

web-flow
Merge 9fd6cc2aa into 384f8e824
Pull Request #882: Ruleset: add tests to document trimming behaviour

24832 of 31560 relevant lines covered (78.68%)

66.33 hits per line

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

0.0
/src/Filters/ExactMatch.php
1
<?php
2
/**
3
 * An abstract filter class for checking files and folders against exact matches.
4
 *
5
 * Supports both allowed files and disallowed files.
6
 *
7
 * @author    Greg Sherwood <gsherwood@squiz.net>
8
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
9
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
10
 */
11

12
namespace PHP_CodeSniffer\Filters;
13

14
use PHP_CodeSniffer\Util\Common;
15

16
abstract class ExactMatch extends Filter
17
{
18

19
    /**
20
     * A list of files to exclude.
21
     *
22
     * @var array
23
     */
24
    private $disallowedFiles = null;
25

26
    /**
27
     * A list of files to include.
28
     *
29
     * If the allowed files list is empty, only files in the disallowed files list will be excluded.
30
     *
31
     * @var array
32
     */
33
    private $allowedFiles = null;
34

35

36
    /**
37
     * Check whether the current element of the iterator is acceptable.
38
     *
39
     * If a file is both disallowed and allowed, it will be deemed unacceptable.
40
     *
41
     * @return bool
42
     */
43
    public function accept()
×
44
    {
45
        if (parent::accept() === false) {
×
46
            return false;
×
47
        }
48

49
        if ($this->disallowedFiles === null) {
×
50
            $this->disallowedFiles = $this->getDisallowedFiles();
×
51

52
            // BC-layer.
53
            if ($this->disallowedFiles === null) {
×
54
                $this->disallowedFiles = $this->getBlacklist();
×
55
            }
56
        }
57

58
        if ($this->allowedFiles === null) {
×
59
            $this->allowedFiles = $this->getAllowedFiles();
×
60

61
            // BC-layer.
62
            if ($this->allowedFiles === null) {
×
63
                $this->allowedFiles = $this->getWhitelist();
×
64
            }
65
        }
66

67
        $filePath = Common::realpath($this->current());
×
68

69
        // If a file is both disallowed and allowed, the disallowed files list takes precedence.
70
        if (isset($this->disallowedFiles[$filePath]) === true) {
×
71
            return false;
×
72
        }
73

74
        if (empty($this->allowedFiles) === true && empty($this->disallowedFiles) === false) {
×
75
            // We are only checking the disallowed files list, so everything else should be allowed.
76
            return true;
×
77
        }
78

79
        return isset($this->allowedFiles[$filePath]);
×
80

81
    }//end accept()
82

83

84
    /**
85
     * Returns an iterator for the current entry.
86
     *
87
     * Ensures that the disallowed files list and the allowed files list are preserved so they don't have
88
     * to be generated each time.
89
     *
90
     * @return \RecursiveIterator
91
     */
92
    public function getChildren()
×
93
    {
94
        $children = parent::getChildren();
×
95
        $children->disallowedFiles = $this->disallowedFiles;
×
96
        $children->allowedFiles    = $this->allowedFiles;
×
97
        return $children;
×
98

99
    }//end getChildren()
100

101

102
    /**
103
     * Get a list of file paths to exclude.
104
     *
105
     * @deprecated 3.9.0 Implement the `getDisallowedFiles()` method instead.
106
     *                   The `getDisallowedFiles()` method will be made abstract and therefore required
107
     *                   in v4.0 and this method will be removed.
108
     *                   If both methods are implemented, the new `getDisallowedFiles()` method will take precedence.
109
     *
110
     * @return array
111
     */
112
    abstract protected function getBlacklist();
113

114

115
    /**
116
     * Get a list of file paths to include.
117
     *
118
     * @deprecated 3.9.0 Implement the `getAllowedFiles()` method instead.
119
     *                   The `getAllowedFiles()` method will be made abstract and therefore required
120
     *                   in v4.0 and this method will be removed.
121
     *                   If both methods are implemented, the new `getAllowedFiles()` method will take precedence.
122
     *
123
     * @return array
124
     */
125
    abstract protected function getWhitelist();
126

127

128
    /**
129
     * Get a list of file paths to exclude.
130
     *
131
     * @since 3.9.0 Replaces the deprecated `getBlacklist()` method.
132
     *
133
     * @return array|null
134
     */
135
    protected function getDisallowedFiles()
×
136
    {
137
        return null;
×
138

139
    }//end getDisallowedFiles()
140

141

142
    /**
143
     * Get a list of file paths to include.
144
     *
145
     * @since 3.9.0 Replaces the deprecated `getWhitelist()` method.
146
     *
147
     * @return array|null
148
     */
149
    protected function getAllowedFiles()
×
150
    {
151
        return null;
×
152

153
    }//end getAllowedFiles()
154

155

156
}//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