Lines
100.00%
9 / 9
Methods
100.00%
4 / 4
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| fromPoints | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | ||
| minimumPoints | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| __toString | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| 10 | final readonly class Rank implements Stringable | |
| 11 | { | |
| 12 | private const array MINIMUM_POINTS = [ | |
| 13 | 1 => 0, | |
| 14 | 2 => 900, | |
| 15 | 3 => 3600, | |
| 16 | 4 => 10_000, | |
| 17 | 5 => 22_500, | |
| 18 | 6 => 44_100, | |
| 19 | 7 => 78_400, | |
| 20 | 8 => 129_600, | |
| 21 | 9 => 202_500, | |
| 22 | 10 => 302_500, | |
| 23 | ]; | |
| 24 | ||
| 25 | /** @var int<1, 10> */ | |
| 26 | public int $value; | |
| 27 | ||
| 28 | public function __construct(int $value) | |
| 29 | { | |
| 30 | $this->value = Type\int_range(min: 1, max: 10)->assert($value); | |
| 31 | } | |
| 32 | ||
| 33 | public static function fromPoints(ClassPoints|Reputation $points): self | |
| 34 | { | |
| 35 | $rank = 1; | |
| 36 | ||
| 37 | foreach (self::MINIMUM_POINTS as $candidate => $minimum) { | |
| 38 | if ($points->value < $minimum) { | |
| 39 | break; | |
| 40 | } | |
| 41 | ||
| 42 | $rank = $candidate; | |
| 43 | } | |
| 44 | ||
| 45 | return new self($rank); | |
| 46 | } | |
| 47 | ||
| 48 | public function minimumPoints(): int | |
| 49 | { | |
| 50 | return self::MINIMUM_POINTS[$this->value]; | |
| 51 | } | |
| 52 | ||
| 53 | public function __toString(): string | |
| 54 | { | |
| 55 | return (string) $this->value; | |
| 56 | } | |
| 57 | } |