Removed Type class.

This commit is contained in:
flash 2023-11-09 12:59:28 +00:00
parent 1c2058e199
commit 06f0ba27bb
3 changed files with 1 additions and 176 deletions

View file

@ -1 +1 @@
0.2311.91257
0.2311.91259

View file

@ -1,126 +0,0 @@
<?php
// Type.php
// Created: 2021-04-26
// Updated: 2022-02-28
namespace Index;
use ReflectionClass;
use RuntimeException;
final class Type {
/**
* Boolean type id.
*
* @var string
*/
public const BOOL = 'boolean';
/**
* Integer type id.
*
* @var string
*/
public const INTEGER = 'integer';
/**
* Floating point type id.
*
* @var string
*/
public const FLOAT = 'double';
/**
* String type id.
*
* @var string
*/
public const STRING = 'string';
/**
* Array type id.
*
* @var string
*/
public const ARRAY = 'array';
/**
* Object type id.
*
* @var string
*/
public const OBJECT = 'object';
/**
* Resource type id.
*
* @var string
*/
public const RESOURCE = 'resource';
/**
* Closed resource type id.
*
* @var string
*/
public const CLOSED_RESOURCE = 'resource (closed)';
/**
* NULL type id.
*
* @var string
*/
public const NULL = 'NULL';
/**
* Unknown type id.
*
* @var string
*/
public const UNKNOWN = 'unknown type';
/**
* Gets the default value for this type.
*
* @return mixed Default value for the given base type.
*/
public static function default(string $type): mixed {
switch($type) {
case self::BOOL:
return false;
case self::INTEGER:
return 0;
case self::FLOAT:
return 0.0;
case self::STRING:
return '';
case self::ARRAY:
return [];
default:
return null;
}
}
/**
* Gets a list of all class names this Type inherits.
*
* @param string|object $target Target object.
* @return array Array containing the names.
*/
public static function classTree(string|object $target): array {
if(is_object($target))
$target = get_class($target);
$tree = [];
$classInfo = new ReflectionClass($target);
foreach($classInfo->getInterfaceNames() as $interface)
$tree[] = $interface;
do {
$tree[] = $classInfo->getName();
} while(($classInfo = $classInfo->getParentClass()) !== false);
return $tree;
}
}

View file

@ -1,49 +0,0 @@
<?php
// TypeTest.php
// Created: 2021-04-27
// Updated: 2022-02-28
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use Index\AString;
use Index\Type;
use Index\XArray;
/**
* @covers Type
*/
final class TypeTest extends TestCase {
public function testDefaults(): void {
$this->assertEquals(Type::default(Type::BOOL), false);
$this->assertEquals(Type::default(Type::INTEGER), 0);
$this->assertEquals(Type::default(Type::FLOAT), 0.0);
$this->assertEquals(Type::default(Type::STRING), '');
$this->assertEquals(Type::default(Type::ARRAY), []);
$this->assertEquals(Type::default(Type::OBJECT), null);
$this->assertEquals(Type::default(Type::RESOURCE), null);
$this->assertEquals(Type::default(Type::CLOSED_RESOURCE), null);
$this->assertEquals(Type::default(Type::NULL), null);
$this->assertEquals(Type::default(Type::UNKNOWN), null);
}
public function testClassTree(): void {
$array1 = Type::classTree(new AString('the'));
$array2 = [
'Index\\IString',
'Stringable',
'Traversable',
'Index\\Serialisation\\IBencodeSerialisable',
'Index\\IEquatable',
'Index\\ICloneable',
'Index\\IComparable',
'JsonSerializable',
'IteratorAggregate',
'Countable',
'ArrayAccess',
'Index\\AString',
];
$this->assertTrue(XArray::sequenceEquals($array1, $array2));
}
}