Failed Conditions
Pull Request — master (#4127)
by Owen
15:28
created

Accounting::format()   B

Complexity

Conditions 10
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 20
rs 7.6666
c 0
b 0
f 0
ccs 17
cts 17
cp 1
cc 10
nc 2
nop 0
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 ?string $overrideNegative = self::NEGATIVE_PARENS;
13
14
    /**
15
     * @throws Exception if the Intl extension and ICU version don't support Accounting formats
16
     */
17
    protected function getLocaleFormat(): string
18
    {
19
        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 27
        // Scrutinizer does not recognize CURRENCY_ACCOUNTING
26
        $formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY_ACCOUNTING);
27
        $mask = $formatter->format($this->stripLeadingRLM);
28
        if ($this->decimals === 0) {
29
            $mask = (string) preg_replace('/\.0+/miu', '', $mask);
30
        }
31
32
        return str_replace('¤', $this->formatCurrencyCode(), $mask);
33
    }
34 27
35 27
    public static function icuVersion(): float
36 27
    {
37 27
        [$major, $minor] = explode('.', INTL_ICU_VERSION);
38 27
39 27
        return (float) "{$major}.{$minor}";
40 27
    }
41
42
    private function formatCurrencyCode(): string
43
    {
44
        if ($this->locale === null) {
45
            return $this->currencyCode . '*';
46 20
        }
47
48 20
        return "[\${$this->currencyCode}-{$this->locale}]";
49
    }
50
}
51