Completed
Push — master ( 5621e4...94f5e8 )
by Lars
16:34
created

SerializerIgbinary   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 69
loc 69
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 11 11 2
A unserialize() 16 16 3
A setUnserializeOptions() 4 4 1
A __construct() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * SerializerIgbinary: serialize / unserialize
9
 */
10 View Code Duplication
class SerializerIgbinary implements iSerializer
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
{
12
    /**
13
     * @var bool
14
     */
15
    public static $_exists_igbinary;
16
17
    /**
18
     * @var null|array
19
     */
20
    private $unserialize_options;
21
22
    /**
23
     * SerializerIgbinary constructor.
24
     */
25 69
    public function __construct()
26
    {
27 69
        if (self::$_exists_igbinary === null) {
28 1
            self::$_exists_igbinary = (
29 1
                \function_exists('igbinary_serialize')
30
                &&
31 1
                \function_exists('igbinary_unserialize')
32
            );
33
        }
34 69
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 33
    public function serialize($value)
40
    {
41 33
        if (self::$_exists_igbinary === true) {
42
            /** @noinspection PhpUndefinedFunctionInspection */
43
            /** @noinspection PhpComposerExtensionStubsInspection */
44
            return \igbinary_serialize($value);
45
        }
46
47
        // fallback
48 33
        return \serialize($value);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 26
    public function unserialize($value)
55
    {
56 26
        if (self::$_exists_igbinary === true) {
57
            /** @noinspection PhpUndefinedFunctionInspection */
58
            /** @noinspection PhpComposerExtensionStubsInspection */
59
            return \igbinary_unserialize($value);
60
        }
61
62
        // fallback
63 26
        if ($this->unserialize_options !== null) {
64 13
            return \unserialize($value, $this->unserialize_options);
65
        }
66
67
        /** @noinspection UnserializeExploitsInspection */
68 13
        return \unserialize($value);
69
    }
70
71
    /**
72
     * @param array $options
73
     */
74 47
    public function setUnserializeOptions(array $options)
75
    {
76 47
        $this->unserialize_options = $options;
77 47
    }
78
}
79