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

CommentFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 84%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 11
eloc 23
c 3
b 0
f 1
dl 0
loc 50
ccs 21
cts 25
cp 0.84
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A classifyComment() 0 14 5
A getCommentType() 0 8 3
A __construct() 0 11 1
A getCommentTypes() 0 4 2
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