is_serialized()   C
last analyzed

Complexity

Conditions 13
Paths 12

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 25
c 2
b 0
f 0
nc 12
nop 1
dl 0
loc 34
ccs 0
cts 24
cp 0
crap 182
rs 6.6166

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
if (! function_exists('value')) {
3
    /**
4
     * Если аргумент это замыкание, то вычисляется его значение и отдается в виде результата
5
     * В противном случае возвращается сам аргумент
6
     *
7
     * @param  mixed $value
8
     * @return mixed
9
     */
10
    function value($value)
11
    {
12
        return $value instanceof Closure ? $value() : $value;
13
    }
14
}
15
16
if (! function_exists('is_serialized')) {
17
    /**
18
     * Проверка переменной на предмет наличия в ней сериализованных данных
19
     *
20
     * @param  mixed $data данные которые необходимо проверить
21
     * @return bool
22
     */
23
    function is_serialized($data)
24
    {
25
        if (! is_string($data)) {
26
            return false;
27
        }
28
        $data = trim($data);
29
        if ('N;' == $data) {
30
            return true;
31
        }
32
        $length = strlen($data);
33
        if ($length < 4) {
34
            return false;
35
        }
36
        if (':' !== $data[1]) {
37
            return false;
38
        }
39
        $lastc = $data[$length - 1];
40
        if (';' !== $lastc && '}' !== $lastc) {
41
            return false;
42
        }
43
        $token = $data[0];
44
        switch ($token) {
45
            case 's':
46
                return ('"' === $data[$length - 2]);
47
            case 'a':
48
            case 'O':
49
                return (bool)preg_match("/^{$token}:[0-9]+:/s", $data);
50
            case 'b':
51
            case 'i':
52
            case 'd':
53
                return (bool)preg_match("/^{$token}:[0-9.E-]+;\$/", $data);
54
        }
55
56
        return false;
57
    }
58
}
59
60
if (! function_exists('var_switch')) {
61
    /**
62
     * Поменять местами значения двух переменных
63
     *
64
     * @param $var1
65
     * @param $var2
66
     * @return void
67
     */
68
    function var_switch(&$var1, &$var2)
69
    {
70
        list($var2, $var1) = [$var1, $var2];
0 ignored issues
show
Comprehensibility Best Practice introduced by
This list assign is not used and could be removed.
Loading history...
71
    }
72
}
73
74
if (! function_exists('arity')) {
75
    /**
76
     * Сколько параметров принимает данное замыкание
77
     *
78
     * @param Closure $callback
79
     * @return int
80
     */
81
    function arity(Closure $callback)
82
    {
83
        $r = new ReflectionObject($callback);
84
        $m = $r->getMethod('__invoke');
85
86
        return $m->getNumberOfParameters();
87
    }
88
}
89