diff --git a/Tests/Unit/EnumerableTest.php b/Tests/Unit/EnumerableTest.php index d1d0c3d..dd2a541 100644 --- a/Tests/Unit/EnumerableTest.php +++ b/Tests/Unit/EnumerableTest.php @@ -617,6 +617,15 @@ function testOfType() $this->assertEnumValuesEquals( [ new Exception ], $a->ofType('Exception')); + + // ofType (subclass) + $derivedException = new class extends Exception { }; + $this->assertEnumValuesSame( + [ $derivedException ], + E::from([ $derivedException ])->ofType(Exception::class)); + $this->assertEnumValuesSame( + [ $derivedException ], + E::from([ $derivedException ])->ofType(\Throwable::class)); } /** @covers \YaLinqo\Enumerable::select diff --git a/YaLinqo/Enumerable.php b/YaLinqo/Enumerable.php index 872f7c9..fc865d6 100644 --- a/YaLinqo/Enumerable.php +++ b/YaLinqo/Enumerable.php @@ -104,7 +104,7 @@ public function cast(string $type): Enumerable * Filters the elements of a sequence based on a specified type. *
Syntax: ofType (type) *
The ofType method returns only elements of the specified type. To instead receive an error if an element cannot be cast, use {@link cast}. - * @param string $type The type to filter the elements of the sequence on. Can be either class name or one of the predefined types: array, int (integer, long), callable (callable), float (real, double), null, string, object, numeric, scalar. + * @param string $type The type to filter the elements of the sequence on. Can be a class or interface name (including subclasses and implementations) or one of the predefined types: array, int (integer, long), callable (callable), float (real, double), null, string, object, numeric, scalar. * @return Enumerable A sequence that contains elements from the input sequence of the specified type. * @package YaLinqo\Projection and filtering */ @@ -135,7 +135,7 @@ public function ofType(string $type): Enumerable case 'string': return $this->where(function($v) { return is_string($v); }); default: - return $this->where(function($v) use ($type) { return is_object($v) && get_class($v) === $type; }); + return $this->where(function($v) use ($type) { return is_object($v) && $v instanceof $type; }); } }