Skip to content

Commit

Permalink
ENH Do not output core code deprecation messages by default
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Oct 16, 2024
1 parent ebbd642 commit 95b873f
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 23 deletions.
105 changes: 98 additions & 7 deletions src/Dev/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class Deprecation
*/
private static bool $showNoReplacementNotices = false;

/**
* @internal
*/
private static bool $showCalledFromSupportedCodeNotices = false;

/**
* Enable throwing deprecation warnings. By default, this excludes warnings for
* deprecated code which is called by core Silverstripe modules.
Expand Down Expand Up @@ -146,12 +151,18 @@ protected static function get_called_method_from_trace($backtrace, $level = 1)
if (!$level) {
$level = 1;
}
$called = Deprecation::get_called_from_trace($backtrace, $level);
return ($called['class'] ?? '') . ($called['type'] ?? '') . ($called['function'] ?? '');
}

private static function get_called_from_trace(array $backtrace, int $level): array
{
$newLevel = $level;
// handle closures inside withSuppressedNotice()
if (Deprecation::$insideNoticeSuppression
&& substr($backtrace[$newLevel]['function'], -strlen('{closure}')) === '{closure}'
) {
$newLevel = $newLevel + 2;
if (Deprecation::$insideNoticeSuppression) {
// handle closures inside withSuppressedNotice()
if (substr($backtrace[$newLevel]['function'], -strlen('{closure}')) === '{closure}') {
$newLevel = $newLevel + 2;
}
}
// handle call_user_func
if ($level === 4 && strpos($backtrace[2]['function'] ?? '', 'call_user_func') !== false) {
Expand All @@ -163,8 +174,52 @@ protected static function get_called_method_from_trace($backtrace, $level = 1)
if ($level == 4 && ($backtrace[$newLevel]['class'] ?? '') === InjectionCreator::class) {
$newLevel = $newLevel + 4;
}
// handle noticeWithNoReplacment()
foreach ($backtrace as $trace) {
if (($trace['class'] ?? '') === Deprecation::class
&& ($trace['function'] ?? '') === 'noticeWithNoReplacment'
) {
$newLevel = $newLevel + 1;
break;
}
}
$called = $backtrace[$newLevel] ?? [];
return ($called['class'] ?? '') . ($called['type'] ?? '') . ($called['function'] ?? '');
return $called;
}

private static function calledFromSupportedCode(array $backtrace): bool
{
$called = Deprecation::get_called_from_trace($backtrace, 1);
$file = $called['file'] ?? '';
if ($file) {
$isSupportedVendorFile = Deprecation::isSupportedVendorFile($file);
if (!$isSupportedVendorFile) {
return false;
}
}
return true;
}

/**
* Whether the given file is supported code
*/
private static function isSupportedVendorFile(string $file): bool
{
// Doing a fairly simple check to see if a file is in a supported vendor folder, rather than whether
// the module itself is actually supported
$vendors = implode('|', [
'bringyourownideas',
'colymba',
'cwp',
'dnadesign',
'silverstripe',
'symbiote',
'tractorcow',
// this is a special case for silverstripe-framework when running in CI
'silverstripe-framework',
]);
var_dump($file);
return (bool) preg_match("#/vendor/($vendors)/#", $file);
}

public static function isEnabled(): bool
Expand Down Expand Up @@ -245,6 +300,19 @@ public static function shouldShowForCli(): bool
return Deprecation::$shouldShowForCli;
}

/**
* If true, deprecation warnings will be shown for deprecated code which is called by core Silverstripe modules.
*/
public static function getShowCalledFromSupportedCodeNotices(): bool
{
return Deprecation::$showCalledFromSupportedCodeNotices;
}

public static function setShowCalledFromSupportedCodeNotices(bool $value): void
{
Deprecation::$showCalledFromSupportedCodeNotices = $value;
}

public static function outputNotices(): void
{
if (!Deprecation::isEnabled()) {
Expand All @@ -258,9 +326,13 @@ public static function outputNotices(): void
$arr = array_shift(Deprecation::$userErrorMessageBuffer);
$message = $arr['message'];
$calledWithNoticeSuppression = $arr['calledWithNoticeSuppression'];
$calledFromSupportedCode = $arr['calledFromSupportedCode'];
if ($calledWithNoticeSuppression && !Deprecation::$showNoReplacementNotices) {
continue;
}
if ($calledFromSupportedCode && !Deprecation::$showCalledFromSupportedCodeNotices) {
continue;
}
Deprecation::$isTriggeringError = true;
user_error($message, E_USER_DEPRECATED);
Deprecation::$isTriggeringError = false;
Expand Down Expand Up @@ -294,6 +366,7 @@ public static function notice($atVersion, $string = '', $scope = Deprecation::SC
$data = [
'key' => sha1($string),
'message' => $string,
'calledFromSupportedCode' => false,
'calledWithNoticeSuppression' => Deprecation::$insideNoticeSuppression
];
} else {
Expand Down Expand Up @@ -322,13 +395,13 @@ public static function notice($atVersion, $string = '', $scope = Deprecation::SC

$level = Deprecation::$insideNoticeSuppression ? 4 : 2;
$string .= " Called from " . Deprecation::get_called_method_from_trace($backtrace, $level) . '.';

if ($caller) {
$string = $caller . ' is deprecated.' . ($string ? ' ' . $string : '');
}
$data = [
'key' => sha1($string),
'message' => $string,
'calledFromSupportedCode' => Deprecation::calledFromSupportedCode($backtrace),
'calledWithNoticeSuppression' => Deprecation::$insideNoticeSuppression
];
}
Expand Down Expand Up @@ -360,6 +433,24 @@ public static function notice($atVersion, $string = '', $scope = Deprecation::SC
}
}

/**
* Shorthand method to create a suppressed notice for something with no immediate replacement.
* If $string is empty, then a standardised message will be used, which is:
* Will be removed without equivalent functionality to replace it.
*/
public static function noticeWithNoReplacment(
string $atVersion,
string $string = '',
int $scope = Deprecation::SCOPE_METHOD
): void {
if ($string === '') {
$string = 'Will be removed without equivalent functionality to replace it.';
}
Deprecation::withSuppressedNotice(
fn() => Deprecation::notice($atVersion, $string, $scope)
);
}

private static function varAsBoolean($val): bool
{
if (is_string($val)) {
Expand Down
Loading

0 comments on commit 95b873f

Please sign in to comment.