Passed
Push — 6.0 ( 4ac4e1...87e1d7 )
by Olivier
01:56
created

Operands   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 35
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 27 4
A to_array() 0 11 1
A from() 0 3 1
1
<?php
2
3
namespace ICanBoogie\CLDR\Supplemental\Plurals;
4
5
use ICanBoogie\CLDR\Numbers\Number;
6
7
use function abs;
8
9
/**
10
 * Representation of plural operands.
11
 *
12
 * @internal
13
 *
14
 * @link https://www.unicode.org/reports/tr35/tr35-72/tr35-numbers.html#Operands
15
 */
16
final class Operands
17
{
18
    /**
19
     * @param float|int|numeric-string $number
0 ignored issues
show
Documentation Bug introduced by
The doc comment float|int|numeric-string at position 4 could not be parsed: Unknown type name 'numeric-string' at position 4 in float|int|numeric-string.
Loading history...
20
     */
21
    public static function from(float|int|string $number): self
22
    {
23
        return OperandsCache::get($number, static fn(): self => new self($number));
24
    }
25
26
    public readonly int|float $n;
27
    public readonly int $i;
28
    public readonly int $v;
29
    public readonly int $w;
30
    public readonly int $f;
31
    public readonly int $t;
32
    public readonly int $e;
33
34
    /**
35
     * @param float|int|numeric-string $number
0 ignored issues
show
Documentation Bug introduced by
The doc comment float|int|numeric-string at position 4 could not be parsed: Unknown type name 'numeric-string' at position 4 in float|int|numeric-string.
Loading history...
36
     */
37
    private function __construct(float|int|string $number)
38
    {
39
        $e = 0;
40
        $number = Number::expand_compact_decimal_exponent($number, $e);
41
42
        [ $integer, $fractional ] = Number::parse($number);
43
44
        $n = abs($number);
0 ignored issues
show
Bug introduced by
It seems like $number can also be of type string; however, parameter $num of abs() does only seem to accept double|integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        $n = abs(/** @scrutinizer ignore-type */ $number);
Loading history...
45
46
        if ($fractional === null || (int)$fractional === 0) {
47
            $n = (int)$n;
48
        }
49
50
        $this->n = $n;
0 ignored issues
show
Bug introduced by
The property n is declared read-only in ICanBoogie\CLDR\Supplemental\Plurals\Operands.
Loading history...
51
        $this->i = $integer;
0 ignored issues
show
Bug introduced by
The property i is declared read-only in ICanBoogie\CLDR\Supplemental\Plurals\Operands.
Loading history...
52
        $this->e = $e;
0 ignored issues
show
Bug introduced by
The property e is declared read-only in ICanBoogie\CLDR\Supplemental\Plurals\Operands.
Loading history...
53
54
        if ($fractional === null) {
55
            $this->v = 0;
0 ignored issues
show
Bug introduced by
The property v is declared read-only in ICanBoogie\CLDR\Supplemental\Plurals\Operands.
Loading history...
56
            $this->w = 0;
0 ignored issues
show
Bug introduced by
The property w is declared read-only in ICanBoogie\CLDR\Supplemental\Plurals\Operands.
Loading history...
57
            $this->f = 0;
0 ignored issues
show
Bug introduced by
The property f is declared read-only in ICanBoogie\CLDR\Supplemental\Plurals\Operands.
Loading history...
58
            $this->t = 0;
0 ignored issues
show
Bug introduced by
The property t is declared read-only in ICanBoogie\CLDR\Supplemental\Plurals\Operands.
Loading history...
59
        } else {
60
            $this->v = strlen($fractional);
61
            $this->w = strlen(rtrim($fractional, '0'));
62
            $this->f = (int)ltrim($fractional, '0');
63
            $this->t = (int)trim($fractional, '0');
64
        }
65
    }
66
67
    /**
68
     * @return array{ n: float|int, i: int, v: int, w: int, f: int, t: int, e: int }
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
69
     */
70
    public function to_array(): array
71
    {
72
        return [
73
74
            'n' => $this->n,
75
            'i' => $this->i,
76
            'v' => $this->v,
77
            'w' => $this->w,
78
            'f' => $this->f,
79
            't' => $this->t,
80
            'e' => $this->e,
81
82
        ];
83
    }
84
}
85