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

PHPCSStandards / PHP_CodeSniffer / 15932682552

27 Jun 2025 05:51PM UTC coverage: 78.537% (+0.1%) from 78.441%
15932682552

Pull #1150

github

web-flow
Merge d9581377b into 368817d89
Pull Request #1150: Files/FileList: adding the same file twice should not increment `FileList::$numFiles`

4 of 4 new or added lines in 1 file covered. (100.0%)

16 existing lines in 1 file now uncovered.

25230 of 32125 relevant lines covered (78.54%)

69.43 hits per line

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

61.29
/src/Files/FileList.php
1
<?php
2
/**
3
 * Represents a list of files on the file system that are to be checked during the run.
4
 *
5
 * File objects are created as needed rather than all at once.
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\Files;
13

14
use Countable;
15
use FilesystemIterator;
16
use Iterator;
17
use PHP_CodeSniffer\Autoload;
18
use PHP_CodeSniffer\Config;
19
use PHP_CodeSniffer\Exceptions\DeepExitException;
20
use PHP_CodeSniffer\Ruleset;
21
use PHP_CodeSniffer\Util\Common;
22
use RecursiveArrayIterator;
23
use RecursiveDirectoryIterator;
24
use RecursiveIteratorIterator;
25
use ReturnTypeWillChange;
26

27
class FileList implements Iterator, Countable
28
{
29

30
    /**
31
     * A list of file paths that are included in the list.
32
     *
33
     * @var array
34
     */
35
    private $files = [];
36

37
    /**
38
     * The number of files in the list.
39
     *
40
     * @var integer
41
     */
42
    private $numFiles = 0;
43

44
    /**
45
     * The config data for the run.
46
     *
47
     * @var \PHP_CodeSniffer\Config
48
     */
49
    public $config = null;
50

51
    /**
52
     * The ruleset used for the run.
53
     *
54
     * @var \PHP_CodeSniffer\Ruleset
55
     */
56
    public $ruleset = null;
57

58
    /**
59
     * An array of patterns to use for skipping files.
60
     *
61
     * @var array
62
     */
63
    protected $ignorePatterns = [];
64

65

66
    /**
67
     * Constructs a file list and loads in an array of file paths to process.
68
     *
69
     * @param \PHP_CodeSniffer\Config  $config  The config data for the run.
70
     * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run.
71
     *
72
     * @return void
73
     */
74
    public function __construct(Config $config, Ruleset $ruleset)
15✔
75
    {
76
        $this->ruleset = $ruleset;
15✔
77
        $this->config  = $config;
15✔
78

79
        $paths = $config->files;
15✔
80
        foreach ($paths as $path) {
15✔
81
            $isPharFile = Common::isPharFile($path);
12✔
82
            if (is_dir($path) === true || $isPharFile === true) {
12✔
83
                if ($isPharFile === true) {
6✔
84
                    $path = 'phar://'.$path;
×
85
                }
86

87
                $filterClass = $this->getFilterClass();
6✔
88

89
                $di       = new RecursiveDirectoryIterator($path, (RecursiveDirectoryIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS));
6✔
90
                $filter   = new $filterClass($di, $path, $config, $ruleset);
6✔
91
                $iterator = new RecursiveIteratorIterator($filter);
6✔
92

93
                foreach ($iterator as $file) {
6✔
94
                    $pathname = $file->getPathname();
6✔
95
                    $this->files[$pathname] = null;
6✔
96
                }
2✔
97
            } else {
2✔
98
                $this->addFile($path);
9✔
99
            }//end if
100
        }//end foreach
5✔
101

102
        reset($this->files);
15✔
103
        $this->numFiles = count($this->files);
15✔
104

105
    }//end __construct()
10✔
106

107

108
    /**
109
     * Add a file to the list.
110
     *
111
     * If a file object has already been created, it can be passed here.
112
     * If it is left NULL, it will be created when accessed.
113
     *
114
     * @param string                      $path The path to the file being added.
115
     * @param \PHP_CodeSniffer\Files\File $file The file being added.
116
     *
117
     * @return void
118
     */
119
    public function addFile($path, $file=null)
