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

PhpRedis::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 17

Duplication

Lines 31
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 3.1105

Importance

Changes 0
Metric Value
dl 31
loc 31
ccs 10
cts 13
cp 0.7692
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 4
nop 4
crap 3.1105
1
<?php namespace Comodojo\Cache\Providers;
2
3
use \Comodojo\Cache\Drivers\PhpRedis as PhpRedisDriver;
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 PhpRedis 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 28
    public function __construct(
33
        $server = '127.0.0.1',
34
        $port = 6379,
35
        $timeout = 0,
36
        LoggerInterface $logger = null
37
    ) {
38
39 28
        if ( empty($server) ) {
40
            throw new InvalidCacheArgumentException("Invalid or unspecified memcached server");
41
        }
42
43 28
        $port = DataFilter::filterPort($port, 6379);
44 28
        $timeout = DataFilter::filterInteger($timeout, 0, PHP_INT_MAX, 0);
45
46
        try {
47
48 28
            $this->driver = new PhpRedisDriver([
49 28
                'server' => $server,
50 28
                'port' => $port,
51 28
                'timeout' => $timeout
52
            ]);
53
54 28
            parent::__construct($logger);
55
56
        } catch (Exception $e) {
57
58
            throw new CacheException($e->getMessage());
59
60
        }
61
62 28
    }
63
64
    public function getInstance() {
65
        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...
66
    }
67
68 1
    public function getStats() {
69
70 1
        $info = $this->driver->stats();
71
72 1
        return new EnhancedCacheItemPoolStats(
73 1
            $this->getId(),
74 1
            $this->driver->getName(),
75 1
            $this->getState(),
76 1
            $info['objects'],
77 1
            $info['stats']
78
        );
79
80
    }
81
82
}
83