Failed Conditions
Push — master ( ff7329...d32c3b )
by Sylvain
02:29
created

Format   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 55.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 8
c 1
b 0
f 0
dl 0
loc 28
ccs 5
cts 9
cp 0.5556
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A truncate() 0 8 2
A money() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix;
6
7
use Money\Currencies\ISOCurrencies;
8
use Money\Formatter\DecimalMoneyFormatter;
9
use Money\Money;
10
11
abstract class Format
12
{
13
    /**
14
     * Truncate a string and append '…' at the end
15
     *
16
     * @param string $ellipsis the string to indicate truncation happened
17
     *
18
     * @return string truncated string
19
     */
20 5
    public static function truncate(string $string, int $maxLength, string $ellipsis = '…'): string
21
    {
22 5
        if (mb_strlen($string) > $maxLength) {
23 3
            $string = mb_substr($string, 0, $maxLength - mb_strlen($ellipsis));
24 3
            $string .= $ellipsis;
25
        }
26
27 5
        return $string;
28
    }
29
30
    /**
31
     * Shortcut to format money
32
     */
33
    public static function money(Money $money): string
34
    {
35
        $currencies = new ISOCurrencies();
36
        $moneyFormatter = new DecimalMoneyFormatter($currencies);
37
38
        return $moneyFormatter->format($money);
39
    }
40
}
41