GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 3.0 ( de1771...791612 )
by Vermeulen
04:28
created

MemcacheTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 17
c 3
b 0
f 0
lcom 0
cbo 2
dl 0
loc 149
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B completeServerInfos() 0 23 4
B testConnect() 0 27 5
A ifExists() 0 23 3
B updateExpire() 0 31 5
1
<?php
2
3
namespace BFW\Memcache;
4
5
use \Exception;
6
7
/**
8
 * Trait to regroup memcache(d) methods
9
 */
10
trait MemcacheTrait
11
{
12
    /**
13
     * Read the server information and add not existing keys
14
     * 
15
     * @param array &$infos Server informations
16
     * 
17
     * @return void
18
     * 
19
     * @throw \Exception If informations datas is not an array
20
     */
21
    protected function completeServerInfos(&$infos)
22
    {
23
        if (!is_array($infos)) {
24
            throw new Exception(
25
                'Memcache(d) server information is not an array.',
26
                $this::ERR_SERVER_INFOS_FORMAT
27
            );
28
        }
29
        
30
        $infosKeyDefaultValues = [
31
            'host'       => null,
32
            'port'       => null,
33
            'weight'     => 0,
34
            'timeout'    => null,
35
            'persistent' => false
36
        ];
37
        
38
        foreach ($infosKeyDefaultValues as $infosKey => $defaultValue) {
39
            if (!isset($infos[$infosKey])) {
40
                $infos[$infosKey] = $defaultValue;
41
            }
42
        }
43
    }
44
    
45
    /**
46
     * addServer not created the connection. It's created at the first call
47
     * to the memcached servers.
48
     * 
49
     * So, we run the connect to all server declared
50
     * 
51
     * @throws \Exception If a server is not connected
52
     * 
53
     * @return boolean
54
     */
55
    protected function testConnect()
56
    {
57
        if ($this instanceof \BFW\Memcache\Memcache) {
58
            //With Memcache getStats not return stats for all connected server.
59
            $stats = $this->getExtendedStats();
60
        } else {
61
            $stats = $this->getStats();
0 ignored issues
show
Bug introduced by
It seems like getStats() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
        }
63
        
64
        if (!is_array($stats)) {
65
            throw new Exception(
66
                'No memcached server connected.',
67
                $this::ERR_NO_SERVER_CONNECTED
68
            );
69
        }
70
        
71
        foreach ($stats as $serverName => $serverStat) {
72
            if ($serverStat['uptime'] < 1) {
73
                throw new Exception(
74
                    'Memcached server '.$serverName.' not connected',
75
                    $this::ERR_A_SERVER_IS_NOT_CONNECTED
76
                );
77
            }
78
        }
79
        
80
        return true;
81
    }
82
    
83
    /**
84
     * Check if a key exists into memcache(d)
85
     * /!\ Not work if the correct value is the boolean false /!\
86
     * 
87
     * @param string $key The memcache(d) key to check
88
     * 
89
     * @return boolean
90
     * 
91
     * @throws Exception If the key is not a string
92
     */
93
    public function ifExists($key)
94
    {
95
        $verifParams = \BFW\Helpers\Datas::checkType([
96
            [
97
                'type' => 'string',
98
                'data' => $key
99
            ]
100
        ]);
101
        
102
        if (!$verifParams) {
103
            throw new Exception(
104
                'The $key parameters must be a string.'
105
                .' Currently the value is a/an '.gettype($key),
106
                $this::ERR_IFEXISTS_PARAM_TYPE
107
            );
108
        }
109
110
        if ($this->get($key) === false) {
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
111
            return false;
112
        }
113
114
        return true;
115
    }
116
117
    /**
118
     * Update the expire time for a memcache(d) key.
119
     * 
120
     * @param string $key The memcache(d) key to update
121
     * @param int $expire The new expire time
122
     * 
123
     * @return boolean
124
     * 
125
     * @throws Exception
126
     */
127
    public function updateExpire($key, $expire)
128
    {
129
        $verifParams = \BFW\Helpers\Datas::checkType([
130
            ['type' => 'string', 'data' => $key],
131
            ['type' => 'int', 'data' => $expire]
132
        ]);
133
134
        if (!$verifParams) {
135
            throw new Exception(
136
                'Once of parameters $key or $expire not have a correct type.',
137
                $this::ERR_UPDATEEXPIRE_PARAM_TYPE
138
            );
139
        }
140
        
141
        if (!$this->ifExists($key)) {
142
            throw new Exception(
143
                'The key '.$key.' not exist on memcache(d) server',
144
                $this::ERR_KEY_NOT_EXIST
145
            );
146
        }
147
148
        //To change expire time, we need to re-set the value.
149
        $value = $this->get($key); //Get the value
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
150
        
151
        //Re-set the value with new expire time.
152
        if (is_subclass_of($this, '\Memcache')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
153
            return $this->replace($key, $value, 0, $expire);
0 ignored issues
show
Bug introduced by
It seems like replace() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
154
        } elseif (is_subclass_of($this, '\Memcached')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
155
            return $this->replace($key, $value, $expire); //We can use touch()
0 ignored issues
show
Bug introduced by
It seems like replace() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
156
        }
157
    }
158
}
159