From e4852504f19242f202e1a69314841189ff497f51 Mon Sep 17 00:00:00 2001 From: Adrien Loyant Date: Thu, 19 Sep 2024 12:36:11 +0200 Subject: [PATCH] feat(chore) : strict type --- SplBool.php | 16 +++++++--- SplEnum.php | 22 +++++++------ SplFloat.php | 19 ++++------- SplInt.php | 19 ++++------- SplString.php | 17 ++++------ SplType.php | 56 +++----------------------------- SplTypeTrait.php | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 131 insertions(+), 101 deletions(-) create mode 100644 SplTypeTrait.php diff --git a/SplBool.php b/SplBool.php index efb6991..7895f9a 100644 --- a/SplBool.php +++ b/SplBool.php @@ -9,14 +9,12 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace Ducks\Component\SplTypes; /** * The SplBool class is used to enforce strong typing of the bool type. - * - * @see SplBool http://php.net/manual/en/class.splbool.php - * - * @psalm-api */ class SplBool extends SplEnum { @@ -37,4 +35,14 @@ class SplBool extends SplEnum */ // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase public const true = true; + + /** + * {@inheritdoc} + * + * @param bool $initial_value + */ + public function __construct(bool $initial_value = self::__default) + { + parent::__construct($initial_value); + } } diff --git a/SplEnum.php b/SplEnum.php index 32eb14e..7acb0e4 100644 --- a/SplEnum.php +++ b/SplEnum.php @@ -9,30 +9,33 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace Ducks\Component\SplTypes; /** * SplEnum gives the ability to emulate and create enumeration objects natively in PHP. - * - * @see SplEnum http://php.net/manual/en/class.splenum.php - * - * @psalm-api */ abstract class SplEnum extends SplType { /** * {@inheritdoc} + * + * @param mixed $initial_value + * @param bool $strict + * + * @throws \UnexpectedValueException if incompatible type is given. */ public function __construct($initial_value = null, bool $strict = true) { - if (null === $initial_value) { - $initial_value = static::__default; - } + $initial_value ??= static::__default; + $class = new \ReflectionClass($this); - if (!in_array($initial_value, $class->getConstants(), $strict)) { + if (!\in_array($initial_value, $class->getConstants(), $strict)) { throw new \UnexpectedValueException('Value not a const in enum ' . $class->getShortName()); } - $this->__default = $initial_value; + + parent::__construct($initial_value); } /** @@ -49,6 +52,7 @@ final public function getConstList(bool $include_default = false) if (!$include_default) { unset($constants['__default']); } + return $constants; } } diff --git a/SplFloat.php b/SplFloat.php index 06bcce1..d9af212 100644 --- a/SplFloat.php +++ b/SplFloat.php @@ -9,14 +9,12 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace Ducks\Component\SplTypes; /** * The SplFloat class is used to enforce strong typing of the float type. - * - * @see http://php.net/manual/en/class.splfloat.php - * - * @psalm-api */ class SplFloat extends SplType { @@ -28,16 +26,11 @@ class SplFloat extends SplType /** * {@inheritdoc} + * + * @param float $initial_value */ - public function __construct($initial_value = self::__default, bool $strict = true) + public function __construct(float $initial_value = self::__default) { - parent::__construct($initial_value, $strict); - if (!$strict) { - $initial_value = (float) $initial_value; - } - if (!is_float($initial_value) && !is_int($initial_value)) { - throw new \UnexpectedValueException('Value not a float'); - } - $this->__default = $initial_value; + parent::__construct($initial_value); } } diff --git a/SplInt.php b/SplInt.php index 7000328..759ba86 100644 --- a/SplInt.php +++ b/SplInt.php @@ -9,14 +9,12 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace Ducks\Component\SplTypes; /** * The SplInt class is used to enforce strong typing of the integer type. - * - * @see SplInt http://php.net/manual/en/class.splint.php - * - * @psalm-api */ class SplInt extends SplType { @@ -28,16 +26,11 @@ class SplInt extends SplType /** * {@inheritdoc} + * + * @param int $initial_value */ - public function __construct($initial_value = self::__default, bool $strict = true) + public function __construct(int $initial_value = self::__default) { - parent::__construct($initial_value, $strict); - if (!$strict) { - $initial_value = (int) $initial_value; - } - if (!is_int($initial_value)) { - throw new \UnexpectedValueException('Value not an integer'); - } - $this->__default = $initial_value; + parent::__construct($initial_value); } } diff --git a/SplString.php b/SplString.php index 247b282..ecc8bf9 100644 --- a/SplString.php +++ b/SplString.php @@ -9,14 +9,14 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace Ducks\Component\SplTypes; /** * The SplString class is used to enforce strong typing of the string type. * * @see SplString http://php.net/manual/en/class.splstring.php - * - * @psalm-api */ class SplString extends SplType { @@ -28,16 +28,11 @@ class SplString extends SplType /** * {@inheritdoc} + * + * @param string $initial_value */ - public function __construct($initial_value = self::__default, bool $strict = true) + public function __construct(string $initial_value = self::__default) { - parent::__construct($initial_value, $strict); - if (!$strict) { - $initial_value = (string) $initial_value; - } - if (!is_string($initial_value)) { - throw new \UnexpectedValueException('Value not a string'); - } - $this->__default = $initial_value; + parent::__construct($initial_value); } } diff --git a/SplType.php b/SplType.php index 254a30e..fcef7e4 100644 --- a/SplType.php +++ b/SplType.php @@ -9,31 +9,25 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace Ducks\Component\SplTypes; /** * Parent class for all SPL types. * * @see SplType http://php.net/manual/en/class.spltype.php - * - * @psalm-api */ abstract class SplType { + use SplTypeTrait; + /** * Default value. */ // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase public const __default = null; - /** - * Internal enum value. - * - * @var mixed - */ - // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore - public $__default; - /** * Creates a new value of some type. * @@ -42,50 +36,10 @@ abstract class SplType * * @return void * - * @throws \UnexpectedValueException if incompatible type is given. - * * @phpstan-ignore-next-line */ public function __construct($initial_value = self::__default, bool $strict = true) { - if (null === $initial_value) { - $initial_value = static::__default; - } - $this->__default = $initial_value; - } - - /** - * Stringify object. - * - * @return string - */ - final public function __toString() - { - return (string) $this->__default; - } - - /** - * Export object. - * - * @param array $properties - * - * @return SplType - */ - final public static function __set_state(array $properties) - { - // @phpstan-ignore-next-line - return new static($properties['__default']); - } - - /** - * Dumping object (php > 5.6.0). - * - * @return array - */ - final public function __debugInfo() - { - return [ - '__default' => $this->__default, - ]; + $this->__default = $initial_value ?? static::__default; } } diff --git a/SplTypeTrait.php b/SplTypeTrait.php new file mode 100644 index 0000000..984613b --- /dev/null +++ b/SplTypeTrait.php @@ -0,0 +1,83 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Ducks\Component\SplTypes; + +/** + * Trait used for magic + */ +trait SplTypeTrait +{ + /** + * Internal enum value. + * + * @var mixed + */ + // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore + protected $__default; + + /** + * Serialize object. + * + * @return array + */ + final public function __serialize(): array + { + return [ + '__default' => $this->__default, + ]; + } + + /** + * Unserialize object. + * + * @param array $data + * @return void + */ + final public function __unserialize(array $data): void + { + $this->__default = $data['__default']; + } + + /** + * Stringify object. + * + * @return string + */ + final public function __toString(): string + { + return (string) $this->__default; + } + + /** + * Export object. + * + * @param array $properties + * + * @return SplType + */ + final public static function __set_state(array $properties): object + { + return new static($properties['__default']); + } + + /** + * Dumping object. + * + * @return array + */ + final public function __debugInfo(): array + { + return [ + '__default' => $this->__default, + ]; + } +}