~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/phpunit/phpunit/src/Util/Filter.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

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