PhoneFormatter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 11
c 2
b 0
f 0
dl 0
loc 40
ccs 11
cts 13
cp 0.8462
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 3 1
B format() 0 14 7
A a() 0 3 1
1
<?php
2
3
4
namespace floor12\phone;
5
6
use yii\helpers\Html;
7
8
class PhoneFormatter
9
{
10
11
    /**
12
     * @param $phone
13
     * @return string
14
     */
15
    public static function run($phone)
16
    {
17
        return self::a($phone);
18
    }
19
20
    /**
21
     * @param $phone
22
     * @param array $options
23
     * @return string
24
     */
25 3
    public static function a($phone, array $options = [], $countCodeLength = 1)
26
    {
27 3
        return Html::a(self::format($phone, $countCodeLength), "tel:+{$phone}", $options);
28
    }
29
30
    /**
31
     * @param $phone
32
     * @return string
33
     */
34 7
    public static function format($phone, $countCodeLength = 1)
35
    {
36
37 7
        if (preg_match('/^(\d{1})(\d{3})(\d{3})(\d{2})(\d{2})$/', $phone, $matches) && $countCodeLength == 1)
38 2
            return "+" . $matches[1] . ' (' . $matches[2] . ') ' . $matches[3] . "-" . $matches[4] . '-' . $matches[5];
39
40 5
        if (preg_match('/^(\d{2})(\d{3})(\d{3})(\d{2})(\d{2})$/', $phone, $matches) && $countCodeLength == 1)
41 2
            return "+" . $matches[1] . ' (' . $matches[2] . ') ' . $matches[3] . "-" . $matches[4] . '-' . $matches[5];
42
43 3
        if ($countCodeLength == 2)
44 2
            if (preg_match('/^(\d{2})(\d{3})(\d{2})(\d{2})(\d{2})$/', $phone, $matches))
45 2
                return "+" . $matches[1] . ' (' . $matches[2] . ') ' . $matches[3] . "-" . $matches[4] . '-' . $matches[5];
46
47 1
        return $phone;
48
    }
49
}