Passed
Pull Request — master (#34)
by Rustam
02:23
created

Classifier::find()   C

Complexity

Conditions 14
Paths 29

Size

Total Lines 62
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 14.0038

Importance

Changes 7
Bugs 0 Features 0
Metric Value
cc 14
eloc 34
c 7
b 0
f 0
nc 29
nop 0
dl 0
loc 62
ccs 36
cts 37
cp 0.973
crap 14.0038
rs 6.2666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Classifier;
6
7
use ReflectionAttribute;
8
use ReflectionClass;
9
use Symfony\Component\Finder\Finder;
10
11
final class Classifier
12
{
13
    /**
14
     * @var string[]
15
     */
16
    private array $interfaces = [];
17
    /**
18
     * @var string[]
19
     */
20
    private array $attributes = [];
21
    /**
22
     * @var class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
23
     */
24
    private ?string $parentClass = null;
25
    /**
26
     * @var string[]
27
     */
28
    private array $directories;
29
30 12
    public function __construct(string ...$directory)
31
    {
32 12
        $this->directories = $directory;
33
    }
34
35
    /**
36
     * @psalm-param class-string ...$interfaces
37
     */
38 9
    public function withInterface(string ...$interfaces): self
39
    {
40 9
        $new = clone $this;
41 9
        array_push($new->interfaces, ...array_values($interfaces));
42
43 9
        return $new;
44
    }
45
46
    /**
47
     * @psalm-param class-string $parentClass
48
     */
49 1
    public function withParentClass(string $parentClass): self
50
    {
51 1
        $new = clone $this;
52 1
        $new->parentClass = $parentClass;
53 1
        return $new;
54
    }
55
56
    /**
57
     * @psalm-param class-string ...$attributes
58
     */
59 4
    public function withAttribute(string ...$attributes): self
60
    {
61 4
        $new = clone $this;
62 4
        array_push($new->attributes, ...array_values($attributes));
63
64 4
        return $new;
65
    }
66
67
    /**
68
     * @return iterable<class-string>
69
     */
70 12
    public function find(): iterable
71
    {
72 12
        $countInterfaces = count($this->interfaces);
73 12
        $countAttributes = count($this->attributes);
74
75 12
        if ($countInterfaces === 0 && $countAttributes === 0 && $this->parentClass === null) {
76 3
            return [];
77
        }
78
79 9
        $this->scanFiles();
80
81 9
        $classesToFind = get_declared_classes();
82 9
        $isWindows = DIRECTORY_SEPARATOR === '\\';
83 9
        $directories = $this->directories;
84
85 9
        if ($isWindows) {
86
            /** @var string[] $directories */
87
            $directories = str_replace('/', '\\', $directories);
88
        }
89
90 9
        foreach ($classesToFind as $className) {
91 9
            $reflection = new ReflectionClass($className);
92
93 9
            if (!$reflection->isUserDefined()) {
94 9
                continue;
95
            }
96
97 9
            $matchedDirs = array_filter(
98 9
                $directories,
99 9
                static fn($directory) => str_starts_with($reflection->getFileName(), $directory)
100 9
            );
101
102 9
            if (count($matchedDirs) === 0) {
103 9
                continue;
104
            }
105
106 9
            if ($countInterfaces > 0) {
107 7
                $interfaces = $reflection->getInterfaces();
108 7
                $interfaces = array_map(static fn(ReflectionClass $class) => $class->getName(), $interfaces);
109
110 7
                if (count(array_intersect($this->interfaces, $interfaces)) !== $countInterfaces) {
111 5
                    continue;
112
                }
113
            }
114
115 9
            if ($countAttributes > 0) {
116 2
                $attributes = $reflection->getAttributes();
117 2
                $attributes = array_map(
118 2
                    static fn(ReflectionAttribute $attribute) => $attribute->getName(),
119 2
                    $attributes
120 2
                );
121
122 2
                if (count(array_intersect($this->attributes, $attributes)) !== $countAttributes) {
123 2
                    continue;
124
                }
125
            }
126
127 9
            if (($this->parentClass !== null) && !is_subclass_of($className, $this->parentClass)) {
128 1
                continue;
129
            }
130
131 9
            yield $className;
132
        }
133
    }
134
135
    /**
136
     * @psalm-suppress UnresolvableInclude
137
     */
138 9
    private function scanFiles(): void
139
    {
140 9
        $files = (new Finder())
141 9
            ->in($this->directories)
142 9
            ->name('*.php')
143 9
            ->sortByName()
144 9
            ->files();
145
146 9
        foreach ($files as $file) {
147 9
            require_once $file;
148
        }
149
    }
150
}
151