Util::formatMobiles()   C
last analyzed

Complexity

Conditions 11
Paths 21

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 21
nc 21
nop 1

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 Toplan\PhpSms;
4
5
use SuperClosure\Serializer;
6
7
class Util
8
{
9
    protected static $closureSerializer = null;
10
11
    public static function operateArray(array &$array, $key, $value = null, $default = null, \Closure $setter = null, $override = false, $willOverride = null, $isSet = false)
12
    {
13
        if (!$isSet && ($key === null || is_string($key) || is_int($key)) && $value === null) {
14
            return $key === null ? $array :
15
                (isset($array[$key]) ? $array[$key] : $default);
16
        }
17
        if ($override) {
18
            if (is_callable($willOverride)) {
19
                call_user_func_array($willOverride, [$array]);
20
            }
21
            $array = [];
22
        }
23
        if (is_array($key) || is_object($key)) {
24
            foreach ($key as $k => $v) {
25
                self::operateArray($array, $k, $v, $default, $setter, false, null, true);
26
            }
27
        } elseif (is_callable($setter)) {
28
            call_user_func_array($setter, [$key, $value]);
29
        } else {
30
            $array[$key] = $value;
31
        }
32
33
        return $array;
34
    }
35
36
    public static function pullFromArray(array &$options, $key)
37
    {
38
        $value = null;
39
        if (!isset($options[$key])) {
40
            return $value;
41
        }
42
        $value = $options[$key];
43
        unset($options[$key]);
44
45
        return $value;
46
    }
47
48
    public static function getClosureSerializer()
49
    {
50
        if (empty(self::$closureSerializer)) {
51
            self::$closureSerializer = new Serializer();
52
        }
53
54
        return self::$closureSerializer;
55
    }
56
57
    public static function formatMobiles($target)
58
    {
59
        if (!is_array($target)) {
60
            return [$target];
61
        }
62
        $list = [];
63
        $nation = $number = null;
64
        $count = count($target);
65
        if ($count === 2) {
66
            $firstItem = $target[0];
67
            if (is_int($firstItem) && $firstItem > 0 && $firstItem <= 9999) {
68
                $nation = $firstItem;
69
                $number = $target[1];
70
            }
71
            if (is_string($firstItem) && strlen($firstItem = trim($firstItem)) <= 4) {
72
                $nation = $firstItem;
73
                $number = $target[1];
74
            }
75
        }
76
        if (!is_null($nation)) {
77
            return [compact('nation', 'number')];
78
        }
79
        foreach ($target as $childTarget) {
80
            $childList = self::formatMobiles($childTarget);
81
            foreach ($childList as $childListItem) {
82
                array_push($list, $childListItem);
83
            }
84
        }
85
86
        return $list;
87
    }
88
}
89