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

Comment   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 30.76%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 19
c 2
b 0
f 0
dl 0
loc 60
ccs 8
cts 26
cp 0.3076
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A matchesPattern() 0 3 1
A hasExceededThreshold() 0 3 1
A getColor() 0 3 1
A getStatColor() 0 10 3
A getName() 0 3 1
A getWeight() 0 3 1
A getPattern() 0 3 1
A isWithinThreshold() 0 9 2
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