Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Dec 28, 2024
1 parent 94f1a87 commit 358ef15
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
12 changes: 1 addition & 11 deletions src/Assert/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
use ReflectionFunction;
use ReflectionMethod;
use ReflectionNamedType;
use Webmozart\Assert\Assert;

use function explode;
use function is_object;
use function sprintf;
use function str_contains;

final class Filter
{
Expand All @@ -25,14 +22,7 @@ public static function boolean(callable $filter): void
} elseif (is_object($filter)) {
$reflection = new ReflectionMethod($filter, '__invoke');
} else {
Assert::string($filter);

if (! str_contains($filter, '::')) {
$reflection = new ReflectionFunction($filter);
} else {
[, $method] = explode('::', $filter);
$reflection = new ReflectionMethod($filter, $method);
}
throw new InvalidArgumentException('Expected Closure or invokable object, string given');
}

$returnType = $reflection->getReturnType();
Expand Down
4 changes: 2 additions & 2 deletions src/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function getResults(): array
// ensure transform property is set early ->withTransform() method
Assert::isCallable($this->transform);

$count = 0;
$collectedData = [];
$count = 0;
$collectedData = [];

if (is_callable($this->when)) {
// filter must be a callable with bool return type
Expand Down
15 changes: 13 additions & 2 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public function __invoke(int $datum): bool
$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);
Expand All @@ -39,7 +50,7 @@ public function __invoke(int $datum)
};
// phpcs:enable

$this->assertTrue(AtLeast::once($data, $filter));
AtLeast::once($data, $filter);
}

public function testWithNonBoolReturnTypeCallable(): void
Expand All @@ -55,6 +66,6 @@ public function __invoke(int $datum): string
}
};

$this->assertTrue(AtLeast::once($data, $filter));
AtLeast::once($data, $filter);
}
}

0 comments on commit 358ef15

Please sign in to comment.