Skip to content

Commit

Permalink
Deprecation notices: declare the constant if thrown
Browse files Browse the repository at this point in the history
... to prevent there ever being more than one deprecation notice in one page load.
  • Loading branch information
jrfnl committed Sep 17, 2021
1 parent 84efc00 commit 4e618f6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 39 deletions.
5 changes: 5 additions & 0 deletions library/Deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
. ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.',
E_USER_DEPRECATED
);

// Prevent the deprecation notice from being thrown twice.
if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS')) {
define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true);
}
}

if (interface_exists('Requests_Auth') === false && interface_exists('WpOrg\Requests\Auth') === true) {
Expand Down
5 changes: 5 additions & 0 deletions library/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
. ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.',
E_USER_DEPRECATED
);

// Prevent the deprecation notice from being thrown twice.
if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS')) {
define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true);
}
}

require_once dirname(__DIR__) . '/src/Requests.php';
Expand Down
58 changes: 19 additions & 39 deletions src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,6 @@ class Autoload {
'requests_exception_http_unknown' => '\WpOrg\Requests\Exception\Http\StatusUnknown',
);

/**
* Whether or not a deprecation notice has been thrown for calling a deprecated class.
*
* @var bool
*/
private static $deprecation_thrown = false;

/**
* Whether or not a deprecation notice should be thrown for calling a deprecated class.
*
* @var bool
*/
private static $throw_notice = true;

/**
* Register the autoloader.
*
Expand Down Expand Up @@ -146,12 +132,8 @@ public static function load($class_name) {
}

if ($class_name === 'Requests') {
/*
* Reference to the original PSR-0 Requests class.
* Loading this class will always generate a deprecation notice.
*/
$file = dirname(__DIR__) . '/library/Requests.php';
self::$deprecation_thrown = true;
// Reference to the original PSR-0 Requests class.
$file = dirname(__DIR__) . '/library/Requests.php';
} elseif ($psr_4_prefix_pos === 0) {
// PSR-4 classname.
$file = __DIR__ . '/' . strtr(substr($class_name, 15), '\\', '/') . '.php';
Expand All @@ -170,25 +152,23 @@ public static function load($class_name) {
$class_lower = strtolower($class_name);

if (isset(self::$deprecated_classes[$class_lower])) {
if (self::$throw_notice === true) {
/*
* Integrators who cannot yet upgrade to the PSR-4 class names can silence deprecations
* by defining a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant and setting it to `true`.
* The constant needs to be defined before the first deprecated class is requested
* via this autoloader.
*/
if (defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS') && REQUESTS_SILENCE_PSR0_DEPRECATIONS === true) {
self::$throw_notice = false;
}

if (self::$throw_notice === true && self::$deprecation_thrown === false) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
trigger_error(
'The PSR-0 `Requests_...` class names in the Request library are deprecated.'
. ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.',
E_USER_DEPRECATED
);
self::$deprecation_thrown = true;
/*
* Integrators who cannot yet upgrade to the PSR-4 class names can silence deprecations
* by defining a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant and setting it to `true`.
* The constant needs to be defined before the first deprecated class is requested
* via this autoloader.
*/
if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS') || REQUESTS_SILENCE_PSR0_DEPRECATIONS !== true) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
trigger_error(
'The PSR-0 `Requests_...` class names in the Request library are deprecated.'
. ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.',
E_USER_DEPRECATED
);

// Prevent the deprecation notice from being thrown twice.
if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS')) {
define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true);
}
}

Expand Down

0 comments on commit 4e618f6

Please sign in to comment.