Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
frankvanhest committed Jan 26, 2024
1 parent e2e373d commit f2f205e
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 77 deletions.
8 changes: 4 additions & 4 deletions src/Abstracts/BooleanValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ final protected function __construct(private bool $value)
{
}

final public function equals(?ValueObject $valueObject): bool
final public function asBoolean(): bool
{
return $valueObject instanceof static && $valueObject->asBoolean() === $this->value;
return $this->value;
}

final public function asBoolean(): bool
final public function equals(?ValueObject $valueObject): bool
{
return $this->value;
return $valueObject instanceof static && $valueObject->asBoolean() === $this->value;
}

final public static function fromBoolean(bool $value): static
Expand Down
11 changes: 6 additions & 5 deletions src/Abstracts/FloatValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use FrankVanHest\ValueObjects\Interfaces\FloatValueObject as FloatValueObjectInterface;
use FrankVanHest\ValueObjects\Interfaces\ValueObject;
use Throwable;

abstract readonly class FloatValueObject implements FloatValueObjectInterface
{
Expand All @@ -14,14 +15,14 @@ final protected function __construct(private float $value)
$this->assert($this->value);
}

final public function equals(?ValueObject $valueObject): bool
final public function asFloat(): float
{
return $valueObject instanceof static && $valueObject->asFloat() === $this->value;
return $this->value;
}

final public function asFloat(): float
final public function equals(?ValueObject $valueObject): bool
{
return $this->value;
return $valueObject instanceof static && $valueObject->asFloat() === $this->value;
}

final public static function fromFloat(float $value): static
Expand All @@ -30,7 +31,7 @@ final public static function fromFloat(float $value): static
}

/**
* @throws \Throwable When the float value does not match the requirements
* @throws Throwable When the float value does not match the requirements
*/
abstract protected function assert(float $value): void;
}
11 changes: 6 additions & 5 deletions src/Abstracts/IntegerValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use FrankVanHest\ValueObjects\Interfaces\IntegerValueObject as IntegerValueObjectInterface;
use FrankVanHest\ValueObjects\Interfaces\ValueObject;
use Throwable;

abstract readonly class IntegerValueObject implements IntegerValueObjectInterface
{
Expand All @@ -14,14 +15,14 @@ final private function __construct(private int $value)
$this->assert($this->value);
}

final public function equals(?ValueObject $valueObject): bool
final public function asInteger(): int
{
return $valueObject instanceof static && $valueObject->asInteger() === $this->value;
return $this->value;
}

final public function asInteger(): int
final public function equals(?ValueObject $valueObject): bool
{
return $this->value;
return $valueObject instanceof static && $valueObject->asInteger() === $this->value;
}

final public static function fromInteger(int $value): static
Expand All @@ -30,7 +31,7 @@ final public static function fromInteger(int $value): static
}

/**
* @throws \Throwable When the integer value does not match the requirements
* @throws Throwable When the integer value does not match the requirements
*/
abstract protected function assert(int $value): void;
}
3 changes: 2 additions & 1 deletion src/Abstracts/StringValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use FrankVanHest\ValueObjects\Interfaces\StringValueObject as StringValueObjectInterface;
use FrankVanHest\ValueObjects\Interfaces\ValueObject;
use Throwable;

abstract readonly class StringValueObject implements StringValueObjectInterface
{
Expand All @@ -30,7 +31,7 @@ final public static function fromString(string $value): static
}

/**
* @throws \Throwable When the string value does not match the requirements
* @throws Throwable When the string value does not match the requirements
*/
abstract protected function assert(string $value): void;
}
7 changes: 6 additions & 1 deletion src/Exceptions/DontUseMagicMethodCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

namespace FrankVanHest\ValueObjects\Exceptions;

final class DontUseMagicMethodCall extends \LogicException
use LogicException;

