MemcacheProfilerStorage::setValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sitetheory\Bundle\ProfilerStorageBundle\Profiler;
13
14
/**
15
 * Memcache Profiler Storage.
16
 *
17
 * Class MemcacheProfilerStorage
18
 *
19
 * @author Andrej Hudec <[email protected]>
20
 */
21
class MemcacheProfilerStorage extends BaseMemcacheProfilerStorage
22
{
23
    /**
24
     * @var \Memcache
25
     */
26
    private $memcache;
27
28
    /**
29
     * Internal convenience method that returns the instance of the Memcache.
30
     *
31
     * @throws \RuntimeException
32
     *
33
     * @return \Memcache
34
     */
35
    protected function getMemcache()
36
    {
37
        if (null === $this->memcache) {
38
            if (!preg_match('#^memcache://(?(?=\[.*\])\[(.*)\]|(.*)):(.*)$#', $this->dsn, $matches)) {
39
                throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Memcache with an invalid dsn "%s". The expected format is "memcache://[host]:port".', $this->dsn));
40
            }
41
42
            $host = $matches[1] ?: $matches[2];
43
            $port = $matches[3];
44
45
            $memcache = new \Memcache();
46
            $memcache->addServer($host, $port);
47
48
            $this->memcache = $memcache;
49
        }
50
51
        return $this->memcache;
52
    }
53
54
    /**
55
     * Set instance of the Memcache.
56
     *
57
     * @param \Memcache $memcache
58
     */
59
    public function setMemcache($memcache)
60
    {
61
        $this->memcache = $memcache;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function getValue($key)
68
    {
69
        return $this->getMemcache()->get($key);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function setValue($key, $value, $expiration = 0)
76
    {
77
        return $this->getMemcache()->set($key, $value, false, time() + $expiration);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $flag of MemcachePool::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        return $this->getMemcache()->set($key, $value, /** @scrutinizer ignore-type */ false, time() + $expiration);
Loading history...
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    protected function delete($key)
84
    {
85
        return $this->getMemcache()->delete($key);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    protected function appendValue($key, $value, $expiration = 0)
92
    {
93
        $memcache = $this->getMemcache();
94
95
        if (method_exists($memcache, 'append')) {
96
            // Memcache v3.0
97
            if (!$result = $memcache->append($key, $value, false, $expiration)) {
0 ignored issues
show
Unused Code introduced by
The call to MemcachePool::append() has too many arguments starting with $key. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
            if (!$result = $memcache->/** @scrutinizer ignore-call */ append($key, $value, false, $expiration)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
98
                return $memcache->set($key, $value, false, $expiration);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $flag of MemcachePool::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
                return $memcache->set($key, $value, /** @scrutinizer ignore-type */ false, $expiration);
Loading history...
99
            }
100
101
            return $result;
102
        }
103
104
        // simulate append in Memcache <3.0
105
        $content = $memcache->get($key);
106
107
        return $memcache->set($key, $content.$value, false, $expiration);
0 ignored issues
show
Bug introduced by
Are you sure $content of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        return $memcache->set($key, /** @scrutinizer ignore-type */ $content.$value, false, $expiration);
Loading history...
108
    }
109
}
110