diff --git a/README.md b/README.md index 9c2ffbb..9d77d1c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # php-types + +[![Build Status](https://travis-ci.com/event-engine/php-types.svg?branch=master)](https://travis-ci.com/event-engine/php-types) +[![Coverage Status](https://coveralls.io/repos/github/event-engine/php-types/badge.svg?branch=master)](https://coveralls.io/github/event-engine/php-types?branch=master) + PHP 7.4 + psalm powered Immutable Types diff --git a/src/ImmutableBoolean.php b/src/ImmutableBoolean.php index 334f241..288953a 100644 --- a/src/ImmutableBoolean.php +++ b/src/ImmutableBoolean.php @@ -39,7 +39,7 @@ public static function fromBool(bool $value): self public function __construct(bool $value) { - if(isset($this->value)) { + if (isset($this->value)) { throw new BadMethodCallException(__METHOD__ . ' called on existing object!'); } @@ -58,11 +58,11 @@ public function __toString(): string public function equals($value): bool { - if(is_object($value) && method_exists($value, 'toBool')) { + if (is_object($value) && method_exists($value, 'toBool')) { return $this->value === $value->toBool(); } - if(is_bool($value)) { + if (is_bool($value)) { return $this->value === $value; } diff --git a/src/ImmutableFloat.php b/src/ImmutableFloat.php index c987af1..0e23e7f 100644 --- a/src/ImmutableFloat.php +++ b/src/ImmutableFloat.php @@ -32,14 +32,14 @@ trait ImmutableFloat * @param float $value * @return static */ - public static function fromString(float $value): self + public static function fromFloat(float $value): self { return new self($value); } public function __construct(float $value) { - if(isset($this->value)) { + if (isset($this->value)) { throw new BadMethodCallException(__METHOD__ . ' called on existing object!'); } @@ -58,11 +58,11 @@ public function __toString(): string public function equals($value): bool { - if(is_object($value) && method_exists($value, 'toFloat')) { + if (is_object($value) && method_exists($value, 'toFloat')) { return $this->value === $value->toFloat(); } - if(is_float($value)) { + if (is_float($value)) { return $this->value === $value; } diff --git a/src/ImmutableInteger.php b/src/ImmutableInteger.php index c2d20cd..e487632 100644 --- a/src/ImmutableInteger.php +++ b/src/ImmutableInteger.php @@ -39,7 +39,7 @@ public static function fromInt(int $value): self public function __construct(int $value) { - if(isset($this->value)) { + if (isset($this->value)) { throw new BadMethodCallException(__METHOD__ . ' called on existing object!'); } @@ -58,11 +58,11 @@ public function __toString(): string public function equals($value): bool { - if(is_object($value) && method_exists($value, 'toInt')) { + if (is_object($value) && method_exists($value, 'toInt')) { return $this->value === $value->toInt(); } - if(is_int($value)) { + if (is_int($value)) { return $this->value === $value; } diff --git a/src/ImmutableList.php b/src/ImmutableList.php index 8a2f89f..9dbbf2e 100644 --- a/src/ImmutableList.php +++ b/src/ImmutableList.php @@ -117,7 +117,7 @@ public function unshift(...$items): self */ public function pop(): self { - if(count($this->items) === 0) { + if (count($this->items) === 0) { return $this; } @@ -133,7 +133,7 @@ public function pop(): self */ public function shift(): self { - if(count($this->items) === 0) { + if (count($this->items) === 0) { return $this; } @@ -149,7 +149,7 @@ public function shift(): self */ public function first() { - if(count($this->items)) { + if (count($this->items)) { return $this->items[0]; } @@ -161,7 +161,7 @@ public function first() */ public function last() { - if(count($this->items)) { + if (count($this->items)) { return $this->items[count($this->items) - 1]; } @@ -177,7 +177,7 @@ public function filter(callable $filter): self $filteredItems = []; foreach ($this->items as $item) { - if($filter($item)) { + if ($filter($item)) { $filteredItems[] = $item; } } @@ -189,11 +189,11 @@ public function filter(callable $filter): self public function equals($other): bool { - if(is_object($other) && method_exists($other, 'toArray')) { + if (is_object($other) && method_exists($other, 'toArray')) { return $this->items == $other->toArray(); } - if(is_array($other)) { + if (is_array($other)) { return $this->items == $other; } @@ -267,7 +267,7 @@ private static function ensurePropTypeMapIsBuilt(): void self::$__propTypeMap = self::buildPropTypeMap(); } - if(null === self::$__arrayPropItemTypeMap) { + if (null === self::$__arrayPropItemTypeMap) { self::$__arrayPropItemTypeMap = self::arrayPropItemTypeMap(); } } diff --git a/src/ImmutableString.php b/src/ImmutableString.php index e257dce..4b0c129 100644 --- a/src/ImmutableString.php +++ b/src/ImmutableString.php @@ -40,7 +40,7 @@ public static function fromString(string $value): self public function __construct(string $value) { - if(isset($this->value)) { + if (isset($this->value)) { throw new BadMethodCallException(__METHOD__ . ' called on existing object!'); } @@ -59,11 +59,11 @@ public function __toString(): string public function equals($value): bool { - if(is_object($value) && method_exists($value, 'toString')) { + if (is_object($value) && method_exists($value, 'toString')) { return $this->value === $value->toString(); } - if(is_string($value)) { + if (is_string($value)) { return $this->value === $value; } diff --git a/tests/ImmutableBooleanTest.php b/tests/ImmutableBooleanTest.php index 605cb7a..3c8a748 100644 --- a/tests/ImmutableBooleanTest.php +++ b/tests/ImmutableBooleanTest.php @@ -34,7 +34,7 @@ public function it_can_be_created_from_boolean() /** * @test */ - public function it_equals_other_strings_with_same_value() + public function it_equals_other_booleans_with_same_value() { $access = Access::fromBool(true); $other = Access::fromBool(true); @@ -42,6 +42,7 @@ public function it_equals_other_strings_with_same_value() $this->assertTrue($access->equals($other)); $this->assertTrue($access->equals(true)); $this->assertFalse($access->equals(false)); + $this->assertFalse($access->equals('test')); } /** diff --git a/tests/ImmutableFloatTest.php b/tests/ImmutableFloatTest.php new file mode 100644 index 0000000..60e0a20 --- /dev/null +++ b/tests/ImmutableFloatTest.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +declare(strict_types=1); + +namespace EventEngineTest\Type; + +use BadMethodCallException; +use EventEngineTest\Type\Stub\Percentage; +use PHPUnit\Framework\TestCase; + +final class ImmutableFloatTest extends TestCase +{ + /** + * @test + */ + public function it_can_be_created_from_boolean() + { + $percentage = Percentage::fromFloat(0.5); + + $this->assertEquals(0.5, $percentage->value); + $this->assertEquals(0.5, $percentage->toFloat()); + $this->assertEquals('0.5', (string)$percentage); + } + + /** + * @test + */ + public function it_equals_other_floats_with_same_value() + { + $percentage = Percentage::fromFloat(0.5); + $other = Percentage::fromFloat(0.5); + + $this->assertTrue($percentage->equals($other)); + $this->assertTrue($percentage->equals(0.5)); + $this->assertFalse($percentage->equals(0.6)); + $this->assertFalse($percentage->equals('test')); + } + + /** + * @test + */ + public function it_prevents_double_constructor_calls() + { + $percentage = new Percentage(0.5); + + $this->expectException(BadMethodCallException::class); + + $percentage->__construct(0.6); + } +} diff --git a/tests/ImmutableIntegerTest.php b/tests/ImmutableIntegerTest.php index 29f3908..eff307d 100644 --- a/tests/ImmutableIntegerTest.php +++ b/tests/ImmutableIntegerTest.php @@ -40,6 +40,7 @@ public function it_equals_other_integers_with_same_value() $this->assertTrue($version->equals($other)); $this->assertTrue($version->equals(1)); $this->assertFalse($version->equals(2)); + $this->assertFalse($version->equals('test')); } /** diff --git a/tests/ImmutableStringTest.php b/tests/ImmutableStringTest.php index 8675aea..a23de0e 100644 --- a/tests/ImmutableStringTest.php +++ b/tests/ImmutableStringTest.php @@ -39,6 +39,7 @@ public function it_equals_other_strings_with_same_value() $this->assertTrue($username->equals($other)); $this->assertTrue($username->equals('Jane')); $this->assertFalse($username->equals('John')); + $this->assertFalse($username->equals(123)); } /** diff --git a/tests/Stub/ListWithoutTypeHintedConstructor.php b/tests/Stub/ListWithoutTypeHintedConstructor.php index 9e5cfbf..451bf5b 100644 --- a/tests/Stub/ListWithoutTypeHintedConstructor.php +++ b/tests/Stub/ListWithoutTypeHintedConstructor.php @@ -1,4 +1,11 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ declare(strict_types=1); namespace EventEngineTest\Type\Stub; diff --git a/tests/Stub/Percentage.php b/tests/Stub/Percentage.php new file mode 100644 index 0000000..3de148e --- /dev/null +++ b/tests/Stub/Percentage.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +declare(strict_types=1); + +namespace EventEngineTest\Type\Stub; + +use EventEngine\Type\ImmutableFloat; + +final class Percentage +{ + use ImmutableFloat; +}