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

PHPCSStandards / PHP_CodeSniffer / 16808529167

07 Aug 2025 03:14PM UTC coverage: 78.557% (+0.1%) from 78.441%
16808529167

Pull #1126

github

web-flow
Merge 98b197449 into 501c14723
Pull Request #1126: fix: Check that the result of pcntl_waitpid is the PID of a managed process

0 of 1 new or added line in 1 file covered. (0.0%)

93 existing lines in 4 files now uncovered.

25260 of 32155 relevant lines covered (78.56%)

69.67 hits per line

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

60.66
/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
                    $this->files[$file->getPathname()] = null;
6✔
95
                }
2✔
96
            } else {
2✔
97
                $this->addFile($path);
9✔
98
            }//end if
99
        }//end foreach
5✔
100

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

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

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

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

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

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

144
    }//end addFile()
6✔
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.
UNCOV
162
                $filename = realpath($filterType);
×
163
                if ($filename === false) {
×
UNCOV
164
                    $error = "ERROR: Custom filter \"$filterType\" not found".PHP_EOL;
×
165
                    throw new DeepExitException($error, 3);
×
166
                }
167

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

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

UNCOV
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
    {
UNCOV
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
    {
UNCOV
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
    {
UNCOV
244
        if (current($this->files) === false) {
×
UNCOV
245
            return false;
×
246
        }
247

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