Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions src/ImmutableBoolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}

Expand All @@ -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;
}

Expand Down
8 changes: 4 additions & 4 deletions src/ImmutableFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}

Expand All @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/ImmutableInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}

Expand All @@ -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;
}

Expand Down
16 changes: 8 additions & 8 deletions src/ImmutableList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -133,7 +133,7 @@ public function pop(): self
*/
public function shift(): self
{
if(count($this->items) === 0) {
if (count($this->items) === 0) {
return $this;
}

Expand All @@ -149,7 +149,7 @@ public function shift(): self
*/
public function first()
{
if(count($this->items)) {
if (count($this->items)) {
return $this->items[0];
}

Expand All @@ -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];
}

Expand All @@ -177,7 +177,7 @@ public function filter(callable $filter): self
$filteredItems = [];

foreach ($this->items as $item) {
if($filter($item)) {
if ($filter($item)) {
$filteredItems[] = $item;
}
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ImmutableString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}

Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/ImmutableBooleanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ 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);

$this->assertTrue($access->equals($other));
$this->assertTrue($access->equals(true));
$this->assertFalse($access->equals(false));
$this->assertFalse($access->equals('test'));
}

/**
Expand Down
56 changes: 56 additions & 0 deletions tests/ImmutableFloatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* This file is part of event-engine/php-types.
* (c) 2020 prooph software GmbH <contact@prooph.de>
*
* 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);
}
}
1 change: 1 addition & 0 deletions tests/ImmutableIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/ImmutableStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/Stub/ListWithoutTypeHintedConstructor.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?php
/**
* This file is part of event-engine/php-types.
* (c) 2020 prooph software GmbH <contact@prooph.de>
*
* 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;
Expand Down
18 changes: 18 additions & 0 deletions tests/Stub/Percentage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This file is part of event-engine/php-types.
* (c) 2020 prooph software GmbH <contact@prooph.de>
*
* 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;
}