Failed Conditions
Push — master ( a2282e...ad0b68 )
by Mark
40s queued 35s
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 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 20
ccs 17
cts 17
cp 1
rs 7.6666
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 Currency
9
{
10
    /**
11
     * @param string $currencyCode the currency symbol or code to display for this mask
12
     * @param int $decimals number of decimal places to display, in the range 0-30
13
     * @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not
14
     * @param bool $currencySymbolPosition indicates whether the currency symbol comes before or after the value
15
     *              Possible values are Currency::LEADING_SYMBOL and Currency::TRAILING_SYMBOL
16
     * @param bool $currencySymbolSpacing indicates whether there is spacing between the currency symbol and the value
17
     *              Possible values are Currency::SYMBOL_WITH_SPACING and Currency::SYMBOL_WITHOUT_SPACING
18
     * @param ?string $locale Set the locale for the currency format; or leave as the default null.
19
     *          If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
20
     *          Note that setting a locale will override any other settings defined in this class
21
     *          other than the currency code.
22
     *
23
     * @throws Exception If a provided locale code is not a valid format
24
     */
25 17
    public function __construct(
26
        string $currencyCode = '$',
27
        int $decimals = 2,
28
        bool $thousandsSeparator = true,
29
        bool $currencySymbolPosition = self::LEADING_SYMBOL,
30
        bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING,
31
        ?string $locale = null
32
    ) {
33 17
        $this->setCurrencyCode($currencyCode);
34 17
        $this->setThousandsSeparator($thousandsSeparator);
35 17
        $this->setDecimals($decimals);
36 17
        $this->setCurrencySymbolPosition($currencySymbolPosition);
37 17
        $this->setCurrencySymbolSpacing($currencySymbolSpacing);
38 17
        $this->setLocale($locale);
39
    }
40
41
    /**
42
     * @throws Exception if the Intl extension and ICU version don't support Accounting formats
43
     */
44 10
    protected function getLocaleFormat(): string
45
    {
46 10
        if (version_compare(PHP_VERSION, '7.4.1', '<')) {
47
            throw new Exception('The Intl extension does not support Accounting Formats below PHP 7.4.1');
48
        }
49
50 10
        if ($this->icuVersion() < 53.0) {
51
            throw new Exception('The Intl extension does not support Accounting Formats without ICU 53');
52
        }
53
54 10
        $formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY_ACCOUNTING); // @phpstan-ignore-line
2 ignored issues
show
Bug introduced by
It seems like $this->fullLocale can also be of type null; however, parameter $locale of PhpOffice\PhpSpreadsheet...d\Locale::__construct() does only seem to accept string, 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

54
        $formatter = new Locale(/** @scrutinizer ignore-type */ $this->fullLocale, NumberFormatter::CURRENCY_ACCOUNTING); // @phpstan-ignore-line
Loading history...
Bug introduced by
The constant NumberFormatter::CURRENCY_ACCOUNTING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55
56 9
        return str_replace('¤', $this->formatCurrencyCode(), $formatter->format());
57
    }
58
59 10
    private function icuVersion(): float
60
    {
61 10
        [$major, $minor] = explode('.', INTL_ICU_VERSION);
62
63 10
        return (float) "{$major}.{$minor}";
64
    }
65
66 15
    private function formatCurrencyCode(): string
67
    {
68 15
        if ($this->locale === null) {
69 6
            return $this->currencyCode . '*';
70
        }
71
72 9
        return "[\${$this->currencyCode}-{$this->locale}]";
73
    }
74
75 15
    public function format(): string
76
    {
77 15
        if ($this->localeFormat !== null) {
78 9
            return $this->localeFormat;
79
        }
80
81 6
        return sprintf(
82 6
            '_-%s%s%s0%s%s%s_-',
83 6
            $this->currencySymbolPosition === self::LEADING_SYMBOL ? $this->formatCurrencyCode() : null,
84 6
            (
85 6
                $this->currencySymbolPosition === self::LEADING_SYMBOL &&
86 6
                $this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING
87 6
            ) ? ' ' : '',
88 6
            $this->thousandsSeparator ? '#,##' : null,
89 6
            $this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null,
90 6
            (
91 6
                $this->currencySymbolPosition === self::TRAILING_SYMBOL &&
92 6
                $this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING
93 6
            ) ? ' ' : '',
94 6
            $this->currencySymbolPosition === self::TRAILING_SYMBOL ? $this->formatCurrencyCode() : null
95 6
        );
96
    }
97
}
98