which we weren't treating right.
* $limit in wfDebugBacktrace() is the number of returned frames,
we thus need to take into account the wfDebugBacktrace() frame, which
is sliced from debug_backtrace().
* wfGetCaller() needs to add a level for itself.
* MWDebug::warning() was logging itself as the warning issuer,
which is useless (the call a few lines before was right, though)
MWDebugTest.php changed accordingly.
* Removed double call to wfGetCaller( $callerOffset + 1 )
* Documented the meaning of wfGetCaller() parameter
* Added unit test
Change-Id: Ief50f4c810bad8b03bb2bf9dc6d945d9acb29851
}
if ( $limit && version_compare( PHP_VERSION, '5.4.0', '>=' ) ) {
- return array_slice( debug_backtrace( DEBUG_BACKTRACE_PROVIDE_OBJECT, $limit ), 1 );
+ return array_slice( debug_backtrace( DEBUG_BACKTRACE_PROVIDE_OBJECT, $limit + 1 ), 1 );
} else {
return array_slice( debug_backtrace(), 1 );
}
/**
* Get the name of the function which called this function
+ * wfGetCaller( 1 ) is the function with the wfGetCaller() call (ie. __FUNCTION__)
+ * wfGetCaller( 2 ) [default] is the caller of the function running wfGetCaller()
+ * wfGetCaller( 3 ) is the parent of that.
*
* @param $level Int
* @return Bool|string
*/
function wfGetCaller( $level = 2 ) {
- $backtrace = wfDebugBacktrace( $level );
+ $backtrace = wfDebugBacktrace( $level + 1 );
if ( isset( $backtrace[$level] ) ) {
return wfFormatStackFrame( $backtrace[$level] );
} else {
// Check to see if there was already a deprecation notice, so not to
// get a duplicate warning
$logCount = count( self::$log );
+ $caller = wfGetCaller( $callerOffset + 1 );
if ( $logCount ) {
$lastLog = self::$log[ $logCount - 1 ];
- if ( $lastLog['type'] == 'deprecated' && $lastLog['caller'] == wfGetCaller( $callerOffset + 1 ) ) {
+ if ( $lastLog['type'] == 'deprecated' && $lastLog['caller'] == $caller ) {
return;
}
}
self::$log[] = array(
'msg' => htmlspecialchars( $msg ),
'type' => 'warn',
- 'caller' => wfGetCaller( $callerOffset ),
+ 'caller' => $caller,
);
}
--- /dev/null
+<?php
+
+class wfGetCaller extends MediaWikiTestCase {
+
+ function testZero() {
+ $this->assertEquals( __METHOD__, wfGetCaller( 1 ) );
+ }
+
+ function callerOne() {
+ return wfGetCaller();
+ }
+
+ function testOne() {
+ $this->assertEquals( "wfGetCaller::testOne", self::callerOne() );
+ }
+
+ function intermediateFunction( $level = 2, $n = 0 ) {
+ if ( $n > 0 )
+ return self::intermediateFunction( $level, $n - 1 );
+ return wfGetCaller( $level );
+ }
+
+ function testTwo() {
+ $this->assertEquals( "wfGetCaller::testTwo", self::intermediateFunction() );
+ }
+
+ function testN() {
+ $this->assertEquals( "wfGetCaller::testN", self::intermediateFunction( 2, 0 ) );
+ $this->assertEquals( "wfGetCaller::intermediateFunction", self::intermediateFunction( 1, 0 ) );
+
+ for ($i=0; $i < 10; $i++)
+ $this->assertEquals( "wfGetCaller::intermediateFunction", self::intermediateFunction( $i + 1, $i ) );
+ }
+}
+
$this->assertEquals( array( array(
'msg' => 'Warning message',
'type' => 'warn',
- 'caller' => 'MWDebug::warning',
+ 'caller' => 'MWDebugTest::testAddWarning',
) ),
MWDebug::getLog()
);