Completed
Pull Request — master (#56)
by
unknown
22:27
created

helpers.php ➔ phone()   C

Complexity

Conditions 8
Paths 33

Size

Total Lines 40
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.048

Importance

Changes 0
Metric Value
cc 8
eloc 20
nc 33
nop 0
dl 0
loc 40
ccs 10
cts 11
cp 0.9091
crap 8.048
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Support\Facades\App;
4
use libphonenumber\PhoneNumberFormat;
5
use Illuminate\Support\Facades\Validator;
6
7
if (! function_exists('phone')) {
8
    /**
9
     * Get the PhoneNumberUtil or format a phone number for display.
10
     *
11
     * @return \libphonenumber\PhoneNumberUtil|string
12
     */
13
    function phone()
14 3
    {
15
        $lib = App::make('libphonenumber');
16 3
17 3
        if (! $arguments = func_get_args()) {
18
            return $lib;
19
        }
20 3
21 3
        $phone = $arguments[0];
22 3
        
23
        $countries = isset($arguments[1]) ? (is_array($arguments[1]) ? $arguments[1] : [$arguments[1]]) : [];
24 3
        
25 3
        $format = isset($arguments[2]) ? $arguments[2] : PhoneNumberFormat::INTERNATIONAL;
26 2
27 1
        $validator = null;
0 ignored issues
show
Unused Code introduced by
$validator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
28
29
        for($i = 0; $i < sizeof($countries); $i++)
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
30
        {
31
            if($countries[$i] === null)
32
            {
33
                return $lib->format(
34
                    $lib->parse($phone, App::getLocale()),
35
                    $format
36
                );
37
            }
38
39
            $validator = Validator::make(['phone' => $phone], [
40
                'phone' => 'required|phone:' . $countries[$i],
41
            ]);
42
43
            if (!$validator->fails()) {
44
                return $lib->format(
45
                    $lib->parse($phone, $countries[$i]),
46
                    $format
47
                );
48
            }
49
        }
50
51
        return $lib;
52
    }
53
}
54
55
if (! function_exists('phone_format')) {
56
    /**
57
     * Formats a phone number and country for display.
58
     *
59
     * @param string          $phone
60
     * @param string|string[] $countries
61
     * @param int|null        $format
62
     * @return string
63
     *
64
     * @deprecated 2.8.0
65
     */
66
    function phone_format($phone, $countries = null, $format = PhoneNumberFormat::INTERNATIONAL)
67
    {
68
        return phone($phone, $countries, $format);
69
    }
70
}
71