From d173dfeb57ab9725088a59af8be3375a009b0f85 Mon Sep 17 00:00:00 2001 From: Fedir Zinchuk Date: Wed, 20 Sep 2023 18:19:32 +0300 Subject: [PATCH] [4.4][RFC] Rename Event pre-processing methods from set/get to onSet/onGet (#41722) * Rename Event pre-processing events from set/get to onSet/onGet * Apply suggestions from code review Co-authored-by: Richard Fath --------- Co-authored-by: Richard Fath Co-authored-by: Allon Moritz --- libraries/src/Event/AbstractEvent.php | 52 +++++++++++++++++++++------ 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/libraries/src/Event/AbstractEvent.php b/libraries/src/Event/AbstractEvent.php index f128ac8095995..740a855a8c463 100644 --- a/libraries/src/Event/AbstractEvent.php +++ b/libraries/src/Event/AbstractEvent.php @@ -122,9 +122,10 @@ public function __construct(string $name, array $arguments = []) } /** - * Get an event argument value. It will use a getter method if one exists. The getters have the signature: + * Get an event argument value. + * It will use a pre-processing method if one exists. The method has the signature: * - * get($value): mixed + * onGet($value): mixed * * where: * @@ -157,21 +158,37 @@ public function getArgument($name, $default = null) ); } - $methodName = 'get' . ucfirst($name); + // Look for the method for the value pre-processing/validation + $ucfirst = ucfirst($name); + $methodName1 = 'onGet' . $ucfirst; + $methodName2 = 'get' . $ucfirst; $value = parent::getArgument($name, $default); - if (method_exists($this, $methodName)) { - return $this->{$methodName}($value); + if (method_exists($this, $methodName1)) { + return $this->{$methodName1}($value); + } elseif (method_exists($this, $methodName2)) { + @trigger_error( + sprintf( + 'Use method "%s" for value pre-processing is deprecated, and will not work in Joomla 6. Use "%s" instead. Event %s', + $methodName2, + $methodName1, + \get_class($this) + ), + E_USER_DEPRECATED + ); + + return $this->{$methodName2}($value); } return $value; } /** - * Add argument to event. It will use a setter method if one exists. The setters have the signature: + * Add argument to event. + * It will use a pre-processing method if one exists. The method has the signature: * - * set($value): mixed + * onSet($value): mixed * * where: * @@ -204,10 +221,25 @@ public function setArgument($name, $value) ); } - $methodName = 'set' . ucfirst($name); + // Look for the method for the value pre-processing/validation + $ucfirst = ucfirst($name); + $methodName1 = 'onSet' . $ucfirst; + $methodName2 = 'set' . $ucfirst; + + if (method_exists($this, $methodName1)) { + $value = $this->{$methodName1}($value); + } elseif (method_exists($this, $methodName2)) { + @trigger_error( + sprintf( + 'Use method "%s" for value pre-processing is deprecated, and will not work in Joomla 6. Use "%s" instead. Event %s', + $methodName2, + $methodName1, + \get_class($this) + ), + E_USER_DEPRECATED + ); - if (method_exists($this, $methodName)) { - $value = $this->{$methodName}($value); + $value = $this->{$methodName2}($value); } return parent::setArgument($name, $value);