Passed
Push — main ( a82980...5a08b8 )
by mikhail
14:17
created

CommentFactory::classifyComment()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.0488

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
eloc 8
c 2
b 0
f 1
nc 4
nop 1
dl 0
loc 14
ccs 7
cts 8
cp 0.875
crap 5.0488
rs 9.6111
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 readonly class CommentFactory
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 10 at column 6
Loading history...
11
{
12
    /** @var array<array-key, CommentTypeInterface> */
13
    private array $commentTypes;
14
15
    /**
16
     * @param string[] $allowedTypes
17
     */
18 19
    public function __construct(private array $allowedTypes = [])
19
    {
20 19
        $this->commentTypes =  [
21 19
            new TodoComment(),
22 19
            new FixMeComment(),
23 19
            new RegularComment(),
24 19
            new LicenseComment(),
25 19
            new DocBlockComment(),
26 19
        ];
27
    }
28
29 3
    public function getCommentTypes(): Generator
30
    {
31 3
        foreach ($this->commentTypes as $commentType) {
32 3
            yield $commentType;
33
        }
34
    }
35
36 7
    public function getCommentType(string $name): ?CommentTypeInterface
37
    {
38 7
        foreach ($this->commentTypes as $commentType) {
39 7
            if ($commentType->getName() === $name) {
40 7
                return $commentType;
41
            }
42
        }
43 7
        return null;
44
    }
45
46 14
    public function classifyComment(string $token): ?CommentTypeInterface
47
    {
48 14
        foreach ($this->commentTypes as $commentType) {
49 14
            if (! $commentType->matchesPattern($token)) {
50 12
                continue;
51
            }
52
            if (
53 14
                empty($this->allowedTypes)
54 14
                || in_array($commentType->getName(), $this->allowedTypes, true)
55
            ) {
56 14
                return $commentType;
57
            }
58
        }
59
        return null;
60
    }
61
}
62