Update phpDoc for Getter and Setter traits

This commit is contained in:
Sebastian Meyer 2024-02-11 17:33:36 +01:00
parent 3f666d724d
commit 02a108aac2
3 changed files with 25 additions and 15 deletions

View File

@ -28,6 +28,15 @@ use InvalidArgumentException;
/** /**
* Reads data from inaccessible properties by using magic methods. * Reads data from inaccessible properties by using magic methods.
* *
* To make a `protected` or `private` property readable, provide a method named
* `_magicGet{Property}()` which handles the reading. Replace `{Property}` in
* the method's name with the name of the actual property (with an uppercase
* first letter).
*
* > Example: If the property is named `$fooBar`, the "magic" method has to be
* > `_magicGetFooBar()`. This method is then called when `$fooBar` is read
* > from outside the class context.
*
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com> * @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
* @package Basics\Traits * @package Basics\Traits
*/ */
@ -40,17 +49,14 @@ trait Getter
* *
* @return mixed The class property's current value * @return mixed The class property's current value
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException if the property does not exist
* *
* @internal * @internal
*/ */
public function __get(string $property): mixed public function __get(string $property): mixed
{ {
$method = 'magicGet' . ucfirst($property); $method = '_magicGet' . ucfirst($property);
if ( if (property_exists(static::class, $property) && method_exists(static::class, $method)) {
property_exists(static::class, $property)
&& method_exists(static::class, $method)
) {
return $this->$method(); return $this->$method();
} else { } else {
throw new InvalidArgumentException( throw new InvalidArgumentException(

View File

@ -28,6 +28,15 @@ use InvalidArgumentException;
/** /**
* Writes data to inaccessible properties by using magic methods. * Writes data to inaccessible properties by using magic methods.
* *
* To make a `protected` or `private` property writable, provide a method named
* `_magicSet{Property}()` which handles the writing. Replace `{Property}` in
* the method's name with the name of the actual property (with an uppercase
* first letter).
*
* > Example: If the property is named `$fooBar`, the "magic" method has to be
* > `_magicSetFooBar()`. This method is then called when `$fooBar` is written
* > to from outside the class context.
*
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com> * @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
* @package Basics\Traits * @package Basics\Traits
*/ */
@ -41,17 +50,14 @@ trait Setter
* *
* @return void * @return void
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException if the property does not exist
* *
* @internal * @internal
*/ */
public function __set(string $property, mixed $value): void public function __set(string $property, mixed $value): void
{ {
$method = 'magicSet' . ucfirst($property); $method = '_magicSet' . ucfirst($property);
if ( if (property_exists(static::class, $property) && method_exists(static::class, $method)) {
property_exists(static::class, $property)
&& method_exists(static::class, $method)
) {
$this->$method($value); $this->$method($value);
} else { } else {
throw new InvalidArgumentException( throw new InvalidArgumentException(
@ -71,8 +77,6 @@ trait Setter
* *
* @return void * @return void
* *
* @throws InvalidArgumentException
*
* @internal * @internal
*/ */
public function __unset(string $property): void public function __unset(string $property): void

View File

@ -80,7 +80,7 @@ trait Singleton
* *
* @return void * @return void
* *
* @throws LogicException * @throws LogicException if trying to clone `$this`
* *
* @internal * @internal
*/ */