Completed
Push — 2.0 ( 8b0753...eaa66b )
by Marco
04:57
created

PhpRedis::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 16

Duplication

Lines 31
Ratio 100 %

Code Coverage

Tests 13
CRAP Score 3.0593

Importance

Changes 0
Metric Value
dl 31
loc 31
ccs 13
cts 16
cp 0.8125
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 4
nop 2
crap 3.0593
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
 * @package     Comodojo Cache
14
 * @author      Marco Giovinazzi <[email protected]>
15
 * @license     MIT
16
 *
17
 * LICENSE:
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 * THE SOFTWARE.
26
 */
27
28 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...
29
30
    protected $default_properties = [
31
        "server" => '127.0.0.1',
32
        "port" => 6379,
33
        "timeout" => 0,
34
        "password" => null
35
    ];
36
37 28
    public function __construct(array $properties = [], LoggerInterface $logger = null) {
38
39 28
        parent::__construct($properties, $logger);
40
41 28
        $properties = $this->getProperties();
42
43 28
        if ( empty($properties->server) ) {
44
            throw new InvalidCacheArgumentException("Invalid or unspecified memcached server");
45
        }
46
47 28
        $port = DataFilter::filterPort($properties->port, 6379);
48 28
        $timeout = DataFilter::filterInteger($properties->timeout, 0, PHP_INT_MAX, 0);
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
50
        try {
51
52 28
            $this->driver = new PhpRedisDriver([
53 28
                'server' => $properties->server,
54 28
                'port' => $port,
55 28
                'timeout' => $timeout,
56 28
                'password' => $properties->password
57
            ]);
58
59 28
            $this->test();
60
61
        } catch (Exception $e) {
62
63
            throw new CacheException($e->getMessage());
64
65
        }
66
67 28
    }
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
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public function getStats() {
77
78 1
        $info = $this->driver->stats();
79
80 1
        return new EnhancedCacheItemPoolStats(
81 1
            $this->getId(),
82 1
            $this->driver->getName(),
83 1
            $this->getState(),
84 1
            $info['objects'],
85 1
            $info['stats']
86
        );
87
88
    }
89
90
}
91