StringExtended   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
B mysql_format() 0 18 7
A mysql_lpad() 0 9 5
1
<?php
2
3
namespace Mhorninger\MySQLite\MySQL;
4
5
use NumberFormatter;
6
7
trait StringExtended
8
{
9
    /**
10
     * Format a number according to the nubmer of decimals provided and culture.
11
     * @param mixed... number, decimals, culture.
12
     */
13
    // phpcs:disable
14
    public static function mysql_format()
15
    {
16
        //phpcs:enable
17
        $args = func_get_args();
18
        $length = count($args);
19
        if ($args && 0 < $length && ($number = $args[0]) != null) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $args of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
20
            $decimals = 1 < $length ? $args[1] : 0;
21
            $culture = 2 < $length ? $args[2] : 'en_US';
22
            $pattern = '#,##0';
23
            if ($decimals > 0) {
24
                $pattern = $pattern.'.';
25
                $base = strlen($pattern);
26
                $decimals = $base + $decimals;
27
                $pattern = str_pad($pattern, $decimals, '0', STR_PAD_RIGHT);
28
            }
29
            $formatter = new NumberFormatter($culture, NumberFormatter::PATTERN_DECIMAL, $pattern);
30
31
            return $formatter->format($number);
32
        }
33
    }
34
35
    // phpcs:disable
36
    public static function mysql_lpad($string, $length, $pad)
37
    {
38
        //phpcs:enable
39
        if ($string && $length && $pad) {
40
            if (strlen($string) < $length) {
41
                return str_pad($string, $length, $pad, STR_PAD_LEFT);
42
            }
43
44
            return substr($string, 0, $length);
45
        }
46
    }
47
}
48