Skip to content

Commit

Permalink
MDL-80005 core: Emit deprecation notices for params
Browse files Browse the repository at this point in the history
Most params are formally deprecated here. This was originally planned
for MDL-80042, but I realised that having an emit, and final param is a
very useful option going forward.

This patch also moves the is_deprecated(), and related methods to the
\core\deprecated attribute.
  • Loading branch information
andrewnicols committed Jan 17, 2024
1 parent a61658d commit b05fc42
Show file tree
Hide file tree
Showing 12 changed files with 647 additions and 10 deletions.
2 changes: 1 addition & 1 deletion enrol/externallib.php
Expand Up @@ -211,7 +211,7 @@ public static function get_enrolled_users_with_capability_returns() {
'lastname' => new external_value(PARAM_NOTAGS, 'The family name of the user', VALUE_OPTIONAL),
'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'),
'email' => new external_value(PARAM_TEXT, 'Email address', VALUE_OPTIONAL),
'address' => new external_value(PARAM_MULTILANG, 'Postal address', VALUE_OPTIONAL),
'address' => new external_value(PARAM_TEXT, 'Postal address', VALUE_OPTIONAL),
'phone1' => new external_value(PARAM_NOTAGS, 'Phone 1', VALUE_OPTIONAL),
'phone2' => new external_value(PARAM_NOTAGS, 'Phone 2', VALUE_OPTIONAL),
'department' => new external_value(PARAM_TEXT, 'department', VALUE_OPTIONAL),
Expand Down
53 changes: 53 additions & 0 deletions lib/classes/deprecated.php
@@ -0,0 +1,53 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core;

use Attribute;

/**
* Attribute to describe a deprecated item.
*
* @package core
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[Attribute]
class deprecated {
/**
* A deprecated item.
*
* This attribute can be applied to any function, class, method, constant, property, enum, etc.
*
* Note: The mere presence of the attribute does not do anything. It must be checked by some part of the code.
*
* @param mixed $descriptor A brief descriptor of the thing that was deprecated.
* @param null|string $since When it was deprecated
* @param null|string $reason Why it was deprecated
* @param null|string $replacement Any replacement for the deprecated thing
* @param null|string $mdl Link to the Moodle Tracker issue for more information
*/
public function __construct(
public readonly mixed $descriptor,
public readonly ?string $since = null,
public readonly ?string $reason = null,
public readonly ?string $replacement = null,
public readonly ?string $mdl = null,
public readonly bool $final = false,
public readonly bool $emit = true,
) {
}
}
203 changes: 203 additions & 0 deletions lib/classes/deprecation.php
@@ -0,0 +1,203 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core;

/**
* Deprecation utility.
*
* @package core
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class deprecation {
/**
* Get the attribute from a reference.
*
* The reference can be:
* - a string, in which case it will be checked for a function, class, method, property, constant, or enum.
* - an array
* - an instantiated object, in which case the object will be checked for a class, method, property, or constant.
*
* @param array|string|object $reference A reference to a potentially deprecated thing.
* @return null|deprecated
*/
public static function from(array|string|object $reference): ?deprecated {
if (is_string($reference)) {
if (str_contains($reference, '::')) {
// The reference is a string but it looks to be in the format `object::item`.
return self::from(explode('::', $reference));
}

if (class_exists($reference)) {
// The reference looks to be a class name.
return self::from([$reference]);
}

if (function_exists($reference)) {
// The reference looks to be a global function.
$ref = new \ReflectionFunction($reference);
if ($attributes = $ref->getAttributes(deprecated::class)) {
return $attributes[0]->newInstance();
}
}

return null;
}

if (is_object($reference)) {
// The reference is an object. Normalise and check again.
return self::from([$reference]);
}

if (is_array($reference) && count($reference)) {
if (is_object($reference[0])) {
$rc = new \ReflectionObject($reference[0]);

if ($rc->isEnum() && $reference[0]->name) {
// Enums can be passed via ::from([enum::NAME]).
// In this case they will have a 'name', which must exist.
return self::from_reflected_object($rc, $reference[0]->name);
}
return self::from_reflected_object($rc, $reference[1] ?? null);
}

if (is_string($reference[0]) && class_exists($reference[0])) {
$rc = new \ReflectionClass($reference[0]);
return self::from_reflected_object($rc, $reference[1] ?? null);
}

// The reference is an array, but it's not an object or a class that currently exists.
return null;
}
}

/**
* Check if a reference is deprecated.
*
* @param array|string|object $reference
* @return bool
*/
public static function is_deprecated(array|string|object $reference): bool {
return self::from($reference) !== null;
}

/**
* Emit a deprecation notice if the reference is deprecated.
*
* @param array|string|object $reference
*/
public static function emit_deprecation_if_present(array|string|object $reference): void {
if ($attribute = self::from($reference)) {
self::emit_deprecation_notice($attribute);
}
}

