Completed
Push — master ( 49a516...79d061 )
by Marco
04:40
created

Memcached::getInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 3
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 3
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace Comodojo\SimpleCache\Providers;
2
3
use \Comodojo\Cache\Drivers\Memcached as MemcachedDriver;
4
use \Comodojo\Foundation\Validation\DataValidation;
5
use \Comodojo\Foundation\Validation\DataFilter;
6
use \Comodojo\Cache\Components\EnhancedCacheItemPoolStats;
7
use \Psr\Log\LoggerInterface;
8
use \Comodojo\Exception\SimpleCacheException;
9
use \Comodojo\Exception\InvalidSimpleCacheArgumentException;
10
use \Exception;
11
/**
12
 * Memcached provider
13
 *
14
 * @package     Comodojo Spare Parts
15
 * @author      Marco Giovinazzi <[email protected]>
16
 * @license     MIT
17
 *
18
 * LICENSE:
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
 * THE SOFTWARE.
27
 */
28
29 View Code Duplication
class Memcached extends AbstractEnhancedProvider {
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...
30
31 34
    public function __construct(
32
        $server = '127.0.0.1',
33
        $port = 11211,
34
        $weight = 0,
35
        $persistent_id = null,
36
        LoggerInterface $logger = null
37
    ) {
38
39 34
        if ( empty($server) ) {
40
            throw new InvalidSimpleCacheArgumentException("Invalid or unspecified memcached server");
41
        }
42
43 34
        if ( $persistent_id !== null && DataValidation::validateString($persistent_id) === false ) {
44
            throw new InvalidSimpleCacheArgumentException("Invalid persistent id");
45
        }
46
47 34
        $port = DataFilter::filterPort($port, 11211);
48 34
        $weight = DataFilter::filterInteger($weight);
49
50
        try {
51
52 34
            $this->driver = new MemcachedDriver([
53 34
                'persistent_id' => $persistent_id,
54 34
                'server' => $server,
55 34
                'port' => $port,
56 34
                'weight' => $weight
57
            ]);
58
59 34
            parent::__construct($logger);
60
61
        } catch (Exception $e) {
62
63
            throw new SimpleCacheException($e->getMessage());
64
65
        }
66
67 34
    }
68
69
    public function getInstance() {
70
        return $this->driver->getInstance();
0 ignored issues
show
Bug introduced by
The method getInstance does only exist in Comodojo\Cache\Drivers\M...\Cache\Drivers\PhpRedis, but not in Comodojo\Cache\Drivers\A...jo\Cache\Drivers\Vacuum.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
71
    }
72
73 2
    public function getStats() {
74
75 2
        $info = $this->driver->stats();
76
77 2
        $objects = 0;
78
79 2
        foreach ( $info as $key => $value ) {
80
81 2
            $objects = max($objects, $value['curr_items']);
82
83
        }
84
85 2
        return new EnhancedCacheItemPoolStats(
86 2
            $this->getId(),
87 2
            $this->driver->getName(),
88 2
            $this->getState(),
89
            $objects,
90 2
            $info
91
        );
92
93
    }
94
95
}
96