]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHP/CodeCoverage/Filter.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHP / CodeCoverage / Filter.php
1 <?php
2 /**
3  * PHP_CodeCoverage
4  *
5  * Copyright (c) 2009-2011, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @category   PHP
38  * @package    CodeCoverage
39  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40  * @copyright  2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42  * @link       http://github.com/sebastianbergmann/php-code-coverage
43  * @since      File available since Release 1.0.0
44  */
45
46 require_once 'File/Iterator/Factory.php';
47
48 /**
49  * Filter for blacklisting and whitelisting of code coverage information.
50  *
51  * @category   PHP
52  * @package    CodeCoverage
53  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
54  * @copyright  2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
55  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
56  * @version    Release: 1.0.4
57  * @link       http://github.com/sebastianbergmann/php-code-coverage
58  * @since      Class available since Release 1.0.0
59  */
60 class PHP_CodeCoverage_Filter
61 {
62     /**
63      * Source files that are blacklisted.
64      *
65      * @var array
66      */
67     protected $blacklistedFiles = array(
68       'DEFAULT' => array()
69     );
70
71     /**
72      * Source files that are whitelisted.
73      *
74      * @var array
75      */
76     protected $whitelistedFiles = array();
77
78     /**
79      * Default PHP_CodeCoverage object.
80      *
81      * @var PHP_CodeCoverage
82      */
83     protected static $instance;
84
85     /**
86      * Returns the default instance.
87      *
88      * @return PHP_CodeCoverage_Filter
89      */
90     public static function getInstance()
91     {
92         if (self::$instance === NULL) {
93             // @codeCoverageIgnoreStart
94             self::$instance = new PHP_CodeCoverage_Filter;
95         }
96         // @codeCoverageIgnoreEnd
97
98         return self::$instance;
99     }
100
101     /**
102      * Adds a directory to the blacklist (recursively).
103      *
104      * @param string $directory
105      * @param string $suffix
106      * @param string $prefix
107      * @param string $group
108      */
109     public function addDirectoryToBlacklist($directory, $suffix = '.php', $prefix = '', $group = 'DEFAULT')
110     {
111         $files = File_Iterator_Factory::getFileIterator(
112           $directory, $suffix, $prefix
113         );
114
115         foreach ($files as $file) {
116             $this->addFileToBlacklist($file->getPathName(), $group, FALSE);
117         }
118     }
119
120     /**
121      * Adds a file to the blacklist.
122      *
123      * @param string $filename
124      * @param string $group
125      */
126     public function addFileToBlacklist($filename, $group = 'DEFAULT')
127     {
128         $this->blacklistedFiles[$group][realpath($filename)] = TRUE;
129     }
130
131     /**
132      * Adds files to the blacklist.
133      *
134      * @param array  $files
135      * @param string $group
136      */
137     public function addFilesToBlacklist(array $files, $group = 'DEFAULT')
138     {
139         foreach ($files as $file) {
140             $this->addFileToBlacklist($file, $group);
141         }
142     }
143
144     /**
145      * Removes a directory from the blacklist (recursively).
146      *
147      * @param string $directory
148      * @param string $suffix
149      * @param string $prefix
150      * @param string $group
151      */
152     public function removeDirectoryFromBlacklist($directory, $suffix = '.php', $prefix = '', $group = 'DEFAULT')
153     {
154         $files = File_Iterator_Factory::getFileIterator(
155           $directory, $suffix, $prefix
156         );
157
158         foreach ($files as $file) {
159             $this->removeFileFromBlacklist($file->getPathName(), $group);
160         }
161     }
162
163     /**
164      * Removes a file from the blacklist.
165      *
166      * @param string $filename
167      * @param string $group
168      */
169     public function removeFileFromBlacklist($filename, $group = 'DEFAULT')
170     {
171         $filename = realpath($filename);
172
173         if (isset($this->blacklistedFiles[$group][$filename])) {
174             unset($this->blacklistedFiles[$group][$filename]);
175         }
176     }
177
178     /**
179      * Adds a directory to the whitelist (recursively).
180      *
181      * @param string $directory
182      * @param string $suffix
183      * @param string $prefix
184      */
185     public function addDirectoryToWhitelist($directory, $suffix = '.php', $prefix = '')
186     {
187         $files = File_Iterator_Factory::getFileIterator(
188           $directory, $suffix, $prefix
189         );
190
191         foreach ($files as $file) {
192             $this->addFileToWhitelist($file->getPathName(), FALSE);
193         }
194     }
195
196     /**
197      * Adds a file to the whitelist.
198      *
199      * When the whitelist is empty (default), blacklisting is used.
200      * When the whitelist is not empty, whitelisting is used.
201      *
202      * @param string $filename
203      */
204     public function addFileToWhitelist($filename)
205     {
206         $this->whitelistedFiles[realpath($filename)] = TRUE;
207     }
208
209     /**
210      * Adds files to the whitelist.
211      *
212      * @param array $files
213      */
214     public function addFilesToWhitelist(array $files)
215     {
216         foreach ($files as $file) {
217             $this->addFileToWhitelist($file);
218         }
219     }
220
221     /**
222      * Removes a directory from the whitelist (recursively).
223      *
224      * @param string $directory
225      * @param string $suffix
226      * @param string $prefix
227      */
228     public function removeDirectoryFromWhitelist($directory, $suffix = '.php', $prefix = '')
229     {
230         $files = File_Iterator_Factory::getFileIterator(
231           $directory, $suffix, $prefix
232         );
233
234         foreach ($files as $file) {
235             $this->removeFileFromWhitelist($file->getPathName());
236         }
237     }
238
239     /**
240      * Removes a file from the whitelist.
241      *
242      * @param string $filename
243      */
244     public function removeFileFromWhitelist($filename)
245     {
246         $filename = realpath($filename);
247
248         if (isset($this->whitelistedFiles[$filename])) {
249             unset($this->whitelistedFiles[$filename]);
250         }
251     }
252
253     /**
254      * Checks whether a filename is a real filename.
255      *
256      * @param string $filename
257      */
258     public static function isFile($filename)
259     {
260         if ($filename == '-' ||
261             strpos($filename, 'eval()\'d code') !== FALSE ||
262             strpos($filename, 'runtime-created function') !== FALSE ||
263             strpos($filename, 'assert code') !== FALSE ||
264             strpos($filename, 'regexp code') !== FALSE) {
265             return FALSE;
266         }
267
268         return TRUE;
269     }
270
271     /**
272      * Checks whether or not a file is filtered.
273      *
274      * When the whitelist is empty (default), blacklisting is used.
275      * When the whitelist is not empty, whitelisting is used.
276      *
277      * @param  string  $filename
278      * @param  array   $groups
279      * @param  boolean $ignoreWhitelist
280      * @return boolean
281      * @throws InvalidArgumentException
282      */
283     public function isFiltered($filename, array $groups = array('DEFAULT'), $ignoreWhitelist = FALSE)
284     {
285         if (!is_bool($ignoreWhitelist)) {
286             throw new InvalidArgumentException;
287         }
288
289         $filename = realpath($filename);
290
291         if (!$ignoreWhitelist && !empty($this->whitelistedFiles)) {
292             return !isset($this->whitelistedFiles[$filename]);
293         }
294
295         $blacklistedFiles = array();
296
297         foreach ($groups as $group) {
298             if (isset($this->blacklistedFiles[$group])) {
299                 $blacklistedFiles = array_merge(
300                   $blacklistedFiles,
301                   $this->blacklistedFiles[$group]
302                 );
303             }
304         }
305
306         return isset($blacklistedFiles[$filename]);
307     }
308
309     /**
310      * Returns the list of blacklisted files.
311      *
312      * @return array
313      */
314     public function getBlacklist()
315     {
316         $blacklistedFiles = array();
317
318         foreach ($this->blacklistedFiles as $group => $list) {
319             $blacklistedFiles[$group] = array_keys($list);
320         }
321
322         return $blacklistedFiles;
323     }
324
325     /**
326      * Returns the list of whitelisted files.
327      *
328      * @return array
329      */
330     public function getWhitelist()
331     {
332         return array_keys($this->whitelistedFiles);
333     }
334 }