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

PHPCSStandards / PHP_CodeSniffer / 17662127818

12 Sep 2025 01:50AM UTC coverage: 78.786%. Remained the same
17662127818

push

github

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

CS: normalize code style rules [3]

343 of 705 new or added lines in 108 files covered. (48.65%)

3 existing lines in 3 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

57.89
/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\Filters\Filter;
21
use PHP_CodeSniffer\Ruleset;
22
use PHP_CodeSniffer\Util\Common;
23
use PHP_CodeSniffer\Util\ExitCode;
24
use RecursiveArrayIterator;
25
use RecursiveDirectoryIterator;
26
use RecursiveIteratorIterator;
27
use ReturnTypeWillChange;
28

29
class FileList implements Iterator, Countable
30
{
31

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

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

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

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

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

67

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

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

89
                $filterClass = $this->getFilterClass();
6✔
90

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

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

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

106
    }//end __construct()
5✔
107

108

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

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

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

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

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

146
    }//end addFile()
3✔
147

148

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

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

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

176
        return $filterClass;
×
177

178
    }//end getFilterClass()
179

180

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

191
    }//end rewind()
192

193

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

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

209
    }//end current()
210

211

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

222
    }//end key()
223

224

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

235
    }//end next()
236

237

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

250
        return true;
×
251

252
    }//end valid()
253

254

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

265
    }//end count()
266

267

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