From: Platonides Date: Thu, 31 May 2012 15:29:43 +0000 (+0200) Subject: PHP >= 5.4.0 started taking advantage of $limit optimization of wfDebugBacktrace(), X-Git-Tag: 1.31.0-rc.0~23442^2 X-Git-Url: http://git.cyclocoop.org/%28?a=commitdiff_plain;h=2e506c4809624c2a91e7798a38f2996e321cd95f;p=lhc%2Fweb%2Fwiklou.git PHP >= 5.4.0 started taking advantage of $limit optimization of wfDebugBacktrace(), 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 --- diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 57ab64cf4d..4cfd946ab6 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -1831,7 +1831,7 @@ function wfDebugBacktrace( $limit = 0 ) { } 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 ); } @@ -1890,12 +1890,15 @@ function wfBacktrace() { /** * 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 { diff --git a/includes/debug/Debug.php b/includes/debug/Debug.php index 211f74a044..0c0052f434 100644 --- a/includes/debug/Debug.php +++ b/includes/debug/Debug.php @@ -145,9 +145,10 @@ class MWDebug { // 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; } } @@ -155,7 +156,7 @@ class MWDebug { self::$log[] = array( 'msg' => htmlspecialchars( $msg ), 'type' => 'warn', - 'caller' => wfGetCaller( $callerOffset ), + 'caller' => $caller, ); } diff --git a/tests/phpunit/includes/GlobalFunctions/wfGetCallerTest.php b/tests/phpunit/includes/GlobalFunctions/wfGetCallerTest.php new file mode 100644 index 0000000000..4c4c4c0495 --- /dev/null +++ b/tests/phpunit/includes/GlobalFunctions/wfGetCallerTest.php @@ -0,0 +1,35 @@ +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 ) ); + } +} + diff --git a/tests/phpunit/includes/debug/MWDebugTest.php b/tests/phpunit/includes/debug/MWDebugTest.php index 5a4e66d422..1627c47358 100644 --- a/tests/phpunit/includes/debug/MWDebugTest.php +++ b/tests/phpunit/includes/debug/MWDebugTest.php @@ -30,7 +30,7 @@ class MWDebugTest extends MediaWikiTestCase { $this->assertEquals( array( array( 'msg' => 'Warning message', 'type' => 'warn', - 'caller' => 'MWDebug::warning', + 'caller' => 'MWDebugTest::testAddWarning', ) ), MWDebug::getLog() );