|
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
|
|
|
final public function hasExceededThreshold(): bool |
|
14
|
|
|
{ |
|
15
|
|
|
return $this->exceedThreshold; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
final public function matchesPattern(string $token): bool |
|
19
|
|
|
{ |
|
20
|
|
|
return (bool) preg_match($this->getPattern(), $token); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function __toString(): string |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->getName(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param array<string, float> $thresholds |
|
30
|
|
|
*/ |
|
31
|
|
|
final public function getStatColor(int $count, array $thresholds): string |
|
32
|
|
|
{ |
|
33
|
|
|
if (!isset($thresholds[$this->getName()])) { |
|
34
|
|
|
return 'white'; |
|
35
|
|
|
} |
|
36
|
|
|
if ($this->isWithinThreshold($count, $thresholds)) { |
|
37
|
|
|
return 'green'; |
|
38
|
|
|
} |
|
39
|
|
|
$this->exceedThreshold = true; |
|
40
|
|
|
|
|
41
|
|
|
return 'red'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
final public function getPattern(): string |
|
45
|
|
|
{ |
|
46
|
|
|
return static::PATTERN; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
final public function getColor(): string |
|
50
|
|
|
{ |
|
51
|
|
|
return static::COLOR; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
final public function getWeight(): float |
|
55
|
|
|
{ |
|
56
|
|
|
return static::WEIGHT; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
final public function getName(): string |
|
60
|
|
|
{ |
|
61
|
|
|
return static::NAME; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @param array<string, float> $thresholds */ |
|
65
|
|
|
protected function isWithinThreshold(int $count, array $thresholds): bool |
|
66
|
|
|
{ |
|
67
|
|
|
$comparisonValue = $thresholds[static::NAME]; |
|
68
|
|
|
|
|
69
|
|
|
if (static::COMPARISON_TYPE === '>=') { |
|
|
|
|
|
|
70
|
|
|
return $count >= $comparisonValue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $count <= $comparisonValue; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|