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

PHPCSStandards / PHP_CodeSniffer / 13887072476

16 Mar 2025 08:25PM UTC coverage: 78.682%. Remained the same
13887072476

Pull #882

github

web-flow
Merge 9fd6cc2aa into 384f8e824
Pull Request #882: Ruleset: add tests to document trimming behaviour

24832 of 31560 relevant lines covered (78.68%)

66.33 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
                    $this->numFiles++;
×
96
                }
97
            } else {
98
                $this->addFile($path);
×
99
            }//end if
100
        }//end foreach
101

102
        reset($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
            $this->files[$path] = $file;
×
136
            $this->numFiles++;
×
137
        }
138

139
    }//end addFile()
140

141

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

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

163
                $filterClass = Autoload::loadFile($filename);
×
164
            } else {
165
                $filterClass = '\PHP_CodeSniffer\Filters\\'.$filterType;
×
166
            }
167
        }
168

169
        return $filterClass;
×
170

171
    }//end getFilterClass()
172

173

174
    /**
175
     * Rewind the iterator to the first file.
176
     *
177
     * @return void
178
     */
179
    #[ReturnTypeWillChange]
180
    public function rewind()
181
    {
182
        reset($this->files);
×
183

184
    }//end rewind()
185

186

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

200
        return $this->files[$path];
×
201

202
    }//end current()
203

204

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

215
    }//end key()
216

217

218
    /**
219
     * Move forward to the next file.
220
     *
221
     * @return void
222
     */
223
    #[ReturnTypeWillChange]
224
    public function next()
225
    {
226
        next($this->files);
×
227

228
    }//end next()
229

230

231
    /**
232
     * Checks if current position is valid.
233
     *
234
     * @return boolean
235
     */
236
    #[ReturnTypeWillChange]
237
    public function valid()
238
    {
239
        if (current($this->files) === false) {
×
240
            return false;
×
241
        }
242

243
        return true;
×
244

245
    }//end valid()
246

247

248
    /**
249
     * Return the number of files in the list.
250
     *
251
     * @return integer
252
     */
253
    #[ReturnTypeWillChange]
254
    public function count()
255
    {
256
        return $this->numFiles;
×
257

258
    }//end count()
259

260

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

© 2025 Coveralls, Inc