-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from samsonasik/v2-filter-early
Version 2: 🚀 Faster process with early validate filter before loop
- Loading branch information
Showing
7 changed files
with
147 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ArrayLookup\Assert; | ||
|
||
use Closure; | ||
use InvalidArgumentException; | ||
use ReflectionFunction; | ||
use ReflectionMethod; | ||
use ReflectionNamedType; | ||
|
||
use function gettype; | ||
use function is_object; | ||
use function sprintf; | ||
|
||
final class Filter | ||
{ | ||
public static function boolean(callable $filter): void | ||
{ | ||
if ($filter instanceof Closure) { | ||
$reflection = new ReflectionFunction($filter); | ||
} elseif (is_object($filter)) { | ||
$reflection = new ReflectionMethod($filter, '__invoke'); | ||
} else { | ||
throw new InvalidArgumentException( | ||
sprintf('Expected Closure or invokable object, %s given', gettype($filter)) | ||
); | ||
} | ||
|
||
$returnType = $reflection->getReturnType(); | ||
|
||
if (! $returnType instanceof ReflectionNamedType) { | ||
throw new InvalidArgumentException('Expected a bool return type on callable filter, null given'); | ||
} | ||
|
||
$returnTypeName = $returnType->getName(); | ||
if ($returnTypeName !== 'bool') { | ||
throw new InvalidArgumentException(sprintf( | ||
'Expected a bool return type on callable filter, %s given', | ||
$returnTypeName | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ArrayLookup\Tests; | ||
|
||
use ArrayLookup\AtLeast; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class FilterTest extends TestCase | ||
{ | ||
public function testOnceWithFilterInvokableClass(): void | ||
{ | ||
$data = [1, 2, 3]; | ||
$filter = new class { | ||
public function __invoke(int $datum): bool | ||
{ | ||
return $datum === 1; | ||
} | ||
}; | ||
|
||
$this->assertTrue(AtLeast::once($data, $filter)); | ||
} | ||
|
||
public function testOnceWithStringFilter(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Expected Closure or invokable object, string given'); | ||
|
||
$data = [1, 'f']; | ||
$filter = 'is_string'; | ||
|
||
AtLeast::once($data, $filter); | ||
} | ||
|
||
public function testWithoutReturnTypeCallable(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Expected a bool return type on callable filter, null given'); | ||
|
||
$data = [1, 2, 3]; | ||
|
||
// phpcs:disable | ||
$filter = new class { | ||
public function __invoke(int $datum) | ||
{ | ||
return $datum === 1; | ||
} | ||
}; | ||
// phpcs:enable | ||
|
||
AtLeast::once($data, $filter); | ||
} | ||
|
||
public function testWithNonBoolReturnTypeCallable(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Expected a bool return type on callable filter, string given'); | ||
|
||
$data = [1, 2, 3]; | ||
$filter = new class { | ||
public function __invoke(int $datum): string | ||
{ | ||
return 'test'; | ||
} | ||
}; | ||
|
||
AtLeast::once($data, $filter); | ||
} | ||
} |