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 d953266
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 18 deletions.
92 changes: 90 additions & 2 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 $showNoticesCalledFromSupportedCode = 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,6 +151,12 @@ 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
Expand All @@ -163,8 +174,47 @@ 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 isCalledFromSupportedCode(array $backtrace): bool
{
$called = Deprecation::get_called_from_trace($backtrace, 1);
$file = $called['file'] ?? '';
if (!$file) {
return false;
}
// This is a special case for silverstripe-framework when running in CI
// Needed because the silverstripe-module is run in the root folder
// rather than in the vendor folder
if (str_contains($file, '/silverstripe-framework/')) {
return true;
}
// 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',
]);
if (preg_match("#/vendor/($vendors)/#", $file)) {
return true;
}
return false;
}

public static function isEnabled(): bool
Expand Down Expand Up @@ -245,6 +295,22 @@ 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 getShowNoticesCalledFromSupportedCode(): bool
{
return Deprecation::$showNoticesCalledFromSupportedCode;
}

/**
* Set whether deprecation warnings will be shown for deprecated code which is called by core Silverstripe modules.
*/
public static function setShowNoticesCalledFromSupportedCode(bool $value): void
{
Deprecation::$showNoticesCalledFromSupportedCode = $value;
}

public static function outputNotices(): void
{
if (!Deprecation::isEnabled()) {
Expand All @@ -258,9 +324,13 @@ public static function outputNotices(): void
$arr = array_shift(Deprecation::$userErrorMessageBuffer);
$message = $arr['message'];
$calledWithNoticeSuppression = $arr['calledWithNoticeSuppression'];
$isCalledFromSupportedCode = $arr['isCalledFromSupportedCode'];
if ($calledWithNoticeSuppression && !Deprecation::$showNoReplacementNotices) {
continue;
}
if ($isCalledFromSupportedCode && !Deprecation::$showNoticesCalledFromSupportedCode) {
continue;
}
Deprecation::$isTriggeringError = true;
user_error($message, E_USER_DEPRECATED);
Deprecation::$isTriggeringError = false;
Expand Down Expand Up @@ -294,6 +364,7 @@ public static function notice($atVersion, $string = '', $scope = Deprecation::SC
$data = [
'key' => sha1($string),
'message' => $string,
'isCalledFromSupportedCode' => false,
'calledWithNoticeSuppression' => Deprecation::$insideNoticeSuppression
];
} else {
Expand Down Expand Up @@ -322,13 +393,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,
'isCalledFromSupportedCode' => Deprecation::isCalledFromSupportedCode($backtrace),
'calledWithNoticeSuppression' => Deprecation::$insideNoticeSuppression
];
}
Expand Down Expand Up @@ -360,6 +431,23 @@ 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
*/
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 d953266

Please sign in to comment.