mirror of
https://github.com/danog/Valinor.git
synced 2024-11-30 04:39:05 +01:00
misc: remove unused code
This commit is contained in:
parent
86802daced
commit
de8aa9f440
@ -5,15 +5,13 @@ declare(strict_types=1);
|
||||
namespace CuyZ\Valinor\Type\Types;
|
||||
|
||||
use CuyZ\Valinor\Type\IntegerType;
|
||||
use CuyZ\Valinor\Type\ScalarType;
|
||||
use CuyZ\Valinor\Type\StringType;
|
||||
use CuyZ\Valinor\Type\Type;
|
||||
use CuyZ\Valinor\Type\Types\Exception\CannotCastValue;
|
||||
|
||||
use function is_int;
|
||||
|
||||
/** @internal */
|
||||
final class ArrayKeyType implements ScalarType
|
||||
final class ArrayKeyType implements Type
|
||||
{
|
||||
private static self $integer;
|
||||
|
||||
@ -101,36 +99,6 @@ final class ArrayKeyType implements ScalarType
|
||||
return false;
|
||||
}
|
||||
|
||||
public function canCast($value): bool
|
||||
{
|
||||
foreach ($this->types as $type) {
|
||||
if ($type->canCast($value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|string
|
||||
*/
|
||||
public function cast($value)
|
||||
{
|
||||
if ($this->accepts($value)) {
|
||||
/** @var int|string $value */
|
||||
return $value;
|
||||
}
|
||||
|
||||
foreach ($this->types as $type) {
|
||||
if ($type->canCast($value)) {
|
||||
return $type->cast($value);
|
||||
}
|
||||
}
|
||||
|
||||
throw new CannotCastValue($value, $this);
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return $this->signature;
|
||||
|
@ -6,7 +6,6 @@ namespace CuyZ\Valinor\Tests\Unit\Type\Types;
|
||||
|
||||
use CuyZ\Valinor\Tests\Fake\Type\FakeType;
|
||||
use CuyZ\Valinor\Type\Types\ArrayKeyType;
|
||||
use CuyZ\Valinor\Type\Types\Exception\CannotCastValue;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use stdClass;
|
||||
|
||||
@ -74,44 +73,4 @@ final class ArrayKeyTypeTest extends TestCase
|
||||
{
|
||||
self::assertFalse(ArrayKeyType::default()->matches(new FakeType()));
|
||||
}
|
||||
|
||||
public function test_can_cast_correct_values(): void
|
||||
{
|
||||
$arrayKeyDefault = ArrayKeyType::default();
|
||||
$arrayKeyInteger = ArrayKeyType::integer();
|
||||
$arrayKeyString = ArrayKeyType::string();
|
||||
|
||||
self::assertTrue($arrayKeyDefault->canCast(42));
|
||||
self::assertTrue($arrayKeyDefault->canCast('foo'));
|
||||
|
||||
self::assertTrue($arrayKeyInteger->canCast(42));
|
||||
self::assertFalse($arrayKeyInteger->canCast('foo'));
|
||||
|
||||
self::assertTrue($arrayKeyString->canCast(42));
|
||||
self::assertTrue($arrayKeyString->canCast('foo'));
|
||||
}
|
||||
|
||||
public function test_cast_value_returns_correct_value(): void
|
||||
{
|
||||
$arrayKeyDefault = ArrayKeyType::default();
|
||||
$arrayKeyInteger = ArrayKeyType::integer();
|
||||
$arrayKeyString = ArrayKeyType::string();
|
||||
|
||||
self::assertSame(42, $arrayKeyDefault->cast(42));
|
||||
self::assertSame('foo', $arrayKeyDefault->cast('foo'));
|
||||
|
||||
self::assertSame(42, $arrayKeyInteger->cast('42'));
|
||||
|
||||
self::assertSame(42, $arrayKeyString->cast(42));
|
||||
self::assertSame('foo', $arrayKeyString->cast('foo'));
|
||||
}
|
||||
|
||||
public function test_cast_unsupported_value_throws_exception(): void
|
||||
{
|
||||
$this->expectException(CannotCastValue::class);
|
||||
$this->expectExceptionCode(1603216198);
|
||||
$this->expectExceptionMessage('Cannot cast object(stdClass) to `array-key`.');
|
||||
|
||||
ArrayKeyType::default()->cast(new stdClass());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user