Skip to content
Open
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
9 changes: 9 additions & 0 deletions Tests/Unit/EnumerableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions YaLinqo/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function cast(string $type): Enumerable
* Filters the elements of a sequence based on a specified type.
* <p><b>Syntax</b>: ofType (type)
* <p>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
*/
Expand Down Expand Up @@ -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; });
}
}

Expand Down