comparison vendor/squizlabs/php_codesniffer/scripts/ValidatePEAR/FileList.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php
2 /**
3 * Retrieve a filtered file list.
4 *
5 * PHP version 5
6 *
7 * @category PHP
8 * @package PHP_CodeSniffer
9 * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
10 * @copyright 2019 Juliette Reinders Folmer. All rights reserved.
11 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12 */
13
14 /**
15 * Class to create a file list with filtering.
16 */
17 class FileList
18 {
19
20 /**
21 * The path to the project root directory.
22 *
23 * @var string
24 */
25 protected $rootPath;
26
27 /**
28 * Recursive directory iterator.
29 *
30 * @var \DirectoryIterator
31 */
32 protected $fileIterator;
33
34 /**
35 * Base regex to use if no filter regex is provided.
36 *
37 * Matches based on:
38 * - File path starts with the project root (replacement done in constructor).
39 * - Don't match .git/ files.
40 * - Don't match dot files, i.e. "." or "..".
41 * - Don't match backup files.
42 * - Match everything else in a case-insensitive manner.
43 *
44 * @var string
45 */
46 private $baseRegex = '`^%s(?!\.git/)(?!(.*/)?\.+$)(?!.*\.(bak|orig)).*$`Dix';
47
48
49 /**
50 * Constructor.
51 *
52 * @param string $directory The directory to examine.
53 * @param string $rootPath Path to the project root.
54 * @param string $filter PCRE regular expression to filter the file list with.
55 */
56 public function __construct($directory, $rootPath='', $filter='')
57 {
58 $this->rootPath = $rootPath;
59
60 $directory = new \RecursiveDirectoryIterator(
61 $directory,
62 \RecursiveDirectoryIterator::UNIX_PATHS
63 );
64 $flattened = new \RecursiveIteratorIterator(
65 $directory,
66 \RecursiveIteratorIterator::LEAVES_ONLY,
67 \RecursiveIteratorIterator::CATCH_GET_CHILD
68 );
69
70 if ($filter === '') {
71 $filter = sprintf($this->baseRegex, preg_quote($this->rootPath));
72 }
73
74 $this->fileIterator = new \RegexIterator($flattened, $filter);
75
76 return $this;
77
78 }//end __construct()
79
80
81 /**
82 * Retrieve the filtered file list as an array.
83 *
84 * @return array
85 */
86 public function getList()
87 {
88 $fileList = [];
89
90 foreach ($this->fileIterator as $file) {
91 $fileList[] = str_replace($this->rootPath, '', $file);
92 }
93
94 return $fileList;
95
96 }//end getList()
97
98
99 }//end class