Passed
Push — main ( 834936...e5772d )
by Michael
11:48
created

LocaleNumberFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 5
dl 0
loc 18
ccs 8
cts 8
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Formatters\Collection;
6
7
use Illuminate\Support\Collection;
8
use MichaelRubel\Formatters\Formatter;
9
use NumberFormatter;
10
11
class LocaleNumberFormatter implements Formatter
12
{
13
    /**
14
     * @var NumberFormatter
15
     */
16
    public NumberFormatter $formatter;
17
18
    /**
19
     * @param int|float|string|null $number
20
     * @param string|null           $locale
21
     * @param int                   $style
22
     * @param string|null           $pattern
23
     * @param int                   $fraction_digits
24
     */
25 7
    public function __construct(
26
        public int|float|string|null $number = null,
27
        public ?string $locale = null,
28
        public int $style = NumberFormatter::DECIMAL,
29
        public ?string $pattern = null,
30
        public int $fraction_digits = 2
31
    ) {
32 7
        $this->locale = $this->locale ?? app()->getLocale();
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

32
        $this->locale = $this->locale ?? app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
33
34 7
        $this->formatter = new NumberFormatter(
35 7
            $this->locale,
36 7
            $this->style,
37 7
            $this->pattern
38
        );
39
40 7
        $this->formatter->setAttribute(
41
            NumberFormatter::FRACTION_DIGITS,
42 7
            $this->fraction_digits
43
        );
44
    }
45
46
    /**
47
     * Format the number based on locale.
48
     *
49
     * @param Collection $items
50
     *
51
     * @return string
52
     */
53 7
    public function format(Collection $items): string
0 ignored issues
show
Unused Code introduced by
The parameter $items is not used and could be removed. ( Ignorable by Annotation )

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

53
    public function format(/** @scrutinizer ignore-unused */ Collection $items): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55 7
        return $this->formatter->format(
56 7
            (float) $this->number
57
        );
58
    }
59
}
60