Completed
Push — master ( 94f5e8...a88b9e )
by Lars
01:57 queued 11s
created

SerializerIgbinary   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 88
loc 88
ccs 18
cts 24
cp 0.75
rs 10
c 0
b 0
f 0

5 Methods

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

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 array|null
19
     */
20
    private $unserialize_options;
21
22
    /**
23
     * @var string
24
     */
25
    private $name = '';
26
27
    /**
28
     * SerializerIgbinary constructor.
29
     */
30 69
    public function __construct()
31
    {
32 69
        if (self::$_exists_igbinary === null) {
33
            self::$_exists_igbinary = (
34 1
                \function_exists('igbinary_serialize')
35
                &&
36 1
                \function_exists('igbinary_unserialize')
37
            );
38
        }
39
40 69
        if (self::$_exists_igbinary) {
41
            $this->name = 'igbinary';
42
        } else {
43 69
            $this->name = 'default';
44
        }
45 69
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getName(): string
51
    {
52
        return $this->name;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 33
    public function serialize($value)
59
    {
60 33
        if (self::$_exists_igbinary === true) {
61
            /** @noinspection PhpUndefinedFunctionInspection */
62
            /** @noinspection PhpComposerExtensionStubsInspection */
63
            return \igbinary_serialize($value);
64
        }
65
66
        // fallback
67 33
        return \serialize($value);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 26
    public function unserialize($value)
74
    {
75 26
        if (self::$_exists_igbinary === true) {
76
            /** @noinspection PhpUndefinedFunctionInspection */
77
            /** @noinspection PhpComposerExtensionStubsInspection */
78
            return \igbinary_unserialize($value);
79
        }
80
81
        // fallback
82 26
        if ($this->unserialize_options !== null) {
83 13
            return \unserialize($value, $this->unserialize_options);
84
        }
85
86
        /** @noinspection UnserializeExploitsInspection */
87 13
        return \unserialize($value);
88
    }
89
90
    /**
91
     * @param array $options
92
     */
93 47
    public function setUnserializeOptions(array $options)
94
    {
95 47
        $this->unserialize_options = $options;
96 47
    }
97
}
98