• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daycry / class-finder / 16724626674

04 Aug 2025 01:32PM UTC coverage: 82.262% (-12.2%) from 94.493%
16724626674

push

github

web-flow
Update php.yml

320 of 389 relevant lines covered (82.26%)

3.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/src/ClassFinder.php
1
<?php
2

3
namespace Daycry\ClassFinder;
4

5
use CodeIgniter\Config\BaseConfig;
6
use Daycry\ClassFinder\Config\ClassFinder as ClassFinderConfig;
7
use Daycry\ClassFinder\Interfaces\FinderInterface;
8
use Exception;
9

10
class ClassFinder
11
{
12
    public const STANDARD_MODE    = 1;
13
    public const RECURSIVE_MODE   = 2;
14
    public const ALLOW_CLASSES    = 4;
15
    public const ALLOW_INTERFACES = 8;
16
    public const ALLOW_TRAITS     = 16;
17
    public const ALLOW_FUNCTIONS  = 32;
18
    public const ALLOW_ALL        = 60;
19

20
    private array $finders                           = [];
21
    private static ?ClassFinderConfig $defaultConfig = null;
22
    private array $namespaceCache                    = [];
23

24
    public function __construct(?BaseConfig $config = null)
25
    {
26
        $this->initialize($config);
8✔
27
    }
28

29
    private function initialize(?BaseConfig $config = null): void
30
    {
31
        if ($config === null) {
8✔
32
            $config = self::$defaultConfig ??= config('ClassFinder');
1✔
33
        }
34

35
        foreach ($config->finder as $method => $value) {
8✔
36
            if ($value === true && isset($config->finderClass[$method])) {
8✔
37
                $finderClass = $config->finderClass[$method];
8✔
38
                if (class_exists($finderClass)) {
8✔
39
                    $class = new $finderClass();
8✔
40
                    if ($class instanceof FinderInterface) {
8✔
41
                        $this->finders[] = $class;
8✔
42
                    }
43
                }
44
            }
45
        }
46

47
        // Sort finders by priority (lower number = higher priority)
48
        usort($this->finders, static fn (FinderInterface $a, FinderInterface $b) => $a->getPriority() <=> $b->getPriority());
8✔
49
    }
50

51
    /**
52
     * Identify classes in a given namespace.
53
     *
54
     * @return list<string>
55
     *
56
     * @throws Exception
57
     */
58
    public function getClassesInNamespace(string $namespace, int $options = self::STANDARD_MODE): array
59
    {
60
        if (! ($options & (self::ALLOW_INTERFACES | self::ALLOW_TRAITS))) {
8✔
61
            $options |= self::ALLOW_CLASSES;
7✔
62
        }
63

64
        $cacheKey = $namespace . '_' . $options;
8✔
65
        if (isset($this->namespaceCache[$cacheKey])) {
8✔
66
            return $this->namespaceCache[$cacheKey];
×
67
        }
68

69
        $findersWithNamespace = $this->findersWithNamespace($namespace);
8✔
70

71
        $classes = [];
8✔
72

73
        foreach ($findersWithNamespace as $finder) {
8✔
74
            $classes = array_merge($classes, $finder->findClasses($namespace, $options));
8✔
75
        }
76

77
        $result                          = array_unique($classes);
8✔
78
        $this->namespaceCache[$cacheKey] = $result;
8✔
79

80
        return $result;
8✔
81
    }
82

83
    /**
84
     * @return list<FinderInterface>
85
     */
86
    private function findersWithNamespace(string $namespace): array
87
    {
88
        return array_filter($this->finders, static fn (FinderInterface $finder) => $finder->isNamespaceKnown($namespace));
8✔
89
    }
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc