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

PHPCSStandards / PHP_CodeSniffer / 17174734458

23 Aug 2025 11:06AM UTC coverage: 76.88% (-2.1%) from 78.934%
17174734458

push

github

jrfnl
TEMP/TESTING PHPUnit 6331

19187 of 24957 relevant lines covered (76.88%)

60.25 hits per line

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

50.82
/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 PHP_CodeSniffer\Util\ExitCode;
23
use RecursiveArrayIterator;
24
use RecursiveDirectoryIterator;
25
use RecursiveIteratorIterator;
26
use ReturnTypeWillChange;
27

28
class FileList implements Iterator, Countable
29
{
30

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

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

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

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

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

66

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

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

88
                $filterClass = $this->getFilterClass();
4✔
89

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

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

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

105
    }//end __construct()
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)
8✔
120
    {
121
        // No filtering is done for STDIN when the filename
122
        // has not been specified.
123
        if ($path === 'STDIN') {
8✔
124
            $this->files[$path] = $file;
2✔
125
            $this->numFiles++;
2✔
126
            return;
2✔
127
        }
128

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

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

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

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

145
    }//end addFile()
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
     */
154
    private function getFilterClass()
×
155
    {
156
        $filterType = $this->config->filter;
×
157

158
        if ($filterType === null) {
×
159
            $filterClass = '\PHP_CodeSniffer\Filters\Filter';
×
160
        } else {
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;
×
166
                    throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
×
167
                }
168

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

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
    {
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) {
×
203
            $this->files[$path] = new LocalFile($path, $this->ruleset, $this->config);
×
204
        }
205

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
    {
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
    {
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) {
×
246
            return false;
×
247
        }
248

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
    {
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