Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/main/php/lang/Primitive.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function newInstance(...$args) {
* @throws lang.ClassCastException
*/
public function cast($value) {
return null === $value ? null : $this->coerce($value, function($value) {
return $this->coerce($value, function($value) {
throw new ClassCastException('Cannot cast to '.$this->getName().' from '.typeof($value)->getName());
});
}
Expand Down
9 changes: 4 additions & 5 deletions src/main/php/lang/Type.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function newInstance(... $args) {
}
}
public function cast($value) {
return null === $value ? null : (array)$value;
return (array)$value;
}
public function isAssignableFrom($type): bool {
return $type instanceof self || $type instanceof ArrayType || $type instanceof MapType;
Expand All @@ -45,8 +45,7 @@ public function newInstance(... $args) {
throw new IllegalAccessException("Cannot instantiate an object from ".($args ? typeof($args[0])->getName() : "null"));
}
public function cast($value) {
if (null === $value || is_object($value)) return $value;
throw new ClassCastException("Cannot cast ".typeof($value)->getName()." to the object type");
return (object)$value;
}
public function isAssignableFrom($type): bool {
return $type instanceof self || $type instanceof XPClass;
Expand All @@ -61,7 +60,7 @@ public function newInstance(... $args) {
throw new IllegalAccessException("Cannot instantiate a callable from ".($args ? typeof($args[0])->getName() : "null"));
}
public function cast($value) {
if (null === $value || is_callable($value)) return $value;
if (is_callable($value)) return $value;
throw new ClassCastException("Cannot cast ".typeof($value)->getName()." to the callable type");
}
public function isAssignableFrom($type): bool {
Expand All @@ -77,7 +76,7 @@ public function newInstance(... $args) {
throw new IllegalAccessException("Cannot instantiate an iterable from ".($args ? typeof($args[0])->getName() : "null"));
}
public function cast($value) {
if (null === $value || $value instanceof \Traversable || is_array($value)) return $value;
if (is_iterable($value)) return $value;
throw new ClassCastException("Cannot cast ".typeof($value)->getName()." to the iterable type");
}
public function isAssignableFrom($type): bool {
Expand Down
2 changes: 0 additions & 2 deletions src/main/php/lang/XPClass.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ public function newInstance(... $args) {
* @throws lang.ClassCastException
*/
public function cast($value) {
if (null === $value) return null;

$literal= literal($this->name);
if ($value instanceof $literal) {
return $value;
Expand Down
4 changes: 2 additions & 2 deletions src/test/php/lang/unittest/ClassCastingTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function thisClassCastingAnUnrelatedClass() {
typeof($this)->cast(Type::$VOID);
}

#[Test]
#[Test, Expect(ClassCastException::class)]
public function thisClassCastingNull() {
Assert::null(typeof($this)->cast(null));
typeof($this)->cast(null);
}

#[Test, Expect(ClassCastException::class)]
Expand Down
8 changes: 4 additions & 4 deletions src/test/php/lang/unittest/PrimitiveTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,22 @@ public function newInstance_of_bool($expected, $value) {
Assert::equals($expected, Primitive::$BOOL->newInstance($value));
}

#[Test, Values([['', ''], ['Test', 'Test'], [null, null], ['0', 0], ['-1', -1], ['0.5', 0.5], ['', false], ['1', true]])]
#[Test, Values([['', ''], ['Test', 'Test'], ['', null], ['0', 0], ['-1', -1], ['0.5', 0.5], ['', false], ['1', true]])]
public function cast_of_string($expected, $value) {
Assert::equals($expected, Primitive::$STRING->cast($value));
}

#[Test, Values([[0, ''], [0, 'Test'], [2, '2'], [123, '123'], [0xFF, '0xFF'], [0755, '0755'], [null, null], [0, 0], [-1, -1], [0, 0.5], [0, false], [1, true]])]
#[Test, Values([[0, ''], [0, 'Test'], [2, '2'], [123, '123'], [0xFF, '0xFF'], [0755, '0755'], [0, null], [0, 0], [-1, -1], [0, 0.5], [0, false], [1, true]])]
public function cast_of_int($expected, $value) {
Assert::equals($expected, Primitive::$INT->cast($value));
}

#[Test, Values([[0.0, ''], [0.0, 'Test'], [123.0, '123'], [0.0, '0xFF'], [755.0, '0755'], [null, null], [0.0, 0], [-1.0, -1], [0.5, 0.5], [0.0, false]])]
#[Test, Values([[0.0, ''], [0.0, 'Test'], [123.0, '123'], [0.0, '0xFF'], [755.0, '0755'], [0.0, null], [0.0, 0], [-1.0, -1], [0.5, 0.5], [0.0, false]])]
public function cast_of_double($expected, $value) {
Assert::equals($expected, Primitive::$FLOAT->cast($value));
}

#[Test, Values([[false, ''], [true, 'Test'], [null, null], [false, 0], [true, -1], [true, 0.5], [false, false], [true, true]])]
#[Test, Values([[false, ''], [true, 'Test'], [false, null], [false, 0], [true, -1], [true, 0.5], [false, false], [true, true]])]
public function cast_of_bool($expected, $value) {
Assert::equals($expected, Primitive::$BOOL->cast($value));
}
Expand Down
13 changes: 7 additions & 6 deletions src/test/php/lang/unittest/TypeTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public function array_type_union_cast($value) {

#[Test]
public function array_type_union_cast_null() {
Assert::equals(null, Type::$ARRAY->cast(null));
Assert::equals([], Type::$ARRAY->cast(null));
}

#[Test, Values(from: 'callables')]
Expand All @@ -395,9 +395,9 @@ public function callable_type_union_cast($value) {
Assert::equals($value, Type::$CALLABLE->cast($value));
}

#[Test]
#[Test, Expect(ClassCastException::class)]
public function callable_type_union_cast_null() {
Assert::equals(null, Type::$CALLABLE->cast(null));
Type::$CALLABLE->cast(null);
}

#[Test]
Expand Down Expand Up @@ -441,10 +441,11 @@ public function iterable_type_union_cast($value) {
Assert::equals($value, Type::$ITERABLE->cast($value));
}

#[Test]
#[Test, Expect(ClassCastException::class)]
public function iterable_type_union_cast_null() {
Assert::null(Type::$ITERABLE->cast(null));
Type::$ITERABLE->cast(null);
}

#[Test, Values(eval: '[[new Name("test")], [new \ArrayObject([])]]')]
public function object_type_union_isInstance($value) {
Assert::true(Type::$OBJECT->isInstance($value));
Expand All @@ -455,7 +456,7 @@ public function closures_are_instances_of_the_object_type_union($value) {
Assert::true(Type::$OBJECT->isInstance($value));
}

#[Test, Values(eval: '[[null], [new Name("test")], [new \ArrayObject([])]]')]
#[Test, Values(eval: '[[new Name("test")], [new \ArrayObject([])]]')]
public function object_type_union_cast($value) {
Assert::equals($value, Type::$OBJECT->cast($value));
}
Expand Down
Loading