/**
* Fetch a deprecation attribute from a reflected object.
*
* @param \ReflectionClass $rc The reflected object
* @param null|string $name The name of the thing to check for deprecation
* @return null|deprecated
*/
protected static function from_reflected_object(
\ReflectionClass $rc,
?string $name,
): ?deprecated {
if ($name === null) {
// No name specified. This may be a deprecated class.
if ($attributes = $rc->getAttributes(deprecated::class)) {
return $attributes[0]->newInstance();
}
return null;
}

if ($rc->hasConstant($name)) {
// This class has a constant with the specified name.
// Note: This also applies to enums.
$ref = $rc->getReflectionConstant($name);
if ($attributes = $ref->getAttributes(deprecated::class)) {
return $attributes[0]->newInstance();
}
}

if ($rc->hasMethod($name)) {
// This class has a method with the specified name.
$ref = $rc->getMethod($name);
if ($attributes = $ref->getAttributes(deprecated::class)) {
return $attributes[0]->newInstance();
}
}

if ($rc->hasProperty($name)) {
// This class has a property with the specified name.
$ref = $rc->getProperty($name);
if ($attributes = $ref->getAttributes(deprecated::class)) {
return $attributes[0]->newInstance();
}
}

return null;
}

/**
* Get a string describing the deprecation.
*
* @param deprecated $attribute
* @return string
*/
public static function get_deprecation_string(deprecated $attribute): string {
$output = "Deprecation: {$attribute->descriptor} has been deprecated";
if ($attribute->since) {
$output .= " since {$attribute->since}";
}

$output .= ".";

if ($attribute->reason) {
$output .= " {$attribute->reason}.";
}

if ($attribute->replacement) {
$output .= " Use {$attribute->replacement} instead.";
}

if ($attribute->mdl) {
$output .= " See {$attribute->mdl} for more information.";
}

return $output;
}

/**
* Emit the relevant deprecation notice.
*
* @param deprecated $attribute
*/
public static function emit_deprecation_notice(deprecated $attribute): void {
if (!$attribute->emit) {
return;
}

$message = self::get_deprecation_string($attribute);

if ($attribute->final) {
throw new \coding_exception($message);
}

debugging($message, DEBUG_DEVELOPER);
}
}
12 changes: 6 additions & 6 deletions lib/classes/hub/api.php
Expand Up @@ -171,9 +171,9 @@ public static function get_hub_info() {
*
* Parameter $options may have any of these fields:
* [
* 'ids' => new external_multiple_structure(new external_value(PARAM_INTEGER, 'id of a course in the hub course
* 'ids' => new external_multiple_structure(new external_value(PARAM_INT, 'id of a course in the hub course
* directory'), 'ids of course', VALUE_OPTIONAL),
* 'sitecourseids' => new external_multiple_structure(new external_value(PARAM_INTEGER, 'id of a course in the
* 'sitecourseids' => new external_multiple_structure(new external_value(PARAM_INT, 'id of a course in the
* site'), 'ids of course in the site', VALUE_OPTIONAL),
* 'coverage' => new external_value(PARAM_TEXT, 'coverage', VALUE_OPTIONAL),
* 'licenceshortname' => new external_value(PARAM_ALPHANUMEXT, 'licence short name', VALUE_OPTIONAL),
Expand All @@ -185,7 +185,7 @@ public static function get_hub_info() {
* ratingaverage', VALUE_OPTIONAL),
* 'givememore' => new external_value(PARAM_INT, 'next range of result - range size being set by the hub
* server ', VALUE_OPTIONAL),
* 'allsitecourses' => new external_value(PARAM_INTEGER,
* 'allsitecourses' => new external_value(PARAM_INT,
* 'if 1 return all not visible and visible courses whose siteid is the site
* matching token. Only courses of this site are returned.
* givememore parameter is ignored if this param = 1.
Expand All @@ -194,7 +194,7 @@ public static function get_hub_info() {
*
* Each course in the returned array of courses will have fields:
* [
* 'id' => new external_value(PARAM_INTEGER, 'id'),
* 'id' => new external_value(PARAM_INT, 'id'),
* 'fullname' => new external_value(PARAM_TEXT, 'course name'),
* 'shortname' => new external_value(PARAM_TEXT, 'course short name'),
* 'description' => new external_value(PARAM_TEXT, 'course description'),
Expand All @@ -211,7 +211,7 @@ public static function get_hub_info() {
* 'audience' => new external_value(PARAM_ALPHA, 'audience'),
* 'educationallevel' => new external_value(PARAM_ALPHA, 'educational level'),
* 'creatornotes' => new external_value(PARAM_RAW, 'creator notes'),
* 'creatornotesformat' => new external_value(PARAM_INTEGER, 'notes format'),
* 'creatornotesformat' => new external_value(PARAM_INT, 'notes format'),
* 'demourl' => new external_value(PARAM_URL, 'demo URL', VALUE_OPTIONAL),
* 'courseurl' => new external_value(PARAM_URL, 'course URL', VALUE_OPTIONAL),
* 'backupsize' => new external_value(PARAM_INT, 'course backup size in bytes', VALUE_OPTIONAL),
Expand Down Expand Up @@ -305,7 +305,7 @@ public static function unregister_courses($courseids) {
* 'audience' => new external_value(PARAM_ALPHA, 'audience'),
* 'educationallevel' => new external_value(PARAM_ALPHA, 'educational level'),
* 'creatornotes' => new external_value(PARAM_RAW, 'creator notes'),
* 'creatornotesformat' => new external_value(PARAM_INTEGER, 'notes format'),
* 'creatornotesformat' => new external_value(PARAM_INT, 'notes format'),
* 'demourl' => new external_value(PARAM_URL, 'demo URL', VALUE_OPTIONAL),
* 'courseurl' => new external_value(PARAM_URL, 'course URL', VALUE_OPTIONAL),
* 'enrollable' => new external_value(PARAM_BOOL, 'is the course enrollable', VALUE_DEFAULT, 0),
Expand Down

0 comments on commit b05fc42

Please sign in to comment.