Skip to content

Commit

Permalink
change order of types
Browse files Browse the repository at this point in the history
  • Loading branch information
sj-i committed Jul 20, 2020
1 parent dcde307 commit c25613d
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 90 deletions.
24 changes: 12 additions & 12 deletions src/Typist/NullableTypeEnforcer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,39 @@ private function __construct()
}

/**
* @param-out int|null $value_holder
* @param-out bool|null $value_holder
*/
public static function int(?int &$value_holder, ?int $value): NullableIntEnforcer
public static function bool(?bool &$value_holder, ?bool $value): NullableBoolEnforcer
{
$value_holder = $value;
return new NullableIntEnforcer($value_holder);
return new NullableBoolEnforcer($value_holder);
}

/**
* @param-out string|null $value_holder
* @param-out int|null $value_holder
*/
public static function string(?string &$value_holder, ?string $value): NullableStringEnforcer
public static function int(?int &$value_holder, ?int $value): NullableIntEnforcer
{
$value_holder = $value;
return new NullableStringEnforcer($value_holder);
return new NullableIntEnforcer($value_holder);
}

/**
* @param-out bool|null $value_holder
* @param-out float|null $value_holder
*/
public static function bool(?bool &$value_holder, ?bool $value): NullableBoolEnforcer
public static function float(?float &$value_holder, ?float $value): NullableFloatEnforcer
{
$value_holder = $value;
return new NullableBoolEnforcer($value_holder);
return new NullableFloatEnforcer($value_holder);
}

/**
* @param-out float|null $value_holder
* @param-out string|null $value_holder
*/
public static function float(?float &$value_holder, ?float $value): NullableFloatEnforcer
public static function string(?string &$value_holder, ?string $value): NullableStringEnforcer
{
$value_holder = $value;
return new NullableFloatEnforcer($value_holder);
return new NullableStringEnforcer($value_holder);
}

/**
Expand Down
24 changes: 12 additions & 12 deletions src/Typist/Typist.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,39 @@ private function __construct()
}

/**
* @param-out int $value_holder
* @param-out bool $value_holder
*/
public static function int(?int &$value_holder, int $value): IntEnforcer
public static function bool(?bool &$value_holder, bool $value): BoolEnforcer
{
$value_holder = $value;
return new IntEnforcer($value_holder);
return new BoolEnforcer($value_holder);
}

/**
* @param-out string $value_holder
* @param-out int $value_holder
*/
public static function string(?string &$value_holder, string $value): StringEnforcer
public static function int(?int &$value_holder, int $value): IntEnforcer
{
$value_holder = $value;
return new StringEnforcer($value_holder);
return new IntEnforcer($value_holder);
}

/**
* @param-out bool $value_holder
* @param-out float $value_holder
*/
public static function bool(?bool &$value_holder, bool $value): BoolEnforcer
public static function float(?float &$value_holder, float $value): FloatEnforcer
{
$value_holder = $value;
return new BoolEnforcer($value_holder);
return new FloatEnforcer($value_holder);
}

/**
* @param-out float $value_holder
* @param-out string $value_holder
*/
public static function float(?float &$value_holder, float $value): FloatEnforcer
public static function string(?string &$value_holder, string $value): StringEnforcer
{
$value_holder = $value;
return new FloatEnforcer($value_holder);
return new StringEnforcer($value_holder);
}

/**
Expand Down
36 changes: 18 additions & 18 deletions src/Typist/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
namespace Typist;

/**
* @param-out int $value_holder
* @param-out bool $value_holder
*/
function int(?int &$value_holder, int $value): IntEnforcer
function bool(?bool &$value_holder, bool $value): BoolEnforcer
{
return Typist::int($value_holder, $value);
return Typist::bool($value_holder, $value);
}

/**
* @param-out string $value_holder
* @param-out int $value_holder
*/
function string(?string &$value_holder, string $value): StringEnforcer
function int(?int &$value_holder, int $value): IntEnforcer
{
return Typist::string($value_holder, $value);
return Typist::int($value_holder, $value);
}

/**
Expand All @@ -38,11 +38,11 @@ function float(?float &$value_holder, float $value): FloatEnforcer
}

/**
* @param-out bool $value_holder
* @param-out string $value_holder
*/
function bool(?bool &$value_holder, bool $value): BoolEnforcer
function string(?string &$value_holder, string $value): StringEnforcer
{
return Typist::bool($value_holder, $value);
return Typist::string($value_holder, $value);
}

/**
Expand All @@ -60,19 +60,19 @@ function class_(string $class_name, &$value_holder, $value): ClassEnforcer
}

/**
* @param-out int|null $value_holder
* @param-out bool|null $value_holder
*/
function nullable_int(?int &$value_holder, ?int $value): NullableIntEnforcer
function nullable_bool(?bool &$value_holder, ?bool $value): NullableBoolEnforcer
{
return Typist::nullable()::int($value_holder, $value);
return Typist::nullable()::bool($value_holder, $value);
}

/**
* @param-out string|null $value_holder
* @param-out int|null $value_holder
*/
function nullable_string(?string &$value_holder, ?string $value): NullableStringEnforcer
function nullable_int(?int &$value_holder, ?int $value): NullableIntEnforcer
{
return Typist::nullable()::string($value_holder, $value);
return Typist::nullable()::int($value_holder, $value);
}