12✔
120
    {
121
        // No filtering is done for STDIN when the filename
122
        // has not been specified.
123
        if ($path === 'STDIN') {
12✔
124
            $this->files[$path] = $file;
3✔
125
            $this->numFiles++;
3✔
126
            return;
3✔
127
        }
128

129
        $filterClass = $this->getFilterClass();
9✔
130

131
        $di       = new RecursiveArrayIterator([$path]);
9✔
132
        $filter   = new $filterClass($di, $path, $this->config, $this->ruleset);
9✔
133
        $iterator = new RecursiveIteratorIterator($filter);
9✔
134

135
        foreach ($iterator as $path) {
9✔
136
            if (array_key_exists($path, $this->files) === true) {
9✔
137
                // The path has already been added.
138
                continue;
3✔
139
            }
140

141
            $this->files[$path] = $file;
9✔
142
            $this->numFiles++;
9✔
143
        }
3✔
144

145
    }//end addFile()
6✔
146

147

148
    /**
149
     * Get the class name of the filter being used for the run.
150
     *
151
     * @return string
152
     * @throws \PHP_CodeSniffer\Exceptions\DeepExitException If the specified filter could not be found.
153
     */
UNCOV
154
    private function getFilterClass()
×
155
    {
UNCOV
156
        $filterType = $this->config->filter;
×
157

158
        if ($filterType === null) {
×
UNCOV
159
            $filterClass = '\PHP_CodeSniffer\Filters\Filter';
×
160
        } else {
UNCOV
161
            if (strpos($filterType, '.') !== false) {
×
162
                // This is a path to a custom filter class.
163
                $filename = realpath($filterType);
×
164
                if ($filename === false) {
×
165
                    $error = "ERROR: Custom filter \"$filterType\" not found".PHP_EOL;
×
UNCOV
166
                    throw new DeepExitException($error, 3);
×
167
                }
168

UNCOV
169
                $filterClass = Autoload::loadFile($filename);
×
170
            } else {
UNCOV
171
                $filterClass = '\PHP_CodeSniffer\Filters\\'.$filterType;
×
172
            }
173
        }
174

UNCOV
175
        return $filterClass;
×
176

177
    }//end getFilterClass()
178

179

180
    /**
181
     * Rewind the iterator to the first file.
182
     *
183
     * @return void
184
     */
185
    #[ReturnTypeWillChange]
186
    public function rewind()
187
    {
UNCOV
188
        reset($this->files);
×
189

190
    }//end rewind()
191

192

193
    /**
194
     * Get the file that is currently being processed.
195
     *
196
     * @return \PHP_CodeSniffer\Files\File
197
     */
198
    #[ReturnTypeWillChange]
199
    public function current()
200
    {
201
        $path = key($this->files);
×
202
        if (isset($this->files[$path]) === false) {
×
UNCOV
203
            $this->files[$path] = new LocalFile($path, $this->ruleset, $this->config);
×
204
        }
205

UNCOV
206
        return $this->files[$path];
×
207

208
    }//end current()
209

210

211
    /**
212
     * Return the file path of the current file being processed.
213
     *
214
     * @return string|null Path name or `null` when the end of the iterator has been reached.
215
     */
216
    #[ReturnTypeWillChange]
217
    public function key()
218
    {
UNCOV
219
        return key($this->files);
×
220

221
    }//end key()
222

223

224
    /**
225
     * Move forward to the next file.
226
     *
227
     * @return void
228
     */
229
    #[ReturnTypeWillChange]
230
    public function next()
231
    {
UNCOV
232
        next($this->files);
×
233

234
    }//end next()
235

236

237
    /**
238
     * Checks if current position is valid.
239
     *
240
     * @return boolean
241
     */
242
    #[ReturnTypeWillChange]
243
    public function valid()
244
    {
245
        if (current($this->files) === false) {
×
UNCOV
246
            return false;
×
247
        }
248

UNCOV
249
        return true;
×
250

251
    }//end valid()
252

253

254
    /**
255
     * Return the number of files in the list.
256
     *
257
     * @return integer
258
     */
259
    #[ReturnTypeWillChange]
260
    public function count()
261
    {
UNCOV
262
        return $this->numFiles;
×
263

264
    }//end count()
265

266

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

© 2026 Coveralls, Inc