Passed
Push — main ( 05b26b...87f70c )
by mikhail
04:10
created

Comment::__toString()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
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, CommentConstantsInterface
10
{
11
    protected bool $exceedThreshold = false;
12
13 3
    public function hasExceededThreshold(): bool
14
    {
15 3
        return $this->exceedThreshold;
16
    }
17
18 14
    public function matchesPattern(string $token): bool
19
    {
20 14
        return (bool) preg_match($this->getPattern(), $token);
21
    }
22
23 1
    public function __toString(): string
24
    {
25 1
        return $this->getName();
26
    }
27
28
    /**
29
     * @param int $count
30
     * @param array<string, float> $thresholds
31
     *
32
     * @return string
33
     */
34 8
    public function getStatColor(int $count, array $thresholds): string
35
    {
36 8
        if (!isset($thresholds[$this->getName()])) {
37 6
            return 'white';
38
        }
39 2
        if ($this->isWithinThreshold($count, $thresholds)) {
40 1
            return 'green';
41
        }
42 1
        $this->exceedThreshold = true;
43 1
        return 'red';
44
    }
45
46
    /** @param array<string, float> $thresholds */
47 12
    protected function isWithinThreshold(int $count, array $thresholds): bool
48
    {
49 12
        $comparisonValue = $thresholds[static::NAME];
50
51 12
        if (static::COMPARISON_TYPE === '>=') {
0 ignored issues
show
introduced by
The condition static::COMPARISON_TYPE === '>=' is always false.
Loading history...
52 4
            return $count >= $comparisonValue;
53
        }
54
55 8
        return $count <= $comparisonValue;
56
    }
57
58 14
    public function getPattern(): string
59
    {
60 14
        return static::PATTERN;
61
    }
62
63 5
    public function getColor(): string
64
    {
65 5
        return static::COLOR;
66
    }
67
68 7
    public function getWeight(): float
69
    {
70 7
        return static::WEIGHT;
71
    }
72
73 11
    public function getName(): string
74
    {
75 11
        return static::NAME;
76
    }
77
}
78