Yaffs site version 1.1
[yaffs-website] / vendor / phpunit / phpunit / src / Util / Filter.php
1 <?php
2 /*
3  * This file is part of PHPUnit.
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  * Utility class for code filtering.
13  *
14  * @since Class available since Release 2.0.0
15  */
16 class PHPUnit_Util_Filter
17 {
18     /**
19      * Filters stack frames from PHPUnit classes.
20      *
21      * @param Exception $e
22      * @param bool      $asString
23      *
24      * @return string
25      */
26     public static function getFilteredStacktrace(Exception $e, $asString = true)
27     {
28         $prefix = false;
29         $script = realpath($GLOBALS['_SERVER']['SCRIPT_NAME']);
30
31         if (defined('__PHPUNIT_PHAR_ROOT__')) {
32             $prefix = __PHPUNIT_PHAR_ROOT__;
33         }
34
35         if ($asString === true) {
36             $filteredStacktrace = '';
37         } else {
38             $filteredStacktrace = array();
39         }
40
41         if ($e instanceof PHPUnit_Framework_SyntheticError) {
42             $eTrace = $e->getSyntheticTrace();
43             $eFile  = $e->getSyntheticFile();
44             $eLine  = $e->getSyntheticLine();
45         } elseif ($e instanceof PHPUnit_Framework_Exception) {
46             $eTrace = $e->getSerializableTrace();
47             $eFile  = $e->getFile();
48             $eLine  = $e->getLine();
49         } else {
50             if ($e->getPrevious()) {
51                 $e = $e->getPrevious();
52             }
53             $eTrace = $e->getTrace();
54             $eFile  = $e->getFile();
55             $eLine  = $e->getLine();
56         }
57
58         if (!self::frameExists($eTrace, $eFile, $eLine)) {
59             array_unshift(
60                 $eTrace,
61                 array('file' => $eFile, 'line' => $eLine)
62             );
63         }
64
65         $blacklist = new PHPUnit_Util_Blacklist;
66
67         foreach ($eTrace as $frame) {
68             if (isset($frame['file']) && is_file($frame['file']) &&
69                 !$blacklist->isBlacklisted($frame['file']) &&
70                 ($prefix === false || strpos($frame['file'], $prefix) !== 0) &&
71                 $frame['file'] !== $script) {
72                 if ($asString === true) {
73                     $filteredStacktrace .= sprintf(
74                         "%s:%s\n",
75                         $frame['file'],
76                         isset($frame['line']) ? $frame['line'] : '?'
77                     );
78                 } else {
79                     $filteredStacktrace[] = $frame;
80                 }
81             }
82         }
83
84         return $filteredStacktrace;
85     }
86
87     /**
88      * @param array  $trace
89      * @param string $file
90      * @param int    $line
91      *
92      * @return bool
93      *
94      * @since  Method available since Release 3.3.2
95      */
96     private static function frameExists(array $trace, $file, $line)
97     {
98         foreach ($trace as $frame) {
99             if (isset($frame['file']) && $frame['file'] == $file &&
100                 isset($frame['line']) && $frame['line'] == $line) {
101                 return true;
102             }
103         }
104
105         return false;
106     }
107 }