1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace SavinMikhail\CommentsDensity\AnalyzeComments\Comments; |
||
6 | |||
7 | use Stringable; |
||
8 | |||
9 | abstract class Comment implements Stringable, CommentTypeInterface, CommentConstantsInterface |
||
10 | { |
||
11 | protected bool $exceedThreshold = false; |
||
12 | |||
13 | 10 | final public function hasExceededThreshold(): bool |
|
14 | { |
||
15 | 10 | return $this->exceedThreshold; |
|
16 | } |
||
17 | |||
18 | 23 | final public function matchesPattern(string $token): bool |
|
19 | { |
||
20 | 23 | 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 array<string, float> $thresholds |
||
30 | */ |
||
31 | 13 | final public function getStatColor(int $count, array $thresholds): string |
|
32 | { |
||
33 | 13 | if (!isset($thresholds[$this->getName()])) { |
|
34 | 1 | return 'white'; |
|
35 | } |
||
36 | 12 | if ($this->isWithinThreshold($count, $thresholds)) { |
|
37 | 7 | return 'green'; |
|
38 | } |
||
39 | 5 | $this->exceedThreshold = true; |
|
40 | |||
41 | 5 | return 'red'; |
|
42 | } |
||
43 | |||
44 | 23 | final public function getPattern(): string |
|
45 | { |
||
46 | 23 | return static::PATTERN; |
|
47 | } |
||
48 | |||
49 | 14 | final public function getColor(): string |
|
50 | { |
||
51 | 14 | return static::COLOR; |
|
52 | } |
||
53 | |||
54 | 2 | final public function getWeight(): float |
|
55 | { |
||
56 | 2 | return static::WEIGHT; |
|
57 | } |
||
58 | |||
59 | 30 | final public function getName(): string |
|
60 | { |
||
61 | 30 | return static::NAME; |
|
62 | } |
||
63 | |||
64 | /** @param array<string, float> $thresholds */ |
||
65 | 22 | protected function isWithinThreshold(int $count, array $thresholds): bool |
|
66 | { |
||
67 | 22 | $comparisonValue = $thresholds[static::NAME]; |
|
68 | |||
69 | 22 | if (static::COMPARISON_TYPE === '>=') { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
70 | 8 | return $count >= $comparisonValue; |
|
71 | } |
||
72 | |||
73 | 14 | return $count <= $comparisonValue; |
|
74 | } |
||
75 | } |
||
76 |