FIX Correctly identify deprecated API in withNoReplacement (#10706)

This commit is contained in:
Guy Sartorelli 2023-02-27 15:25:27 +13:00 committed by GitHub
parent ab566b0a15
commit 652281507f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -177,6 +177,12 @@ class Deprecation
$level = 1; $level = 1;
} }
$newLevel = $level; $newLevel = $level;
// handle closures inside withNoReplacement()
if (self::$insideWithNoReplacement
&& substr($backtrace[$newLevel]['function'], -strlen('{closure}')) === '{closure}'
) {
$newLevel = $newLevel + 2;
}
// handle call_user_func // handle call_user_func
if ($level === 4 && strpos($backtrace[2]['function'] ?? '', 'call_user_func') !== false) { if ($level === 4 && strpos($backtrace[2]['function'] ?? '', 'call_user_func') !== false) {
$newLevel = 5; $newLevel = 5;
@ -277,10 +283,10 @@ class Deprecation
// Do not add to self::$userErrorMessageBuffer, as the backtrace is too expensive // Do not add to self::$userErrorMessageBuffer, as the backtrace is too expensive
return; return;
} }
// Getting a backtrace is slow, so we only do it if we need it // Getting a backtrace is slow, so we only do it if we need it
$backtrace = null; $backtrace = null;
// Get the calling scope // Get the calling scope
if ($scope == Deprecation::SCOPE_METHOD) { if ($scope == Deprecation::SCOPE_METHOD) {
$backtrace = debug_backtrace(0); $backtrace = debug_backtrace(0);
@ -291,7 +297,7 @@ class Deprecation
} else { } else {
$caller = false; $caller = false;
} }
if (substr($string, -1) != '.') { if (substr($string, -1) != '.') {
$string .= "."; $string .= ".";
} }

View File

@ -151,6 +151,22 @@ class DeprecationTest extends SapphireTest
Deprecation::outputNotices(); Deprecation::outputNotices();
} }
public function testNoticeWithNoReplacementTrue()
{
$message = implode(' ', [
'SilverStripe\Dev\Tests\DeprecationTest->testNoticeWithNoReplacementTrue is deprecated.',
'My message.',
'Called from PHPUnit\Framework\TestCase->runTest.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
Deprecation::enable(true);
Deprecation::withNoReplacement(function () {
Deprecation::notice('123', 'My message.');
});
Deprecation::outputNotices();
}
public function testClassWithNoReplacement() public function testClassWithNoReplacement()
{ {
$message = implode(' ', [ $message = implode(' ', [