Refactor deprecation logging into its own class

This gives us the flexibility to handle both deprecated properties and
methods/functions properly.
This commit is contained in:
Michael Große 2019-02-25 22:04:42 +01:00
parent dc7da772c0
commit 0c5eb5e2d5
No known key found for this signature in database
GPG Key ID: BDD834E001B99EDC
4 changed files with 129 additions and 46 deletions

116
inc/Debug/DebugHelper.php Normal file
View File

@ -0,0 +1,116 @@
<?php
namespace dokuwiki\Debug;
use Doku_Event;
class DebugHelper
{
const INFO_DEPRECATION_LOG_EVENT = 'INFO_DEPRECATION_LOG';
/**
* Log accesses to deprecated fucntions to the debug log
*
* @param string $alternative (optional) The function or method that should be used instead
* @param int $callerOffset (optional) How far the deprecated method is removed from this one
*
* @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT
*/
public static function dbgDeprecatedFunction($alternative = '', $callerOffset = 1)
{
global $conf;
global $EVENT_HANDLER;
if (!$conf['allowdebug'] && !$EVENT_HANDLER->hasHandlerForEvent(self::INFO_DEPRECATION_LOG_EVENT)) {
// avoid any work if no one cares
return;
}
$backtrace = debug_backtrace();
for ($i = 0; $i < $callerOffset; $i += 1) {
array_shift($backtrace);
}
list($self, $call) = $backtrace;
self::triggerDeprecationEvent(
$backtrace,
$alternative,
trim($self['class'] . '::' . $self['function'] . '()', ':'),
trim($call['class'] . '::' . $call['function'] . '()', ':'),
$call['file'],
$call['line']
);
}
/**
* This marks logs a deprecation warning for a property that should no longer be used
*
* This is usually called withing a magic getter or setter.
* For logging deprecated functions or methods see dbgDeprecatedFunction()
*
* @param string $class The class with the deprecated property
* @param string $propertyName The name of the deprecated property
*
* @triggers \dokuwiki\Debug::INFO_DEPRECATION_LOG_EVENT
*/
public static function dbgDeprecatedProperty($class, $propertyName)
{
global $conf;
global $EVENT_HANDLER;
if (!$conf['allowdebug'] && !$EVENT_HANDLER->hasHandlerForEvent(self::INFO_DEPRECATION_LOG_EVENT)) {
// avoid any work if no one cares
return;
}
$backtrace = debug_backtrace();
array_shift($backtrace);
$call = $backtrace[1];
$caller = trim($call['class'] . '::' . $call['function'] . '()', ':');
$qualifiedName = $class . '::$' . $propertyName;
self::triggerDeprecationEvent(
$backtrace,
'',
$qualifiedName,
$caller,
$backtrace[0]['file'],
$backtrace[0]['line']
);
}
/**
* @param array $backtrace
* @param string $alternative
* @param string $deprecatedThing
* @param string $caller
* @param string $file
* @param int $line
*/
private static function triggerDeprecationEvent(
array $backtrace,
$alternative,
$deprecatedThing,
$caller,
$file,
$line
) {
$data = [
'trace' => $backtrace,
'alternative' => $alternative,
'called' => $deprecatedThing,
'caller' => $caller,
'file' => $file,
'line' => $line,
];
$event = new Doku_Event(self::INFO_DEPRECATION_LOG_EVENT, $data);
if ($event->advise_before()) {
$msg = $event->data['called'] . ' is deprecated. It was called from ';
$msg .= $event->data['caller'] . ' in ' . $event->data['file'] . ':' . $event->data['line'];
if ($event->data['alternative']) {
$msg .= ' ' . $event->data['alternative'] . ' should be used instead!';
}
dbglog($msg);
}
$event->advise_after();
}
}

View File

@ -48,7 +48,7 @@ trait PropertyDeprecationHelper
*
* @param string $property The name of the property.
* @param null $class name of the class defining the property
* @see dbg_deprecated()
* @see DebugHelper::dbgDeprecatedProperty
*/
protected function deprecatePublicProperty(
$property,
@ -61,8 +61,7 @@ trait PropertyDeprecationHelper
{
if (isset($this->deprecatedPublicProperties[$name])) {
$class = $this->deprecatedPublicProperties[$name];
$qualifiedName = $class . '::$' . $name;
dbg_deprecated('', $qualifiedName);
DebugHelper::dbgDeprecatedProperty($class, $name);
return $this->$name;
}
@ -81,8 +80,7 @@ trait PropertyDeprecationHelper
{
if (isset($this->deprecatedPublicProperties[$name])) {
$class = $this->deprecatedPublicProperties[$name];
$qualifiedName = $class . '::$' . $name;
dbg_deprecated('', $qualifiedName);
DebugHelper::dbgDeprecatedProperty($class, $name);
$this->$name = $value;
return;
}
@ -133,5 +131,4 @@ trait PropertyDeprecationHelper
}
return false;
}
}

