Passed
Push — main ( b8dd52...35d689 )
by mikhail
03:26
created

Comment::getPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\Comments;
6
7
use Stringable;
8
9
abstract class Comment implements Stringable, CommentTypeInterface
10
{
11
    protected bool $exceedThreshold = false;
12
13
    public function hasExceededThreshold(): bool
14
    {
15
        return $this->exceedThreshold;
16
    }
17
18 9
    public function matchesPattern(string $token): bool
19
    {
20 9
        return (bool) preg_match($this->getPattern(), $token);
21
    }
22
23
    public function __toString(): string
24
    {
25
        return $this->getName();
26
    }
27
28
    public function getStatColor(int $count, array $thresholds): string
29
    {
30
        if (!isset($thresholds[$this->getName()])) {
31
            return 'white';
32
        }
33
        if ($this->isWithinThreshold($count, $thresholds)) {
34
            return 'green';
35
        }
36
        $this->exceedThreshold = true;
37
        return 'red';
38
    }
39
40
    protected function isWithinThreshold(int $count, array $thresholds): bool
41
    {
42
        $comparisonValue = $thresholds[static::NAME];
0 ignored issues
show
Bug introduced by
The constant SavinMikhail\CommentsDen...\Comments\Comment::NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
43
44
        if (static::COMPARISON_TYPE === '>=') {
0 ignored issues
show
Bug introduced by
The constant SavinMikhail\CommentsDen...omment::COMPARISON_TYPE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
45
            return $count >= $comparisonValue;
46
        }
47
48
        return $count <= $comparisonValue;
49
    }
50
51 9
    public function getPattern(): string
52
    {
53 9
        return static::PATTERN;
0 ignored issues
show
Bug introduced by
The constant SavinMikhail\CommentsDen...mments\Comment::PATTERN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
54
    }
55
56
    public function getColor(): string
57
    {
58
        return static::COLOR;
0 ignored issues
show
Bug introduced by
The constant SavinMikhail\CommentsDen...Comments\Comment::COLOR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
59
    }
60
61 1
    public function getWeight(): float
62
    {
63 1
        return static::WEIGHT;
0 ignored issues
show
Bug introduced by
The constant SavinMikhail\CommentsDen...omments\Comment::WEIGHT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
64
    }
65
66 1
    public function getName(): string
67
    {
68 1
        return static::NAME;
0 ignored issues
show
Bug introduced by
The constant SavinMikhail\CommentsDen...\Comments\Comment::NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
69
    }
70
}
71