Skip to content

Commit

Permalink
feat: add roave/better-reflection reflector factory
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Mar 21, 2024
1 parent a32f5c5 commit b4afadf
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 10 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"doctrine/annotations": "^1.0",
"phpdocumentor/reflection": "^4.0 || ^5.0",
"phpunit/phpunit": "^10.5",
"roave/better-reflection": "^6.0",
"roave/security-advisories": "dev-master",
"solido/php-coding-standards": "dev-master",
"symfony/error-handler": "^5.0 || ^6.0 || ^7.0"
Expand Down
176 changes: 169 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/Iterator/Psr0Iterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Throwable;

use function array_map;
use function assert;
use function class_exists;
use function defined;
use function in_array;
Expand Down Expand Up @@ -68,6 +69,8 @@ class_exists($class, true);
null,
);

assert($include instanceof Closure);

foreach ($this->search() as $path => $info) {
if (! preg_match($pattern, $path, $m) || ! $info->isReadable()) {
continue;
Expand Down
3 changes: 3 additions & 0 deletions lib/Iterator/Psr4Iterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Throwable;

use function array_map;
use function assert;
use function class_exists;
use function defined;
use function in_array;
Expand Down Expand Up @@ -68,6 +69,8 @@ class_exists($class, true);
null,
);

assert($include instanceof Closure);

foreach ($this->search() as $path => $info) {
if (! preg_match($pattern, $path, $m) || ! $info->isReadable()) {
continue;
Expand Down
4 changes: 1 addition & 3 deletions lib/Iterator/RecursiveIteratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ private function search(): Generator
$files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS),
static function (SplFileInfo $file) {
return $file->getBasename()[0] !== '.';
},
static fn (SplFileInfo $file): bool => $file->getBasename()[0] !== '.',
),
RecursiveIteratorIterator::LEAVES_ONLY,
);
Expand Down
17 changes: 17 additions & 0 deletions lib/Reflection/BetterReflectionReflectorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Kcs\ClassFinder\Reflection;

use Roave\BetterReflection\BetterReflection;

class BetterReflectionReflectorFactory implements ReflectorFactoryInterface
{
public function reflect(string $className): object
{
return (new BetterReflection())
->reflector()
->reflectClass($className);
}
}
18 changes: 18 additions & 0 deletions tests/unit/Reflection/BetterReflectionReflectorFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Kcs\ClassFinder\Tests\unit\Reflection;

use Kcs\ClassFinder\Reflection\BetterReflectionReflectorFactory;
use PHPUnit\Framework\TestCase;
use Roave\BetterReflection\Reflection\ReflectionClass;

class BetterReflectionReflectorFactoryTest extends TestCase
{
public function testShouldReturnABetterReflectionReflectorObject(): void
{
$factory = new BetterReflectionReflectorFactory();
self::assertInstanceOf(ReflectionClass::class, $factory->reflect(self::class));
}
}

0 comments on commit b4afadf

Please sign in to comment.