Failed Conditions
Pull Request — master (#4127)
by Owen
39:04 queued 27:50
created

Accounting::icuVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
4
5
use NumberFormatter;
6
use PhpOffice\PhpSpreadsheet\Exception;
7
8
class Accounting extends CurrencyBase
9
{
10
    protected ?bool $overrideSpacing = true;
11
12
    protected ?CurrencyNegative $overrideNegative = CurrencyNegative::parentheses;
13
14
    /**
15
     * @throws Exception if the Intl extension and ICU version don't support Accounting formats
16
     */
17 20
    protected function getLocaleFormat(): string
18
    {
19 20
        if (self::icuVersion() < 53.0) {
20
            // @codeCoverageIgnoreStart
21
            throw new Exception('The Intl extension does not support Accounting Formats without ICU 53');
22
            // @codeCoverageIgnoreEnd
23
        }
24
25
        // Scrutinizer does not recognize CURRENCY_ACCOUNTING
26 20
        $formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY_ACCOUNTING);
27 19
        $mask = $formatter->format($this->stripLeadingRLM);
28 19
        if ($this->decimals === 0) {
29 9
            $mask = (string) preg_replace('/\.0+/miu', '', $mask);
30
        }
31
32 19
        return str_replace('¤', $this->formatCurrencyCode(), $mask);
33
    }
34
35 21
    public static function icuVersion(): float
36
    {
37 21
        [$major, $minor] = explode('.', INTL_ICU_VERSION);
38
39 21
        return (float) "{$major}.{$minor}";
40
    }
41
42 19
    private function formatCurrencyCode(): string
43
    {
44 19
        if ($this->locale === null) {
45
            return $this->currencyCode . '*';
46
        }
47
48 19
        return "[\${$this->currencyCode}-{$this->locale}]";
49
    }
50
}
51