Skip to content

Commit

Permalink
feat(chore) : strict type
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien Loyant committed Sep 19, 2024
1 parent 93497e2 commit e485250
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 101 deletions.
16 changes: 12 additions & 4 deletions SplBool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
22 changes: 13 additions & 9 deletions SplEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -49,6 +52,7 @@ final public function getConstList(bool $include_default = false)
if (!$include_default) {
unset($constants['__default']);
}

return $constants;
}
}
19 changes: 6 additions & 13 deletions SplFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
19 changes: 6 additions & 13 deletions SplInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
17 changes: 6 additions & 11 deletions SplString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
56 changes: 5 additions & 51 deletions SplType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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<mixed, mixed> $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<mixed, mixed>
*/
final public function __debugInfo()
{
return [
'__default' => $this->__default,
];
$this->__default = $initial_value ?? static::__default;
}
}
83 changes: 83 additions & 0 deletions SplTypeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/**
* Part of SplTypes package.
*
* (c) Adrien Loyant <[email protected]>
*
* 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<string,mixed>
*/
final public function __serialize(): array
{
return [
'__default' => $this->__default,
];
}

/**
* Unserialize object.
*
* @param array<string,mixed> $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<mixed,mixed> $properties
*
* @return SplType
*/
final public static function __set_state(array $properties): object
{
return new static($properties['__default']);
}

/**
* Dumping object.
*
* @return array<string,mixed>
*/
final public function __debugInfo(): array
{
return [
'__default' => $this->__default,
];
}
}

0 comments on commit e485250

Please sign in to comment.