/**
Expand All @@ -84,11 +84,11 @@ function nullable_float(?float &$value_holder, ?float $value): NullableFloatEnfo
}

/**
* @param-out bool|null $value_holder
* @param-out string|null $value_holder
*/
function nullable_bool(?bool &$value_holder, ?bool $value): NullableBoolEnforcer
function nullable_string(?string &$value_holder, ?string $value): NullableStringEnforcer
{
return Typist::nullable()::bool($value_holder, $value);
return Typist::nullable()::string($value_holder, $value);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/Typist/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,25 @@ class FunctionsTest extends TestCase
public function testFunctions()
{
$_ = [
bool($typed_bool1, false),
int($typed_int1, 1),
float($typed_float1, 1.1),
string($typed_string1, 'str'),
bool($typed_bool1, false),
class_(DateTimeInterface::class, $typed_object1, new DateTime()),
nullable_bool($typed_bool2, false),
nullable_int($typed_int2, 1),
nullable_float($typed_float2, 1.1),
nullable_string($typed_string2, 'str'),
nullable_bool($typed_bool2, false),
nullable_class(DateTimeInterface::class, $typed_object2, new DateTime()),
nullable_bool($typed_bool3, null),
nullable_int($typed_int3, null),
nullable_float($typed_float3, null),
nullable_string($typed_string3, null),
nullable_bool($typed_bool3, null),
nullable_class(DateTimeInterface::class, $typed_object3, null),
];
$this->assertSame(false, $typed_bool1);
$this->assertSame(false, $typed_bool2);
$this->assertSame(null, $typed_bool3);
$this->assertSame(1, $typed_int1);
$this->assertSame(1, $typed_int2);
$this->assertSame(null, $typed_int3);
Expand All @@ -48,9 +51,6 @@ class_(DateTimeInterface::class, $typed_object1, new DateTime()),
$this->assertSame('str', $typed_string1);
$this->assertSame('str', $typed_string2);
$this->assertSame(null, $typed_string3);
$this->assertSame(false, $typed_bool1);
$this->assertSame(false, $typed_bool2);
$this->assertSame(null, $typed_bool3);
$this->assertInstanceOf(DateTime::class, $typed_object1);
$this->assertInstanceOf(DateTime::class, $typed_object2);
$this->assertSame(null, $typed_object3);
Expand Down
84 changes: 42 additions & 42 deletions tests/Typist/TypistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,61 +19,61 @@

class TypistTest extends TestCase
{
public function testInt(): void
public function testBool(): void
{
$_ = [
Typist::int($typed_value, 1),
Typist::bool($typed_value, false),
];
$this->assertSame(1, $typed_value);
$typed_value = 2;
$this->assertSame(false, $typed_value);
$typed_value = true;
$this->assertTypeError(function () use (&$typed_value) {
$typed_value = 'aaa';
$typed_value = 1;
});
$this->assertTypeError(function () use (&$typed_value) {
$typed_value = null;
});
}

public function testString(): void
public function testInt(): void
{
$_ = [
Typist::string($typed_value, 'a'),
Typist::int($typed_value, 1),
];
$this->assertSame('a', $typed_value);
$typed_value = 'b';
$this->assertSame(1, $typed_value);
$typed_value = 2;
$this->assertTypeError(function () use (&$typed_value) {
$typed_value = 1;
$typed_value = 'aaa';
});
$this->assertTypeError(function () use (&$typed_value) {
$typed_value = null;
});
}

public function testBool(): void
public function testFloat(): void
{
$_ = [
Typist::bool($typed_value, false),
Typist::float($typed_value, 1.1),
];
$this->assertSame(false, $typed_value);
$typed_value = true;
$this->assertSame(1.1, $typed_value);
$typed_value = -1.1;
$typed_value = 1;
$this->assertTypeError(function () use (&$typed_value) {
$typed_value = 1;
$typed_value = 'a';
});
$this->assertTypeError(function () use (&$typed_value) {
$typed_value = null;
});
}

public function testFloat(): void
public function testString(): void
{
$_ = [
Typist::float($typed_value, 1.1),
Typist::string($typed_value, 'a'),
];
$this->assertSame(1.1, $typed_value);
$typed_value = -1.1;
$typed_value = 1;
$this->assertSame('a', $typed_value);
$typed_value = 'b';
$this->assertTypeError(function () use (&$typed_value) {
$typed_value = 'a';
$typed_value = 1;
});
$this->assertTypeError(function () use (&$typed_value) {
$typed_value = null;
Expand All @@ -95,40 +95,28 @@ public function testClass(): void
});
}

public function testNullableInt(): void
{
$_ = [
Typist::nullable()::int($typed_value, 1),
];
$this->assertSame(1, $typed_value);
$typed_value = 2;
$typed_value = null;
$this->expectException(TypeError::class);
$typed_value = 'aaa';
}

public function testNullableString(): void
public function testNullableBool(): void
{
$_ = [
Typist::nullable()::string($typed_value, 'a'),
Typist::nullable()::bool($typed_value, false),
];
$this->assertSame('a', $typed_value);
$typed_value = 'b';
$this->assertSame(false, $typed_value);
$typed_value = true;
$typed_value = null;
$this->expectException(TypeError::class);
$typed_value = 1;
}

public function testNullableBool(): void
public function testNullableInt(): void
{
$_ = [
Typist::nullable()::bool($typed_value, false),
Typist::nullable()::int($typed_value, 1),
];
$this->assertSame(false, $typed_value);
$typed_value = true;
$this->assertSame(1, $typed_value);
$typed_value = 2;
$typed_value = null;
$this->expectException(TypeError::class);
$typed_value = 1;
$typed_value = 'aaa';
}

public function testNullableFloat(): void
Expand All @@ -144,6 +132,18 @@ public function testNullableFloat(): void
$typed_value = '';
}

public function testNullableString(): void
{
$_ = [
Typist::nullable()::string($typed_value, 'a'),
];
$this->assertSame('a', $typed_value);
$typed_value = 'b';
$typed_value = null;
$this->expectException(TypeError::class);
$typed_value = 1;
}

public function testNullableClass(): void
{
$_ = [
Expand Down

0 comments on commit c25613d

Please sign in to comment.