Skip to content

Commit

Permalink
Add predicateForAttributeInstanceOf()
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Mar 24, 2023
1 parent 3193222 commit 83eaa34
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ None

- [#11](https://github.com/olvlvl/composer-attribute-collector/pull/11) Attribute instantiation errors are decorated to help find origin (@withinboredom @olvlvl)
- [#12](https://github.com/olvlvl/composer-attribute-collector/pull/12) `Attributes::filterTargetClasses()` can filter target classes using a predicate (@olvlvl)
- [#12](https://github.com/olvlvl/composer-attribute-collector/pull/12) `Attributes::filterTargetMethods()` can filter target methods using a predicate (@olvlvl)
- [#12](https://github.com/olvlvl/composer-attribute-collector/pull/12) `Attributes::filterTargetMethods()` can filter target methods using a predicate. `Attributes::predicateForAttributeInstanceOf()` can be used to create a predicate to filter classes or methods targeted by an attribute class or subclass (@olvlvl)
- [#10](https://github.com/olvlvl/composer-attribute-collector/pull/10) 3 types of cache speed up generation by limiting updates to changed files (@xepozz @olvlvl)

### Backward Incompatible Changes
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ Because the `Get` and `Post` attributes extend `Route`, all action methods can b
```php
/** @var TargetMethod<Route>[] $target_methods */
$target_methods = Attributes::filterTargetMethods(
fn($attribute) => is_a($attribute, Route::class, true)
Attributes::predicateForAttributeInstanceOf(Route::class)
);
```

Expand Down Expand Up @@ -399,7 +399,7 @@ $target_methods = [

/** @var TargetMethod<Route>[] $target_methods */
$target_methods = Attributes::filterTargetMethods(
fn($attribute) => is_a($attribute, Route::class, true)
Attributes::predicateForAttributeInstanceOf(Route::class)
);
```

Expand Down
12 changes: 12 additions & 0 deletions src/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Closure;
use LogicException;

use function is_a;

final class Attributes
{
/**
Expand Down Expand Up @@ -74,6 +76,16 @@ public static function filterTargetMethods(callable $predicate): array
return self::getCollection()->filterTargetMethods($predicate);
}

/**
* @param class-string $class
*
* @return Closure(class-string $attribute):bool
*/
public static function predicateForAttributeInstanceOf(string $class): Closure
{
return fn(string $attribute): bool => is_a($attribute, $class, true);
}

/**
* @var array<class-string, ForClass>
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function testFilterTargetClasses(): void
public function testFilterTargetMethods(): void
{
$actual = Attributes::filterTargetMethods(
fn($attribute) => is_a($attribute, Route::class, true)
Attributes::predicateForAttributeInstanceOf(Route::class)
);

$this->assertEquals([
Expand Down

0 comments on commit 83eaa34

Please sign in to comment.