Completed
Push — master ( b2571a...4cd5f2 )
by James
9s
created

testGetWithNotExistingKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace RoaveTest\DoctrineSimpleCache;
5
6
use Doctrine\Common\Cache\ArrayCache;
7
use Roave\DoctrineSimpleCache\CacheException;
8
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
9
use RoaveTestAsset\DoctrineSimpleCache\FullyImplementedCache;
10
use RoaveTestAsset\DoctrineSimpleCache\NotClearableCache;
11
use RoaveTestAsset\DoctrineSimpleCache\NotMultiGettableCache;
12
use RoaveTestAsset\DoctrineSimpleCache\NotMultiPuttableCache;
13
14
/**
15
 * @covers \Roave\DoctrineSimpleCache\SimpleCacheAdapter
16
 */
17
final class SimpleCacheAdapterTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed()
20
    {
21
        /** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
22
        $doctrineCache = $this->createMock(NotMultiPuttableCache::class);
23
24
        $this->expectException(CacheException::class);
25
        new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...tiPuttableCache::class) on line 22 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
26
    }
27
28
    public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed()
29
    {
30
        /** @var NotClearableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
31
        $doctrineCache = $this->createMock(NotClearableCache::class);
32
33
        $this->expectException(CacheException::class);
34
        new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...tClearableCache::class) on line 31 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
35
    }
36
37
    public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed()
38
    {
39
        /** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
40
        $doctrineCache = $this->createMock(NotMultiGettableCache::class);
41
42
        $this->expectException(CacheException::class);
43
        new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...tiGettableCache::class) on line 40 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
44
    }
45
46
    public function testGetProxiesToDoctrineFetch()
47
    {
48
        $key = uniqid('key', true);
49
        $value = uniqid('value', true);
50
51
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
52
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
53
        $doctrineCache->expects(self::once())->method('fetch')->with($key)->willReturn($value);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

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...
54
55
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 52 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
56
        self::assertSame($value, $psrCache->get($key));
57
    }
58
59
    public function testGetWithNotExistingKey()
60
    {
61
        $key = uniqid('key', true);
62
        $value = uniqid('value', true);
63
64
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
65
        $psrCache->set($key, $value);
66
67
        $default = uniqid('default', true);
68
        self::assertSame($value, $psrCache->get($key, $default));
69
70
        $anotherKey = uniqid('key', true);
71
        self::assertSame($default, $psrCache->get($anotherKey, $default));
72
    }
73
74
    public function testSetProxiesToDoctrineSave()
75
    {
76
        $key = uniqid('key', true);
77
        $value = uniqid('value', true);
78
        $ttl = random_int(1000, 9999);
79
80
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
81
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
82
        $doctrineCache->expects(self::once())->method('save')->with($key, $value, $ttl)->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

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...
83
84
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 81 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
85
        self::assertTrue($psrCache->set($key, $value, $ttl));
86
    }
87
88 View Code Duplication
    public function testDeleteProxiesToDoctrineDelete()
0 ignored issues
show
Duplication introduced by
This method 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...
89
    {
90
        $key = uniqid('key', true);
91
92
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
93
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
94
        $doctrineCache->expects(self::once())->method('delete')->with($key)->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

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...
95
96
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 93 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
97
        self::assertTrue($psrCache->delete($key));
98
    }
99
100
    public function testClearProxiesToDeleteAll()
101
    {
102
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
103
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
104
        $doctrineCache->expects(self::once())->method('deleteAll')->with()->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

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...
105
106
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 103 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107
        self::assertTrue($psrCache->clear());
108
    }
109
110
    public function testGetMultipleProxiesToFetchMultiple()
111
    {
112
        $values = [
113
            uniqid('key1', true) => uniqid('value1', true),
114
            uniqid('key2', true) => uniqid('value2', true),
115
        ];
116
        $keys = array_keys($values);
117
118
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
119
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
120
        $doctrineCache->expects(self::once())->method('fetchMultiple')->with($keys)->willReturn($values);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

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...
121
122
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 119 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
123
        self::assertSame($values, $psrCache->getMultiple($keys));
0 ignored issues
show
Documentation introduced by
$keys is of type array<integer,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
124
    }
125
126
    public function testGetMultipleWithPartialKeys()
127
    {
128
        $values = [
129
            uniqid('key1', true) => uniqid('value1', true),
130
            uniqid('key2', true) => uniqid('value2', true),
131
        ];
132
        $keys = array_keys($values);
133
134
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
135
        $psrCache->setMultiple($values);
0 ignored issues
show
Documentation introduced by
$values is of type array<string,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
136
137
        $default = uniqid('default', true);
138
        $invalid_key = uniqid('key3', true);
139
        $keys[] = $invalid_key;
140
        $values[$invalid_key] = $default;
141
142
        self::assertSame($values, $psrCache->getMultiple($keys, $default));
0 ignored issues
show
Documentation introduced by
$keys is of type array<integer,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
143
    }
144
145
    public function testSetMultipleProxiesToSaveMultiple()
146
    {
147
        $values = [
148
            uniqid('key1', true) => uniqid('value1', true),
149
            uniqid('key2', true) => uniqid('value2', true),
150
        ];
151
152
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
153
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
154
        $doctrineCache->expects(self::once())->method('saveMultiple')->with($values)->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

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...
155
156
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 153 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
157
        self::assertTrue($psrCache->setMultiple($values));
0 ignored issues
show
Documentation introduced by
$values is of type array<string,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
158
    }
159
160 View Code Duplication
    public function testDeleteMultipleReturnsTrueWhenAllDeletesSucceed()
0 ignored issues
show
Duplication introduced by
This method 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...
161
    {
162
        $keys = [
163
            uniqid('key1', true),
164
            uniqid('key2', true),
165
        ];
166
167
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
168
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
169
        $doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

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...
170
        $doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true);
171
172
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 168 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
173
        self::assertTrue($psrCache->deleteMultiple($keys));
0 ignored issues
show
Documentation introduced by
$keys is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
174
    }
175
176 View Code Duplication
    public function testDeleteMultipleReturnsFalseWhenOneDeleteFails()
0 ignored issues
show
Duplication introduced by
This method 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...
177
    {
178
        $keys = [
179
            uniqid('key1', true),
180
            uniqid('key2', true),
181
        ];
182
183
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
184
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
185
        $doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(false);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

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...
186
        $doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true);
187
188
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 184 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
189
        self::assertFalse($psrCache->deleteMultiple($keys));
0 ignored issues
show
Documentation introduced by
$keys is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
190
    }
191
192 View Code Duplication
    public function testHasProxiesToDoctrineContains()
0 ignored issues
show
Duplication introduced by
This method 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...
193
    {
194
        $key = uniqid('key', true);
195
196
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
197
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
198
        $doctrineCache->expects(self::once())->method('contains')->with($key)->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

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...
199
200
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 197 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
201
        self::assertTrue($psrCache->has($key));
202
    }
203
}
204