View File

@ -1,5 +1,9 @@
<?php
// phpcs:ignoreFile
use dokuwiki\Cache\CacheParser;
use dokuwiki\Cache\CacheInstructions;
use dokuwiki\Cache\CacheRenderer;
use dokuwiki\Debug\DebugHelper;
/**
* @deprecated since 2019-02-02 use \dokuwiki\Cache\Cache instead!
@ -13,7 +17,7 @@ class cache extends \dokuwiki\Cache\Cache
'cache is deprecated since 2019-02-02. Use \dokuwiki\Cache\Cache instead',
E_USER_DEPRECATED
);
dbg_deprecated('\dokuwiki\Cache\Cache');
DebugHelper::dbgDeprecatedFunction(dokuwiki\Cache\Cache::class);
parent::__construct($key, $ext);
}
@ -31,7 +35,7 @@ class cache_parser extends \dokuwiki\Cache\CacheParser
'cache_parser is deprecated since 2019-02-02. Use \dokuwiki\Cache\CacheParser instead',
E_USER_DEPRECATED
);
dbg_deprecated('\dokuwiki\Cache\CacheParser');
DebugHelper::dbgDeprecatedFunction(CacheParser::class);
parent::__construct($id, $file, $mode);
}
@ -49,7 +53,7 @@ class cache_renderer extends \dokuwiki\Cache\CacheRenderer
'cache_renderer is deprecated since 2019-02-02. Use \dokuwiki\Cache\CacheRenderer instead',
E_USER_DEPRECATED
);
dbg_deprecated('\dokuwiki\Cache\CacheRenderer');
DebugHelper::dbgDeprecatedFunction(CacheRenderer::class);
parent::__construct($id, $file, $mode);
}
}
@ -65,7 +69,7 @@ class cache_instructions extends \dokuwiki\Cache\CacheInstructions
'cache_instructions is deprecated since 2019-02-02. Use \dokuwiki\Cache\CacheInstructions instead',
E_USER_DEPRECATED
);
dbg_deprecated('\dokuwiki\Cache\CacheInstructions');
DebugHelper::dbgDeprecatedFunction(CacheInstructions::class);
parent::__construct($id, $file);
}
}

View File

@ -447,42 +447,8 @@ function dbglog($msg,$header=''){
* @param string|null $deprecatedThing What is deprecated if not the current method
* @triggers INFO_DEPRECATION_LOG
*/
function dbg_deprecated($alternative = '', $deprecatedThing = null) {
global $conf;
global $EVENT_HANDLER;
if(!$conf['allowdebug'] && !$EVENT_HANDLER->hasHandlerForEvent('INFO_DEPRECATION_LOG')) {
// avoid any work if no one cares
return;
}
$backtrace = debug_backtrace();
array_shift($backtrace);
$self = $backtrace[0];
$call = $backtrace[1];
if ($deprecatedThing === null) {
$deprecatedThing = trim($self['class'] . '::' . $self['function'] . '()', ':');
}
$data = [
'trace' => $backtrace,
'alternative' => $alternative,
'called' => $deprecatedThing,
'caller' => trim($call['class'] . '::' . $call['function'] . '()', ':'),
'file' => $call['file'],
'line' => $call['line'],
];
$event = new Doku_Event('INFO_DEPRECATION_LOG', $data);
if($event->advise_before()) {
$msg = $event->data['called'] . ' is deprecated. It was called from ';
$msg .= $event->data['caller'] . ' in ' . $event->data['file'] . ':' . $event->data['line'];
if($event->data['alternative']) {
$msg .= ' ' . $event->data['alternative'] . ' should be used instead!';
}
dbglog($msg);
}
$event->advise_after();
function dbg_deprecated($alternative = '') {
\dokuwiki\Debug\DebugHelper::dbgDeprecatedFunction($alternative, 2);
}
/**