Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Do not output core code deprecation messages by default #11429

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
"psr/container": "^1.1 || ^2.0",
"psr/http-message": "^1",
"sebastian/diff": "^4.0",
"silverstripe/config": "^2",
"silverstripe/config": "^2.2",
"silverstripe/assets": "^2.3",
"silverstripe/supported-modules": "^1.1",
"silverstripe/vendor-plugin": "^2",
"sminnee/callbacklist": "^0.1.1",
"symfony/cache": "^6.1",
Expand Down
1 change: 0 additions & 1 deletion src/Control/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
class Controller extends RequestHandler implements TemplateGlobalProvider
{

/**
* An array of arguments extracted from the URL.
*
Expand Down
109 changes: 107 additions & 2 deletions src/Dev/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace SilverStripe\Dev;

use BadMethodCallException;
use RuntimeException;
use SilverStripe\Control\Director;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\InjectionCreator;
use SilverStripe\Core\Injector\InjectorLoader;
use SilverStripe\Core\Manifest\Module;
use SilverStripe\Core\Path;
use SilverStripe\SupportedModules\MetaData;

/**
* Handles raising an notice when accessing a deprecated method, class, configuration, or behaviour.
Expand Down Expand Up @@ -77,6 +80,18 @@ class Deprecation
*/
private static bool $showNoReplacementNotices = false;

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

/**
* Cache of supported module directories, read from silverstripe/supported-modules repositories.json
*
* @internal
*/
private static array $supportedModuleDirectories = [];

/**
* 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 +161,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 +184,51 @@ 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;
}
}
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
$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;
}
return Deprecation::fileIsInSupportedModule($file);
}

/**
* Check whether a file (path to file) is in a supported module
*/
public static function fileIsInSupportedModule(string $file): bool
{
// Cache the supported modules list
if (count(Deprecation::$supportedModuleDirectories) === 0) {
// Do not make a network request when fetching metadata which could slow down a website
// While there is a small risk of the list being out of date, there is minimal downside to this
$metaData = MetaData::getAllRepositoryMetaData(fromRemote: false);
$dirs = array_map(fn($module) => "/vendor/{$module['packagist']}/", $metaData['supportedModules']);
// This is a special case for silverstripe-framework when running in CI
// Needed because module is run in the root folder rather than in the vendor folder
$dirs[] = '/silverstripe-framework/';
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
Deprecation::$supportedModuleDirectories = $dirs;
}
foreach (Deprecation::$supportedModuleDirectories as $dir) {
if (str_contains($file, $dir)) {
return true;
}
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
}
return false;
}

public static function isEnabled(): bool
Expand Down Expand Up @@ -245,6 +309,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 +338,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 +378,10 @@ public static function notice($atVersion, $string = '', $scope = Deprecation::SC
$data = [
'key' => sha1($string),
'message' => $string,
// Setting to `false` as here as any SCOPE_CONFIG notices from supported modules have
// already been filtered out if needed if they came from a supported module in
// SilverStripe\Config\Transformer\YamlTransformer::checkForDeprecatedConfig()
'isCalledFromSupportedCode' => false,
'calledWithNoticeSuppression' => Deprecation::$insideNoticeSuppression
];
} else {
Expand Down Expand Up @@ -322,13 +410,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 +448,23 @@ public static function notice($atVersion, $string = '', $scope = Deprecation::SC
}
}

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

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