Failed Conditions
Push — master ( a2282e...ad0b68 )
by Mark
40s queued 35s
created

NumberBase   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 23
c 1
b 0
f 0
dl 0
loc 70
ccs 20
cts 22
cp 0.9091
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A setLocale() 0 12 3
A setDecimals() 0 3 2
A validateLocale() 0 15 5
A format() 0 3 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
4
5
use NumberFormatter;
6
use PhpOffice\PhpSpreadsheet\Exception;
7
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
8
9
abstract class NumberBase
10
{
11
    protected const MAX_DECIMALS = 30;
12
13
    protected int $decimals = 2;
14
15
    protected ?string $locale = null;
16
17
    protected ?string $fullLocale = null;
18
19
    protected ?string $localeFormat = null;
20
21 70
    public function setDecimals(int $decimals = 2, ?string $locale = null): void
0 ignored issues
show
Unused Code introduced by
The parameter $locale 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

21
    public function setDecimals(int $decimals = 2, /** @scrutinizer ignore-unused */ ?string $locale = null): void

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...
22
    {
23 70
        $this->decimals = ($decimals > self::MAX_DECIMALS) ? self::MAX_DECIMALS : max($decimals, 0);
24
    }
25
26
    /**
27
     * Setting a locale will override any settings defined in this class.
28
     *
29
     * @throws Exception If the locale code is not a valid format
30
     */
31 70
    public function setLocale(?string $locale = null): void
32
    {
33 70
        if ($locale === null) {
34 70
            $this->localeFormat = $this->locale = $this->fullLocale = null;
35
36 70
            return;
37
        }
38
39 40
        $this->locale = $this->validateLocale($locale);
40
41 35
        if (class_exists(NumberFormatter::class)) {
42 35
            $this->localeFormat = $this->getLocaleFormat();
43
        }
44
    }
45
46
    /**
47
     * Stub: should be implemented as a concrete method in concrete wizards.
48
     */
49
    abstract protected function getLocaleFormat(): string;
50
51
    /**
52
     * @throws Exception If the locale code is not a valid format
53
     */
54 40
    private function validateLocale(string $locale): string
55
    {
56 40
        if (preg_match(Locale::STRUCTURE, $locale, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
57 5
            throw new Exception("Invalid locale code '{$locale}'");
58
        }
59
60 35
        ['language' => $language, 'script' => $script, 'country' => $country] = $matches;
61
        // Set case and separator to match standardised locale case
62 35
        $language = strtolower($language ?? '');
63 35
        $script = ($script === null) ? null : ucfirst(strtolower($script));
64 35
        $country = ($country === null) ? null : strtoupper($country);
65
66 35
        $this->fullLocale = implode('-', array_filter([$language, $script, $country]));
67
68 35
        return $country === null ? $language : "{$language}-{$country}";
69
    }
70
71
    public function format(): string
72
    {
73
        return NumberFormat::FORMAT_GENERAL;
74
    }
75
76 62
    public function __toString(): string
77
    {
78 62
        return $this->format();
79
    }
80
}
81