final class DontUseMagicMethodCall extends LogicException
{
/**
* @param class-string $class
*/
public function __construct(string $class)
{
parent::__construct(sprintf('Don\t use magic method __call in class %s', $class));
Expand Down
7 changes: 6 additions & 1 deletion src/Exceptions/DontUseMagicMethodCallStatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

namespace FrankVanHest\ValueObjects\Exceptions;

final class DontUseMagicMethodCallStatic extends \LogicException
use LogicException;

final class DontUseMagicMethodCallStatic extends LogicException
{
/**
* @param class-string $class
*/
public function __construct(string $class)
{
parent::__construct(sprintf('Don\t use magic method __callStatic in class %s', $class));
Expand Down
7 changes: 6 additions & 1 deletion src/Exceptions/DontUseMagicMethodGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

namespace FrankVanHest\ValueObjects\Exceptions;

final class DontUseMagicMethodGet extends \LogicException
use LogicException;

final class DontUseMagicMethodGet extends LogicException
{
/**
* @param class-string $class
*/
public function __construct(string $class)
{
parent::__construct(sprintf('Don\t use magic method __get in class %s', $class));
Expand Down
7 changes: 6 additions & 1 deletion src/Exceptions/DontUseMagicMethodIsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

namespace FrankVanHest\ValueObjects\Exceptions;

final class DontUseMagicMethodIsset extends \LogicException
use LogicException;

final class DontUseMagicMethodIsset extends LogicException
{
/**
* @param class-string $class
*/
public function __construct(string $class)
{
parent::__construct(sprintf('Don\t use magic method __isset in class %s', $class));
Expand Down
7 changes: 6 additions & 1 deletion src/Exceptions/DontUseMagicMethodSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

namespace FrankVanHest\ValueObjects\Exceptions;

final class DontUseMagicMethodSet extends \LogicException
use LogicException;

final class DontUseMagicMethodSet extends LogicException
{
/**
* @param class-string $class
*/
public function __construct(string $class)
{
parent::__construct(sprintf('Don\t use magic method __set in class %s', $class));
Expand Down
7 changes: 6 additions & 1 deletion src/Exceptions/DontUseMagicMethodUnset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

namespace FrankVanHest\ValueObjects\Exceptions;

final class DontUseMagicMethodUnset extends \LogicException
use LogicException;

final class DontUseMagicMethodUnset extends LogicException
{
/**
* @param class-string $class
*/
public function __construct(string $class)
{
parent::__construct(sprintf('Don\t use magic method __unset in class %s', $class));
Expand Down
16 changes: 9 additions & 7 deletions tests/Abstracts/BooleanValueObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
use FrankVanHest\ValueObjects\Interfaces\ValueObject;
use FrankVanHest\ValueObjects\Tests\Dummies\AreWeGreat;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionMethod;

final class BooleanValueObjectTest extends TestCase
{
public function testClassIsReadonly(): void
{
self::assertTrue((new \ReflectionClass(BooleanValueObject::class))->isReadOnly());
self::assertTrue((new ReflectionClass(BooleanValueObject::class))->isReadOnly());
}

public function testEquals(): void
Expand Down Expand Up @@ -121,11 +123,11 @@ public function testPreventMagicUnset(): void

public function testPreventedMagicMethodsAreFinal(): void
{
self::assertTrue((new \ReflectionMethod(BooleanValueObject::class, '__call'))->isFinal());
self::assertTrue((new \ReflectionMethod(BooleanValueObject::class, '__callStatic'))->isFinal());
self::assertTrue((new \ReflectionMethod(BooleanValueObject::class, '__get'))->isFinal());
self::assertTrue((new \ReflectionMethod(BooleanValueObject::class, '__isset'))->isFinal());
self::assertTrue((new \ReflectionMethod(BooleanValueObject::class, '__set'))->isFinal());
self::assertTrue((new \ReflectionMethod(BooleanValueObject::class, '__unset'))->isFinal());
self::assertTrue((new ReflectionMethod(BooleanValueObject::class, '__call'))->isFinal());
self::assertTrue((new ReflectionMethod(BooleanValueObject::class, '__callStatic'))->isFinal());
self::assertTrue((new ReflectionMethod(BooleanValueObject::class, '__get'))->isFinal());
self::assertTrue((new ReflectionMethod(BooleanValueObject::class, '__isset'))->isFinal());
self::assertTrue((new ReflectionMethod(BooleanValueObject::class, '__set'))->isFinal());
self::assertTrue((new ReflectionMethod(BooleanValueObject::class, '__unset'))->isFinal());
}
}
16 changes: 9 additions & 7 deletions tests/Abstracts/FloatValueObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
use FrankVanHest\ValueObjects\Interfaces\ValueObject;
use FrankVanHest\ValueObjects\Tests\Dummies\FloatGreaterThanZeroPointOne;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionMethod;
use Throwable;

final class FloatValueObjectTest extends TestCase
{
public function testClassIsReadonly(): void
{
self::assertTrue((new \ReflectionClass(FloatValueObject::class))->isReadOnly());
self::assertTrue((new ReflectionClass(FloatValueObject::class))->isReadOnly());
}

public function testEquals(): void
Expand Down Expand Up @@ -133,11 +135,11 @@ public function testPreventMagicUnset(): void

public function testPreventedMagicMethodsAreFinal(): void
{
self::assertTrue((new \ReflectionMethod(FloatValueObject::class, '__call'))->isFinal());
self::assertTrue((new \ReflectionMethod(FloatValueObject::class, '__callStatic'))->isFinal());
self::assertTrue((new \ReflectionMethod(FloatValueObject::class, '__get'))->isFinal());
self::assertTrue((new \ReflectionMethod(FloatValueObject::class, '__isset'))->isFinal());
self::assertTrue((new \ReflectionMethod(FloatValueObject::class, '__set'))->isFinal());
self::assertTrue((new \ReflectionMethod(FloatValueObject::class, '__unset'))->isFinal());
self::assertTrue((new ReflectionMethod(FloatValueObject::class, '__call'))->isFinal());
self::assertTrue((new ReflectionMethod(FloatValueObject::class, '__callStatic'))->isFinal());
self::assertTrue((new ReflectionMethod(FloatValueObject::class, '__get'))->isFinal());
self::assertTrue((new ReflectionMethod(FloatValueObject::class, '__isset'))->isFinal());
self::assertTrue((new ReflectionMethod(FloatValueObject::class, '__set'))->isFinal());
self::assertTrue((new ReflectionMethod(FloatValueObject::class, '__unset'))->isFinal());
}
}
16 changes: 9 additions & 7 deletions tests/Abstracts/IntegerValueObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
use FrankVanHest\ValueObjects\Interfaces\ValueObject;
use FrankVanHest\ValueObjects\Tests\Dummies\IntegerGreaterThanZero;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionMethod;
use Throwable;

final class IntegerValueObjectTest extends TestCase
{
public function testClassIsReadonly(): void
{
self::assertTrue((new \ReflectionClass(IntegerValueObject::class))->isReadOnly());
self::assertTrue((new ReflectionClass(IntegerValueObject::class))->isReadOnly());
}

public function testEquals(): void
Expand Down Expand Up @@ -131,11 +133,11 @@ public function testPreventMagicUnset(): void

public function testPreventedMagicMethodsAreFinal(): void
{
self::assertTrue((new \ReflectionMethod(IntegerValueObject::class, '__call'))->isFinal());
self::assertTrue((new \ReflectionMethod(IntegerValueObject::class, '__callStatic'))->isFinal());
self::assertTrue((new \ReflectionMethod(IntegerValueObject::class, '__get'))->isFinal());
self::assertTrue((new \ReflectionMethod(IntegerValueObject::class, '__isset'))->isFinal());
self::assertTrue((new \ReflectionMethod(IntegerValueObject::class, '__set'))->isFinal());
self::assertTrue((new \ReflectionMethod(IntegerValueObject::class, '__unset'))->isFinal());
self::assertTrue((new ReflectionMethod(IntegerValueObject::class, '__call'))->isFinal());
self::assertTrue((new ReflectionMethod(IntegerValueObject::class, '__callStatic'))->isFinal());
self::assertTrue((new ReflectionMethod(IntegerValueObject::class, '__get'))->isFinal());
self::assertTrue((new ReflectionMethod(IntegerValueObject::class, '__isset'))->isFinal());
self::assertTrue((new ReflectionMethod(IntegerValueObject::class, '__set'))->isFinal());
self::assertTrue((new ReflectionMethod(IntegerValueObject::class, '__unset'))->isFinal());
}
}
16 changes: 9 additions & 7 deletions tests/Abstracts/StringValueObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
use FrankVanHest\ValueObjects\Interfaces\ValueObject;
use FrankVanHest\ValueObjects\Tests\Dummies\NotEmptyString;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionMethod;
use Throwable;

final class StringValueObjectTest extends TestCase
{
public function testClassIsReadonly(): void
{
self::assertTrue((new \ReflectionClass(StringValueObject::class))->isReadOnly());
self::assertTrue((new ReflectionClass(StringValueObject::class))->isReadOnly());
}

public function testEquals(): void
Expand Down Expand Up @@ -127,11 +129,11 @@ public function testPreventMagicUnset(): void

public function testPreventedMagicMethodsAreFinal(): void
{
self::assertTrue((new \ReflectionMethod(StringValueObject::class, '__call'))->isFinal());
self::assertTrue((new \ReflectionMethod(StringValueObject::class, '__callStatic'))->isFinal());
self::assertTrue((new \ReflectionMethod(StringValueObject::class, '__get'))->isFinal());
self::assertTrue((new \ReflectionMethod(StringValueObject::class, '__isset'))->isFinal());
self::assertTrue((new \ReflectionMethod(StringValueObject::class, '__set'))->isFinal());
self::assertTrue((new \ReflectionMethod(StringValueObject::class, '__unset'))->isFinal());
self::assertTrue((new ReflectionMethod(StringValueObject::class, '__call'))->isFinal());
self::assertTrue((new ReflectionMethod(StringValueObject::class, '__callStatic'))->isFinal());
self::assertTrue((new ReflectionMethod(StringValueObject::class, '__get'))->isFinal());
self::assertTrue((new ReflectionMethod(StringValueObject::class, '__isset'))->isFinal());
self::assertTrue((new ReflectionMethod(StringValueObject::class, '__set'))->isFinal());
self::assertTrue((new ReflectionMethod(StringValueObject::class, '__unset'))->isFinal());
}
}
18 changes: 9 additions & 9 deletions tests/Factories/FloatValueObjectFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@

final class FloatValueObjectFactoryTest extends TestCase
{
public function testFactoryWithoutValueAlteration(): void
{
$valueObject = FloatValueObjectFactory::create(FloatGreaterThanZeroPointOne::class, 0.2);
self::assertInstanceOf(
FloatGreaterThanZeroPointOne::class,
$valueObject
);
}

public function testFactoryWithValueAlteration(): void
{
$valueModifierFirst = new AddFloatValue(0.2);
Expand All @@ -36,4 +27,13 @@ public function testFactoryWithValueAlteration(): void
);
self::assertSame(0.7, $valueObject->asFloat());
}

public function testFactoryWithoutValueAlteration(): void
{
$valueObject = FloatValueObjectFactory::create(FloatGreaterThanZeroPointOne::class, 0.2);
self::assertInstanceOf(
FloatGreaterThanZeroPointOne::class,
$valueObject
);
}
}
18 changes: 9 additions & 9 deletions tests/Factories/IntegerValueObjectFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@

final class IntegerValueObjectFactoryTest extends TestCase
{
public function testFactoryWithoutValueAlteration(): void
{
$valueObject = IntegerValueObjectFactory::create(IntegerGreaterThanZero::class, 2);
self::assertInstanceOf(
IntegerGreaterThanZero::class,
$valueObject
);
}

public function testFactoryWithValueAlteration(): void
{
$valueModifierFirst = new MultiplyIntegerValue(2);
Expand All @@ -36,4 +27,13 @@ public function testFactoryWithValueAlteration(): void
);
self::assertSame(8, $valueObject->asInteger());
}

public function testFactoryWithoutValueAlteration(): void
{
$valueObject = IntegerValueObjectFactory::create(IntegerGreaterThanZero::class, 2);
self::assertInstanceOf(
IntegerGreaterThanZero::class,
$valueObject
);
}
}
Loading

0 comments on commit f2f205e

Please sign in to comment.