Completed
Pull Request — master (#4)
by mw
22:32 queued 09:07
created

BacktraceSniffer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 74
rs 10
c 0
b 0
f 0

5 Methods

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