Passed
Push — main ( e2cb9c...09e2a2 )
by mikhail
03:47
created

CommentFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\Comments;
6
7
use Generator;
8
use function in_array;
9
10
final class CommentFactory
11
{
12
    /** @var array<array-key, CommentTypeInterface> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, CommentTypeInterface> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, CommentTypeInterface>.
Loading history...
13
    private array $commentTypes;
14
    private array $allowedTypes;
15
16 13
    public function __construct(array $allowedTypes = [])
17
    {
18 13
        $this->commentTypes =  [
19 13
            new TodoComment(),
20 13
            new FixMeComment(),
21 13
            new RegularComment(),
22 13
            new LicenseComment(),
23 13
            new DocBlockComment(),
24 13
        ];
25
26 13
        $this->allowedTypes = $allowedTypes;
27
    }
28
29
    public function getCommentTypes(): Generator
30
    {
31
        foreach ($this->commentTypes as $commentType) {
32
            yield $commentType;
33
        }
34
    }
35
36 2
    public function getCommentType(string $name): ?CommentTypeInterface
37
    {
38 2
        foreach ($this->commentTypes as $commentType) {
39 2
            if ($commentType->getName() === $name) {
40 2
                return $commentType;
41
            }
42
        }
43 2
        return null;
44
    }
45
46 9
    public function classifyComment(string $token): ?CommentTypeInterface
47
    {
48 9
        foreach ($this->commentTypes as $commentType) {
49 9
            if (! $commentType->matchesPattern($token)) {
50 7
                continue;
51
            }
52
            if (
53 9
                empty($this->allowedTypes)
54 9
                || in_array($commentType->getName(), $this->allowedTypes, true)
55
            ) {
56 9
                return $commentType;
57
            }
58
        }
59
        return null;
60
    }
61
}
62