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

SerializerIgbinary::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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