Passed
Push — master ( 848a13...c93e13 )
by Mauro
02:28
created

SerializeChecker::isSerialized()   C

Complexity

Conditions 12
Paths 16

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 21
nc 16
nop 1
dl 0
loc 33
rs 5.1612
c 0
b 0
f 0

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
 * This file is part of the InMemoryList package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 */
10
11
namespace InMemoryList\Domain\Helper;
12
13
class SerializeChecker
14
{
15
    /**
16
     * @param $data
17
     *
18
     * @return bool
19
     */
20
    public static function isSerialized($data)
21
    {
22
        if (!is_string($data)) {
23
            return false;
24
        }
25
26
        $data = trim($data);
27
        if ('N;' === $data) {
28
            return true;
29
        }
30
31
        if (!preg_match('/^([adObis]):/', $data, $badions)) {
32
            return false;
33
        }
34
35
        switch ($badions[1]) {
36
            case 'a':
37
            case 'O':
38
            case 's':
39
                if (preg_match("/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data)) {
40
                    return true;
41
                }
42
                break;
43
            case 'b':
44
            case 'i':
45
            case 'd':
46
                if (preg_match("/^{$badions[1]}:[0-9.E-]+;\$/", $data)) {
47
                    return true;
48
                }
49
                break;
50
        }
51
52
        return false;
53
    }
54
}