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

PHPCSStandards / PHP_CodeSniffer / 17483947696

05 Sep 2025 04:45AM UTC coverage: 78.507% (-0.7%) from 79.184%
17483947696

push

github

web-flow
Merge pull request #1213 from PHPCSStandards/feature/remove-filelist-tests

:fire: Hot Fix: remove FileList tests

25309 of 32238 relevant lines covered (78.51%)

73.81 hits per line

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

0.0
/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)
×
75
    {
76
        $this->ruleset = $ruleset;
×
77
        $this->config  = $config;
×
78

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

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

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

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

101
        reset($this->files);
×
102
        $this->numFiles = count($this->files);
×
103

104
    }//end __construct()
105

106

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

128
        $filterClass = $this->getFilterClass();
×
129

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

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

140
            $this->files[$path] = $file;
×
141
            $this->numFiles++;
×
142
        }
143

144
    }//end addFile()
145

146

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

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

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

174
        return $filterClass;
×
175

176
    }//end getFilterClass()
177

178

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

189
    }//end rewind()
190

191

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

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

207
    }//end current()
208

209

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

220
    }//end key()
221

222

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

233
    }//end next()
234

235

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

248
        return true;
×
249

250
    }//end valid()
251

252

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

263
    }//end count()
264

265

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