Completed
Pull Request — master (#317)
by Leny
80:20 queued 63:15
created

ViewReferenceRedisTool::isSerialized()   C

Complexity

Conditions 12
Paths 16

Size

Total Lines 25
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 25
rs 5.1612
c 1
b 0
f 1
cc 12
eloc 22
nc 16
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 Victoire\Bundle\ViewReferenceBundle\Connector\Redis;
4
5
/**
6
 * Class ViewReferenceRedisTool.
7
 */
8
class ViewReferenceRedisTool
9
{
10
    /**
11
     * This method generated a string that can be persisted for redis with data.
12
     *
13
     * @param $data
14
     *
15
     * @return string
16
     */
17
    public function redislize($data)
18
    {
19
        // Only serialize if it's not a string or a integer
20
        if (!is_string($data) && !is_int($data)) {
21
            return urlencode(serialize($data));
22
        }
23
        // Encode string to escape wrong saves
24
        return urlencode($data);
25
    }
26
27
    /**
28
     * This method unredislize a string.
29
     *
30
     * @param $data
31
     *
32
     * @return mixed|string
33
     */
34
    public function unredislize($data)
35
    {
36
        $data = urldecode($data);
37
38
        if (self::isSerialized($data)) {
39
            $unserializedData = @unserialize($data);
40
            if ($unserializedData !== false) {
41
                return $unserializedData;
42
            }
43
        }
44
45
        return $data;
46
    }
47
48
    /**
49
     * Redislize an array (key, valueToRedislize).
50
     *
51
     * @param array $data
52
     *
53
     * @return array
54
     */
55
    public function redislizeArray(array $data)
56
    {
57
        $result = [];
58
        foreach ($data as $key => $value) {
59
            if (!is_array($value)) {
60
                $result[$key] = $this->redislize($value);
61
            }
62
        }
63
64
        return $result;
65
    }
66
67
    /**
68
     * unredislize an array (key, valueToUnredislize).
69
     *
70
     * @param array $data
71
     *
72
     * @return array
73
     */
74
    public function unredislizeArray(array $data)
75
    {
76
        $result = [];
77
        foreach ($data as $key => $value) {
78
            $result[$key] = $this->unredislize($value);
79
        }
80
81
        return $result;
82
    }
83
84
    /**
85
     * This method generate a key hash.
86
     *
87
     * @param $alias
88
     * @param $id
89
     *
90
     * @return string
91
     */
92
    public function generateKey($alias, $id)
93
    {
94
        return $alias.':'.$id;
95
    }
96
97
    public static function isSerialized($data) {
98
        // if it isn't a string, it isn't serialized
99
        if ( !is_string( $data ) )
100
            return false;
101
        $data = trim( $data );
102
        if ( 'N;' == $data )
103
            return true;
104
        if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
105
            return false;
106
        switch ( $badions[1] ) {
107
            case 'a' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
108
            case 'O' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
109
            case 's' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
110
                if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
111
                    return true;
112
                break;
113
            case 'b' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
114
            case 'i' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
115
            case 'd' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
116
                if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
117
                    return true;
118
                break;
119
        }
120
        return false;
121
    }
122
}
123