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 ( 9b5baa...d405c3 )
by Vermeulen
02:54
created

Memcache::getServerInfos()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 13
nc 4
nop 1
1
<?php
2
3
namespace BFW\Traits;
4
5
use \Exception;
6
7
/**
8
 * Trait to regroup memcache(d) methods
9
 */
10
trait Memcache
11
{
12
    /**
13
     * Define default value for server informations
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 getServerInfos(&$infos)
22
    {
23
        if (!is_array($infos)) {
24
            throw new Exception(
25
                'Memcache(d) server information is not an array.'
26
            );
27
        }
28
        
29
        $infosKeyDefaultValues = [
30
            'host'       => null,
31
            'port'       => null,
32
            'weight'     => 0,
33
            'timeout'    => null,
34
            'persistent' => false
35
        ];
36
        
37
        foreach ($infosKeyDefaultValues as $infosKey => $defaultValue) {
38
            if (!isset($infos[$infosKey])) {
39
                $infos[$infosKey] = $defaultValue;
40
            }
41
        }
42
    }
43
    
44
    /**
45
     * Check if a key exists
46
     * 
47
     * @param string $key The memcache(d) key to check
48
     * 
49
     * @return boolean
50
     * 
51
     * @throws Exception If the key is not a string
52
     */
53
    public function ifExists($key)
54
    {
55
        $verifParams = \BFW\Helpers\Datas::checkType(
56
            [
57
                [
58
                    'type' => 'string',
59
                    'data' => $key
60
                ]
61
            ]
62
        );
63
        
64
        if (!$verifParams) {
65
            throw new Exception('The $key parameters must be a string');
66
        }
67
68
        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...
69
            return false;
70
        }
71
72
        return true;
73
    }
74
75
    /**
76
     * Update the expire time for a memcache(d) key.
77
     * 
78
     * @param string $key The memcache(d) key to update
79
     * @param int $expire The new expire time
80
     * 
81
     * @return boolean
82
     * 
83
     * @throws Exception
84
     */
85
    public function majExpire($key, $expire)
86
    {
87
        $verifParams = \BFW\Helpers\Datas::checkType(
88
            [
89
                ['type' => 'string', 'data' => $key],
90
                ['type' => 'int', 'data' => $expire]
91
            ]
92
        );
93
94
        if (!$verifParams) {
95
            throw new Exception(
96
                'Once of parameters $key or $expire not have a correct type.'
97
            );
98
        }
99
        
100
        if (!$this->ifExists($key)) {
101
            throw new Exception(
102
                'The key '.$key.' not exist on memcache(d) server'
103
            );
104
        }
105
106
        $value = $this->get($key); //Récupère la valeur
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...
107
        
108
        //On la "modifie" en remettant la même valeur mais en changeant
109
        //le temps avant expiration
110
        
111
        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...
112
            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...
113
        } 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...
114
            return $this->replace($key, $value, $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...
115
        }
116
    }
117
}
118