5 * Copyright (c) 2009-2011, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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
46 require_once 'File/Iterator/Factory.php';
49 * Filter for blacklisting and whitelisting of code coverage information.
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
60 class PHP_CodeCoverage_Filter
63 * Source files that are blacklisted.
67 protected $blacklistedFiles = array(
72 * Source files that are whitelisted.
76 protected $whitelistedFiles = array();
79 * Default PHP_CodeCoverage object.
81 * @var PHP_CodeCoverage
83 protected static $instance;
86 * Returns the default instance.
88 * @return PHP_CodeCoverage_Filter
90 public static function getInstance()
92 if (self::$instance === NULL) {
93 // @codeCoverageIgnoreStart
94 self::$instance = new PHP_CodeCoverage_Filter;
96 // @codeCoverageIgnoreEnd
98 return self::$instance;
102 * Adds a directory to the blacklist (recursively).
104 * @param string $directory
105 * @param string $suffix
106 * @param string $prefix
107 * @param string $group
109 public function addDirectoryToBlacklist($directory, $suffix = '.php', $prefix = '', $group = 'DEFAULT')
111 $files = File_Iterator_Factory::getFileIterator(
112 $directory, $suffix, $prefix
115 foreach ($files as $file) {
116 $this->addFileToBlacklist($file->getPathName(), $group, FALSE);
121 * Adds a file to the blacklist.
123 * @param string $filename
124 * @param string $group
126 public function addFileToBlacklist($filename, $group = 'DEFAULT')
128 $this->blacklistedFiles[$group][realpath($filename)] = TRUE;
132 * Adds files to the blacklist.
134 * @param array $files
135 * @param string $group
137 public function addFilesToBlacklist(array $files, $group = 'DEFAULT')
139 foreach ($files as $file) {
140 $this->addFileToBlacklist($file, $group);
145 * Removes a directory from the blacklist (recursively).
147 * @param string $directory
148 * @param string $suffix
149 * @param string $prefix
150 * @param string $group
152 public function removeDirectoryFromBlacklist($directory, $suffix = '.php', $prefix = '', $group = 'DEFAULT')
154 $files = File_Iterator_Factory::getFileIterator(
155 $directory, $suffix, $prefix
158 foreach ($files as $file) {
159 $this->removeFileFromBlacklist($file->getPathName(), $group);
164 * Removes a file from the blacklist.
166 * @param string $filename
167 * @param string $group
169 public function removeFileFromBlacklist($filename, $group = 'DEFAULT')
171 $filename = realpath($filename);
173 if (isset($this->blacklistedFiles[$group][$filename])) {
174 unset($this->blacklistedFiles[$group][$filename]);
179 * Adds a directory to the whitelist (recursively).
181 * @param string $directory
182 * @param string $suffix
183 * @param string $prefix
185 public function addDirectoryToWhitelist($directory, $suffix = '.php', $prefix = '')
187 $files = File_Iterator_Factory::getFileIterator(
188 $directory, $suffix, $prefix
191 foreach ($files as $file) {
192 $this->addFileToWhitelist($file->getPathName(), FALSE);
197 * Adds a file to the whitelist.
199 * When the whitelist is empty (default), blacklisting is used.
200 * When the whitelist is not empty, whitelisting is used.
202 * @param string $filename
204 public function addFileToWhitelist($filename)
206 $this->whitelistedFiles[realpath($filename)] = TRUE;
210 * Adds files to the whitelist.
212 * @param array $files
214 public function addFilesToWhitelist(array $files)
216 foreach ($files as $file) {
217 $this->addFileToWhitelist($file);
222 * Removes a directory from the whitelist (recursively).
224 * @param string $directory
225 * @param string $suffix
226 * @param string $prefix
228 public function removeDirectoryFromWhitelist($directory, $suffix = '.php', $prefix = '')
230 $files = File_Iterator_Factory::getFileIterator(
231 $directory, $suffix, $prefix
234 foreach ($files as $file) {
235 $this->removeFileFromWhitelist($file->getPathName());
240 * Removes a file from the whitelist.
242 * @param string $filename
244 public function removeFileFromWhitelist($filename)
246 $filename = realpath($filename);
248 if (isset($this->whitelistedFiles[$filename])) {
249 unset($this->whitelistedFiles[$filename]);
254 * Checks whether a filename is a real filename.
256 * @param string $filename
258 public static function isFile($filename)
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) {
272 * Checks whether or not a file is filtered.
274 * When the whitelist is empty (default), blacklisting is used.
275 * When the whitelist is not empty, whitelisting is used.
277 * @param string $filename
278 * @param array $groups
279 * @param boolean $ignoreWhitelist
281 * @throws InvalidArgumentException
283 public function isFiltered($filename, array $groups = array('DEFAULT'), $ignoreWhitelist = FALSE)
285 if (!is_bool($ignoreWhitelist)) {
286 throw new InvalidArgumentException;
289 $filename = realpath($filename);
291 if (!$ignoreWhitelist && !empty($this->whitelistedFiles)) {
292 return !isset($this->whitelistedFiles[$filename]);
295 $blacklistedFiles = array();
297 foreach ($groups as $group) {
298 if (isset($this->blacklistedFiles[$group])) {
299 $blacklistedFiles = array_merge(
301 $this->blacklistedFiles[$group]
306 return isset($blacklistedFiles[$filename]);
310 * Returns the list of blacklisted files.
314 public function getBlacklist()
316 $blacklistedFiles = array();
318 foreach ($this->blacklistedFiles as $group => $list) {
319 $blacklistedFiles[$group] = array_keys($list);
322 return $blacklistedFiles;
326 * Returns the list of whitelisted files.
330 public function getWhitelist()
332 return array_keys($this->whitelistedFiles);