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

Memcached   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 75.86%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 67
loc 67
ccs 22
cts 29
cp 0.7586
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 3 3 1
B __construct() 37 37 5
A getStats() 21 21 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Comodojo\Cache\Providers;
2
3
use \Comodojo\Cache\Drivers\Memcached as MemcachedDriver;
4
use \Comodojo\Cache\Item;
5
use \Comodojo\Cache\Components\EnhancedCacheItemPoolStats;
6
use \Comodojo\Foundation\Validation\DataValidation;
7
use \Comodojo\Foundation\Validation\DataFilter;
8
use \Psr\Log\LoggerInterface;
9
use \Comodojo\Exception\CacheException;
10
use \Exception;
11
12
/**
13
 * Memcached provider
14
 *
15
 * @package     Comodojo Spare Parts
16
 * @author      Marco Giovinazzi <[email protected]>
17
 * @license     MIT
18
 *
19
 * LICENSE:
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 */
29
30 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...
31
32 52
    public function __construct(
33
        $server = '127.0.0.1',
34
        $port = 11211,
35
        $weight = 0,
36
        $persistent_id = null,
37
        LoggerInterface $logger = null
38
    ) {
39
40 52
        if ( empty($server) ) {
41
            throw new InvalidCacheArgumentException("Invalid or unspecified memcached server");
42
        }
43
44 52
        if ( $persistent_id !== null && DataValidation::validateString($persistent_id) === false ) {
45
            throw new InvalidCacheArgumentException("Invalid persistent id");
46
        }
47
48 52
        $port = DataFilter::filterPort($port, 11211);
49 52
        $weight = DataFilter::filterInteger($weight);
50
51
        try {
52
53 52
            $this->driver = new MemcachedDriver([
54 52
                'persistent_id' => $persistent_id,
55 52
                'server' => $server,
56 52
                'port' => $port,
57 52
                'weight' => $weight
58
            ]);
59
60 52
            parent::__construct($logger);
61
62
        } catch (Exception $e) {
63
64
            throw new CacheException($e->getMessage());
65
66
        }
67
68 52
    }
69
70
    public function getInstance() {
71
        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...
72
    }
73
74 2
    public function getStats() {
75
76 2
        $info = $this->driver->stats();
77
78 2
        $objects = 0;
79
80 2
        foreach ( $info as $key => $value ) {
81
82 2
            $objects = max($objects, $value['curr_items']);
83
84
        }
85
86 2
        return new EnhancedCacheItemPoolStats(
87 2
            $this->getId(),
88 2
            $this->driver->getName(),
89 2
            $this->getState(),
90
            $objects,
91 2
            $info
92
        );
93
94
    }
95
96
}
97