Completed
Pull Request — master (#4)
by mw
11:15 queued 09:43
created

BacktraceSniffer::getCallers()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 8
cp 0.75
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
crap 4.25
1
<?php
2
3
namespace Onoi\CallbackContainer;
4
5
/**
6
 * @license GNU GPL v2+
7
 * @since 2.0
8
 */
9
class BacktraceSniffer {
10
11
	/**
12
	 * @var integer
13
	 */
14
	private $depth = 1;
15
16
	/**
17
	 * @since 2.0
18
	 *
19
	 * @param integer $depth
20
	 */
21 5
	public function __construct( $depth = 1 ) {
22 5
		$this->depth = $depth;
23 5
	}
24
25
	/**
26
	 * @see MediaWiki::wfGetCaller
27
	 * @since 2.0
28
	 *
29
	 * @return $string
0 ignored issues
show
Documentation introduced by
The doc-type $string could not be parsed: Unknown type name "$string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
30
	 */
31 2
	public function getCaller( $depth = null ) {
32
33 2
		$depth = $depth === null ? $this->depth : $depth;
34 2
		$backtrace = $this->getBackTrace( $depth + 1 );
35
36 2
		if ( isset( $backtrace[$depth] ) ) {
37 2
			return $this->doFormatStackFrame( $backtrace[$depth] );
38
		}
39
40 1
		return 'unknown';
41
	}
42
43
	/**
44
	 * @see MediaWiki::wfGetCallers
45
	 * @since 2.0
46
	 *
47
	 * @return array
48
	 */
49 2
	public function getCallers( $depth = null ) {
50
51 2
		$depth = $depth === null ? $this->depth : $depth;
52 2
		$backtrace = array_reverse( $this->getBackTrace() );
53
54
55 2
		if ( !$depth || $depth > count( $backtrace ) - 1 ) {
56
			$depth = count( $backtrace ) - 1;
57
		}
58
59 2
		$backtrace = array_slice( $backtrace, -$depth - 1, $depth );
60
61 2
		return array_map( array( $this, 'doFormatStackFrame' ), $backtrace );
62
	}
63
64 4
	private function doFormatStackFrame( $frame ) {
65 4
		return isset( $frame['class'] ) ? $frame['class'] . '::' . $frame['function'] : $frame['function'];
66
	}
67
68 4
	private function getBackTrace( $limit = 0 ) {
69 4
		static $disabled = null;
70
71 4
		if ( $disabled || ( $disabled = !function_exists( 'debug_backtrace' ) ) === true ) {
72
			return array();
73
		}
74
75 4
		if ( $limit && version_compare( PHP_VERSION, '5.4.0', '>=' ) ) {
76 2
			return array_slice( debug_backtrace( DEBUG_BACKTRACE_PROVIDE_OBJECT, $limit + 1 ), 1 );
77
		} else {
78 2
			return array_slice( debug_backtrace(), 1 );
79
		}
80
	}
81
82
}
83