Passed
Push — main ( 5c2914...7703d5 )
by mikhail
03:07
created

CommentFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 11
eloc 21
c 4
b 0
f 1
dl 0
loc 50
ccs 20
cts 24
cp 0.8333
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A classifyComment() 0 14 5
A getCommentType() 0 8 3
A getCommentTypes() 0 4 2
A __construct() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\Comments;
6
7
use Generator;
8
9
use function in_array;
10
11
final class CommentFactory
12
{
13
    /** @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...
14
    private array $commentTypes;
15
16
    /**
17
     * @param string[] $allowedTypes
18
     */
19 13
    public function __construct(private readonly array $allowedTypes = [])
20
    {
21 13
        $this->commentTypes =  [
22 13
            new TodoComment(),
23 13
            new FixMeComment(),
24 13
            new RegularComment(),
25 13
            new LicenseComment(),
26 13
            new DocBlockComment(),
27 13
        ];
28
    }
29
30
    public function getCommentTypes(): Generator
31
    {
32
        foreach ($this->commentTypes as $commentType) {
33
            yield $commentType;
34
        }
35
    }
36
37 2
    public function getCommentType(string $name): ?CommentTypeInterface
38
    {
39 2
        foreach ($this->commentTypes as $commentType) {
40 2
            if ($commentType->getName() === $name) {
41 2
                return $commentType;
42
            }
43
        }
44 2
        return null;
45
    }
46
47 9
    public function classifyComment(string $token): ?CommentTypeInterface
48
    {
49 9
        foreach ($this->commentTypes as $commentType) {
50 9
            if (! $commentType->matchesPattern($token)) {
51 7
                continue;
52
            }
53
            if (
54 9
                empty($this->allowedTypes)
55 9
                || in_array($commentType->getName(), $this->allowedTypes, true)
56
            ) {
57 9
                return $commentType;
58
            }
59
        }
60
        return null;
61
    }
62
}
63