Skip to content

Commit 83eaa34

Browse files
committed
Add predicateForAttributeInstanceOf()
1 parent 3193222 commit 83eaa34

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

MIGRATION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ None
1010

1111
- [#11](https://github.com/olvlvl/composer-attribute-collector/pull/11) Attribute instantiation errors are decorated to help find origin (@withinboredom @olvlvl)
1212
- [#12](https://github.com/olvlvl/composer-attribute-collector/pull/12) `Attributes::filterTargetClasses()` can filter target classes using a predicate (@olvlvl)
13-
- [#12](https://github.com/olvlvl/composer-attribute-collector/pull/12) `Attributes::filterTargetMethods()` can filter target methods using a predicate (@olvlvl)
13+
- [#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)
1414
- [#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)
1515

1616
### Backward Incompatible Changes

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ Because the `Get` and `Post` attributes extend `Route`, all action methods can b
350350
```php
351351
/** @var TargetMethod<Route>[] $target_methods */
352352
$target_methods = Attributes::filterTargetMethods(
353-
fn($attribute) => is_a($attribute, Route::class, true)
353+
Attributes::predicateForAttributeInstanceOf(Route::class)
354354
);
355355
```
356356

@@ -399,7 +399,7 @@ $target_methods = [
399399

400400
/** @var TargetMethod<Route>[] $target_methods */
401401
$target_methods = Attributes::filterTargetMethods(
402-
fn($attribute) => is_a($attribute, Route::class, true)
402+
Attributes::predicateForAttributeInstanceOf(Route::class)
403403
);
404404
```
405405

src/Attributes.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Closure;
1313
use LogicException;
1414

15+
use function is_a;
16+
1517
final class Attributes
1618
{
1719
/**
@@ -74,6 +76,16 @@ public static function filterTargetMethods(callable $predicate): array
7476
return self::getCollection()->filterTargetMethods($predicate);
7577
}
7678

79+
/**
80+
* @param class-string $class
81+
*
82+
* @return Closure(class-string $attribute):bool
83+
*/
84+
public static function predicateForAttributeInstanceOf(string $class): Closure
85+
{
86+
return fn(string $attribute): bool => is_a($attribute, $class, true);
87+
}
88+
7789
/**
7890
* @var array<class-string, ForClass>
7991
*/

tests/PluginTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public function testFilterTargetClasses(): void
201201
public function testFilterTargetMethods(): void
202202
{
203203
$actual = Attributes::filterTargetMethods(
204-
fn($attribute) => is_a($attribute, Route::class, true)
204+
Attributes::predicateForAttributeInstanceOf(Route::class)
205205
);
206206

207207
$this->assertEquals([

0 commit comments

Comments
 (0)