db backup prior to drupal security update
[yaffs-website] / vendor / phpunit / php-code-coverage / src / CodeCoverage / Filter.php
1 <?php
2 /*
3  * This file is part of the PHP_CodeCoverage package.
4  *
5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Filter for blacklisting and whitelisting of code coverage information.
13  *
14  * @since Class available since Release 1.0.0
15  */
16 class PHP_CodeCoverage_Filter
17 {
18     /**
19      * Source files that are blacklisted.
20      *
21      * @var array
22      */
23     private $blacklistedFiles = array();
24
25     /**
26      * Source files that are whitelisted.
27      *
28      * @var array
29      */
30     private $whitelistedFiles = array();
31
32     /**
33      * Adds a directory to the blacklist (recursively).
34      *
35      * @param string $directory
36      * @param string $suffix
37      * @param string $prefix
38      */
39     public function addDirectoryToBlacklist($directory, $suffix = '.php', $prefix = '')
40     {
41         $facade = new File_Iterator_Facade;
42         $files  = $facade->getFilesAsArray($directory, $suffix, $prefix);
43
44         foreach ($files as $file) {
45             $this->addFileToBlacklist($file);
46         }
47     }
48
49     /**
50      * Adds a file to the blacklist.
51      *
52      * @param string $filename
53      */
54     public function addFileToBlacklist($filename)
55     {
56         $this->blacklistedFiles[realpath($filename)] = true;
57     }
58
59     /**
60      * Adds files to the blacklist.
61      *
62      * @param array $files
63      */
64     public function addFilesToBlacklist(array $files)
65     {
66         foreach ($files as $file) {
67             $this->addFileToBlacklist($file);
68         }
69     }
70
71     /**
72      * Removes a directory from the blacklist (recursively).
73      *
74      * @param string $directory
75      * @param string $suffix
76      * @param string $prefix
77      */
78     public function removeDirectoryFromBlacklist($directory, $suffix = '.php', $prefix = '')
79     {
80         $facade = new File_Iterator_Facade;
81         $files  = $facade->getFilesAsArray($directory, $suffix, $prefix);
82
83         foreach ($files as $file) {
84             $this->removeFileFromBlacklist($file);
85         }
86     }
87
88     /**
89      * Removes a file from the blacklist.
90      *
91      * @param string $filename
92      */
93     public function removeFileFromBlacklist($filename)
94     {
95         $filename = realpath($filename);
96
97         if (isset($this->blacklistedFiles[$filename])) {
98             unset($this->blacklistedFiles[$filename]);
99         }
100     }
101
102     /**
103      * Adds a directory to the whitelist (recursively).
104      *
105      * @param string $directory
106      * @param string $suffix
107      * @param string $prefix
108      */
109     public function addDirectoryToWhitelist($directory, $suffix = '.php', $prefix = '')
110     {
111         $facade = new File_Iterator_Facade;
112         $files  = $facade->getFilesAsArray($directory, $suffix, $prefix);
113
114         foreach ($files as $file) {
115             $this->addFileToWhitelist($file);
116         }
117     }
118
119     /**
120      * Adds a file to the whitelist.
121      *
122      * @param string $filename
123      */
124     public function addFileToWhitelist($filename)
125     {
126         $this->whitelistedFiles[realpath($filename)] = true;
127     }
128
129     /**
130      * Adds files to the whitelist.
131      *
132      * @param array $files
133      */
134     public function addFilesToWhitelist(array $files)
135     {
136         foreach ($files as $file) {
137             $this->addFileToWhitelist($file);
138         }
139     }
140
141     /**
142      * Removes a directory from the whitelist (recursively).
143      *
144      * @param string $directory
145      * @param string $suffix
146      * @param string $prefix
147      */
148     public function removeDirectoryFromWhitelist($directory, $suffix = '.php', $prefix = '')
149     {
150         $facade = new File_Iterator_Facade;
151         $files  = $facade->getFilesAsArray($directory, $suffix, $prefix);
152
153         foreach ($files as $file) {
154             $this->removeFileFromWhitelist($file);
155         }
156     }
157
158     /**
159      * Removes a file from the whitelist.
160      *
161      * @param string $filename
162      */
163     public function removeFileFromWhitelist($filename)
164     {
165         $filename = realpath($filename);
166
167         if (isset($this->whitelistedFiles[$filename])) {
168             unset($this->whitelistedFiles[$filename]);
169         }
170     }
171
172     /**
173      * Checks whether a filename is a real filename.
174      *
175      * @param  string $filename
176      * @return bool
177      */
178     public function isFile($filename)
179     {
180         if ($filename == '-' ||
181             strpos($filename, 'vfs://') === 0 ||
182             strpos($filename, 'xdebug://debug-eval') !== false ||
183             strpos($filename, 'eval()\'d code') !== false ||
184             strpos($filename, 'runtime-created function') !== false ||
185             strpos($filename, 'runkit created function') !== false ||
186             strpos($filename, 'assert code') !== false ||
187             strpos($filename, 'regexp code') !== false) {
188             return false;
189         }
190
191         return file_exists($filename);
192     }
193
194     /**
195      * Checks whether or not a file is filtered.
196      *
197      * When the whitelist is empty (default), blacklisting is used.
198      * When the whitelist is not empty, whitelisting is used.
199      *
200      * @param  string                     $filename
201      * @return bool
202      * @throws PHP_CodeCoverage_Exception
203      */
204     public function isFiltered($filename)
205     {
206         if (!$this->isFile($filename)) {
207             return true;
208         }
209
210         $filename = realpath($filename);
211
212         if (!empty($this->whitelistedFiles)) {
213             return !isset($this->whitelistedFiles[$filename]);
214         }
215
216         return isset($this->blacklistedFiles[$filename]);
217     }
218
219     /**
220      * Returns the list of blacklisted files.
221      *
222      * @return array
223      */
224     public function getBlacklist()
225     {
226         return array_keys($this->blacklistedFiles);
227     }
228
229     /**
230      * Returns the list of whitelisted files.
231      *
232      * @return array
233      */
234     public function getWhitelist()
235     {
236         return array_keys($this->whitelistedFiles);
237     }
238
239     /**
240      * Returns whether this filter has a whitelist.
241      *
242      * @return bool
243      * @since  Method available since Release 1.1.0
244      */
245     public function hasWhitelist()
246     {
247         return !empty($this->whitelistedFiles);
248     }
249
250     /**
251      * Returns the blacklisted files.
252      *
253      * @return array
254      * @since Method available since Release 2.0.0
255      */
256     public function getBlacklistedFiles()
257     {
258         return $this->blacklistedFiles;
259     }
260
261     /**
262      * Sets the blacklisted files.
263      *
264      * @param array $blacklistedFiles
265      * @since Method available since Release 2.0.0
266      */
267     public function setBlacklistedFiles($blacklistedFiles)
268     {
269         $this->blacklistedFiles = $blacklistedFiles;
270     }
271
272     /**
273      * Returns the whitelisted files.
274      *
275      * @return array
276      * @since Method available since Release 2.0.0
277      */
278     public function getWhitelistedFiles()
279     {
280         return $this->whitelistedFiles;
281     }
282
283     /**
284      * Sets the whitelisted files.
285      *
286      * @param array $whitelistedFiles
287      * @since Method available since Release 2.0.0
288      */
289     public function setWhitelistedFiles($whitelistedFiles)
290     {
291         $this->whitelistedFiles = $whitelistedFiles;
292     }
293 }