Skip to content

Commit ac10762

Browse files
committed
Added ClassFinder::findWith() method
1 parent a005eea commit ac10762

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- `Query::descendingNullsFirst()`
1111
- `Query::ascendingNullsLast()`
1212
- `Query::descendingNullsLast()`
13+
* Added `findWith` method to the `ClassFinder` class to enable finding classes using a certain attribute.
1314

1415
#### Deprecations
1516

src/mako/classes/ClassFinder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,16 @@ public function findUsing(string $traitName): Generator
306306
}
307307
}
308308
}
309+
310+
/**
311+
* Returns all the classes with the attribute.
312+
*/
313+
public function findWith(string $attributeName): Generator
314+
{
315+
foreach ($this->findClasses() as $file => $class) {
316+
if (in_array($attributeName, ClassInspector::getAttributes($class))) {
317+
yield $file => $class;
318+
}
319+
}
320+
}
309321
}

tests/unit/classes/ClassFinderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace mako\tests\unit\classes;
99

10+
use AllowDynamicProperties;
1011
use mako\classes\ClassFinder;
1112
use mako\file\Finder;
1213
use mako\tests\TestCase;
@@ -232,4 +233,25 @@ public function testClassFinderUsing(): void
232233

233234
$this->assertSame($expectedClasses, $classes);
234235
}
236+
237+
/**
238+
*
239+
*/
240+
public function testClassFinderWith(): void
241+
{
242+
$expectedClasses =
243+
[
244+
BazClass::class,
245+
];
246+
247+
$finder = new ClassFinder(new Finder([__DIR__ . '/classes']));
248+
249+
$classes = iterator_to_array($finder->findWith(AllowDynamicProperties::class));
250+
251+
sort($expectedClasses);
252+
253+
sort($classes);
254+
255+
$this->assertSame($expectedClasses, $classes);
256+
}
235257
}

tests/unit/classes/classes/BazClass.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace mako\tests\unit\classes\classes;
44

5+
use AllowDynamicProperties;
6+
7+
#[AllowDynamicProperties]
58
class BazClass
69
{
710
use FooTrait;

0 commit comments

Comments
 (0)