Completed
Push — master ( e45ace...c787d2 )
by Marco
02:32
created

SimpleCacheAdapterTest   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 416
Duplicated Lines 25.96 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 31
c 6
b 0
f 1
lcom 1
cbo 3
dl 108
loc 416
rs 9.8

29 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetProxiesToDoctrineSave() 0 13 1
A testSetWithDateIntervalTTL() 0 12 1
A testSetWithNonPositiveTTL() 0 14 1
A testDeleteProxiesToDoctrineDelete() 11 11 1
A testClearProxiesToDeleteAll() 0 9 1
A testGetMultipleProxiesToFetchMultiple() 0 15 1
A testGetMultipleWithPartialKeys() 0 18 1
A testGetMultipleThrowsExceptionWithInvalidKeys() 0 7 1
A testGetMultipleAcceptsTraversable() 13 13 1
A invalidTTLs() 0 15 1
A validKeys() 0 7 1
A invalidKeys() 0 23 1
A testConstructorThrowsExceptionWhenNotMultiOperationCacheIsUsed() 0 8 1
A testConstructorThrowsExceptionWhenNotClearableCacheIsUsed() 0 8 1
A testGetProxiesToDoctrineFetch() 12 12 1
A testGetWithNotExistingKey() 0 14 1
A testGetWithFalseValueStoredInCache() 9 9 1
A testSetWithInvalidTTL() 10 10 1
A testGetMultipleAcceptsGenerator() 0 19 2
A testGetMultipleThrowsExceptionWhenNotArrayOrTraversable() 7 7 1
A testSetMultipleProxiesToSaveMultiple() 0 14 1
A testSetMultipleWithDateIntervalTTL() 0 15 1
A testSetMultipleWithNonPositiveTTL() 0 17 1
A testSetMultipleWithInvalidTTL() 0 12 1
A testSetMultipleThrowsExceptionWhenNotArrayOrTraversable() 7 7 1
A testSetMultipleAcceptsGenerator() 0 21 2
A testDeleteMultipleReturnsTrueWhenAllDeletesSucceed() 14 14 1
A testDeleteMultipleReturnsFalseWhenOneDeleteFails() 14 14 1
A testHasProxiesToDoctrineContains() 11 11 1

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
2
declare(strict_types = 1);
3
4
namespace RoaveTest\DoctrineSimpleCache;
5
6
use Doctrine\Common\Cache\ArrayCache;
7
use Roave\DoctrineSimpleCache\Exception\CacheException;
8
use Roave\DoctrineSimpleCache\Exception\InvalidArgumentException;
9
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
10
use RoaveTestAsset\DoctrineSimpleCache\FullyImplementedCache;
11
use RoaveTestAsset\DoctrineSimpleCache\NotClearableCache;
12
use RoaveTestAsset\DoctrineSimpleCache\NotMultiOperationCache;
13
14
/**
15
 * @covers \Roave\DoctrineSimpleCache\SimpleCacheAdapter
16
 */
17
final class SimpleCacheAdapterTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function invalidTTLs() : array
20
    {
21
        return [
22
            [''],
23
            [true],
24
            [false],
25
            ['abc'],
26
            [2.5],
27
            [' 1'], // can be casted to a int
28
            ['12foo'], // can be casted to a int
29
            ['025'], // can be interpreted as hex
30
            [new \stdClass()],
31
            [['array']],
32
        ];
33
    }
34
35
    public function validKeys()
36
    {
37
        return [
38
            ['AbC19_.'],
39
            ['1234567890123456789012345678901234567890123456789012345678901234'],
40
        ];
41
    }
42
43
    public function invalidKeys()
44
    {
45
        return [
46
            [''],
47
            [true],
48
            [false],
49
            [null],
50
            [2],
51
            [2.5],
52
            ['{str'],
53
            ['rand{'],
54
            ['rand{str'],
55
            ['rand}str'],
56
            ['rand(str'],
57
            ['rand)str'],
58
            ['rand/str'],
59
            ['rand\\str'],
60
            ['rand@str'],
61
            ['rand:str'],
62
            [new \stdClass()],
63
            [['array']],
64
        ];
65
    }
66
67
    public function testConstructorThrowsExceptionWhenNotMultiOperationCacheIsUsed()
68
    {
69
        /** @var NotMultiOperationCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
70
        $doctrineCache = $this->createMock(NotMultiOperationCache::class);
71
72
        $this->expectException(CacheException::class);
73
        new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...iOperationCache::class) on line 70 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...
74
    }
75
76
    public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed()
77
    {
78
        /** @var NotClearableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
79
        $doctrineCache = $this->createMock(NotClearableCache::class);
80
81
        $this->expectException(CacheException::class);
82
        new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...tClearableCache::class) on line 79 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...
83
    }
84
85 View Code Duplication
    public function testGetProxiesToDoctrineFetch()
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...
86
    {
87
        $key = uniqid('key', true);
88
        $value = uniqid('value', true);
89
90
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
91
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
92
        $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...
93
94
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 91 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...
95
        self::assertSame($value, $psrCache->get($key));
96
    }
97
98
    public function testGetWithNotExistingKey()
99
    {
100
        $key = uniqid('key', true);
101
        $value = uniqid('value', true);
102
103
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
104
        $psrCache->set($key, $value);
105
106
        $default = uniqid('default', true);
107
        self::assertSame($value, $psrCache->get($key, $default));
108
109
        $anotherKey = uniqid('key', true);
110
        self::assertSame($default, $psrCache->get($anotherKey, $default));
111
    }
112
113 View Code Duplication
    public function testGetWithFalseValueStoredInCache()
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...
114
    {
115
        $key = uniqid('key', true);
116
117
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
118
        $psrCache->set($key, false);
119
120
        self::assertFalse($psrCache->get($key, uniqid('default', true)));
121
    }
122
123
    public function testSetProxiesToDoctrineSave()
124
    {
125
        $key = uniqid('key', true);
126
        $value = uniqid('value', true);
127
        $ttl = random_int(1000, 9999);
128
129
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
130
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
131
        $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...
132
133
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 130 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...
134
        self::assertTrue($psrCache->set($key, $value, $ttl));
135
    }
136
137
    public function testSetWithDateIntervalTTL()
138
    {
139
        $key = uniqid('key', true);
140
        $value = uniqid('value', true);
141
        $ttl_date = \DateInterval::createFromDateString('1 day');
142
143
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
144
145
        // This does not test if ttl is correctly set to 86400 sec.
146
        self::assertTrue($psrCache->set($key, $value, $ttl_date));
147
        self::assertSame($psrCache->get($key), $value);
148
    }
149
150
    public function testSetWithNonPositiveTTL()
151
    {
152
        $key = uniqid('key', true);
153
        $value = uniqid('value', true);
154
        $ttl = random_int(1000, 9999);
155
156
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
157
158
        $psrCache->set($key, $value, $ttl);
159
        self::assertSame($psrCache->get($key), $value);
160
161
        $psrCache->set($key, $value, -1);
162
        self::assertNull($psrCache->get($key), null);
163
    }
164
165
    /**
166
     * @param mixed $ttl
167
     * @dataProvider invalidTTLs
168
     */
169 View Code Duplication
    public function testSetWithInvalidTTL($ttl)
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...
170
    {
171
        $key = uniqid('key', true);
172
        $value = uniqid('value', true);
173
174
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
175
176
        $this->expectException(InvalidArgumentException::class);
177
        $psrCache->set($key, $value, $ttl);
178
    }
179
180 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...
181
    {
182
        $key = uniqid('key', true);
183
184
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
185
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
186
        $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...
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 185 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::assertTrue($psrCache->delete($key));
190
    }
191
192
    public function testClearProxiesToDeleteAll()
193
    {
194
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
195
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
196
        $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...
197
198
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 195 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...
199
        self::assertTrue($psrCache->clear());
200
    }
201
202
    public function testGetMultipleProxiesToFetchMultiple()
203
    {
204
        $values = [
205
            uniqid('key1', true) => uniqid('value1', true),
206
            uniqid('key2', true) => uniqid('value2', true),
207
        ];
208
        $keys = array_keys($values);
209
210
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
211
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
212
        $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...
213
214
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 211 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...
215
        self::assertSame($values, $psrCache->getMultiple($keys));
216
    }
217
218
    public function testGetMultipleWithPartialKeys()
219
    {
220
        $values = [
221
            uniqid('key1', true) => uniqid('value1', true),
222
            uniqid('key2', true) => uniqid('value2', true),
223
        ];
224
        $keys = array_keys($values);
225
226
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
227
        $psrCache->setMultiple($values);
228
229
        $default = uniqid('default', true);
230
        $invalid_key = uniqid('key3', true);
231
        $keys[] = $invalid_key;
232
        $values[$invalid_key] = $default;
233
234
        self::assertSame($values, $psrCache->getMultiple($keys, $default));
235
    }
236
237
    /**
238
     * @param mixed $key
239
     * @dataProvider invalidKeys
240
     */
241
    public function testGetMultipleThrowsExceptionWithInvalidKeys($key)
242
    {
243
        $this->expectException(InvalidArgumentException::class);
244
245
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
246
        $psrCache->getMultiple([$key]);
247
    }
248
249
    /**
250
     * @param mixed $key
251
     * @dataProvider validKeys
252
     */
253 View Code Duplication
    public function testGetMultipleAcceptsTraversable($key)
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...
254
    {
255
        $values = [
256
            $key => uniqid('value', true),
257
        ];
258
259
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
260
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
261
        $doctrineCache->expects(self::once())->method('fetchMultiple')->with(array_keys($values))->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...
262
263
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 260 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...
264
        $psrCache->getMultiple(new \ArrayObject(array_keys($values)));
265
    }
266
267
    public function testGetMultipleAcceptsGenerator()
268
    {
269
        $values = [
270
            uniqid('key0', true) => uniqid('value0', true),
271
            uniqid('key1', true) => uniqid('value1', true),
272
        ];
273
274
        $generator = function () use ($values) {
275
            /** @noinspection ForeachOnArrayComponentsInspection */
276
            foreach (array_keys($values) as $k) {
277
                yield $k;
278
            }
279
        };
280
281
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
282
        $psrCache->setMultiple($values);
283
284
        self::assertSame($values, $psrCache->getMultiple($generator()));
285
    }
286
287 View Code Duplication
    public function testGetMultipleThrowsExceptionWhenNotArrayOrTraversable()
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...
288
    {
289
        $this->expectException(InvalidArgumentException::class);
290
291
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
292
        $psrCache->getMultiple(uniqid('string', true));
0 ignored issues
show
Documentation introduced by
uniqid('string', true) is of type string, but the function expects a array|object<Traversable>.

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...
293
    }
294
295
    public function testSetMultipleProxiesToSaveMultiple()
296
    {
297
        $values = [
298
            uniqid('key1', true) => uniqid('value1', true),
299
            uniqid('key2', true) => uniqid('value2', true),
300
        ];
301
302
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
303
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
304
        $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...
305
306
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 303 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...
307
        self::assertTrue($psrCache->setMultiple($values));
308
    }
309
310
    public function testSetMultipleWithDateIntervalTTL()
311
    {
312
        $values = [
313
            uniqid('key1', true) => uniqid('value1', true),
314
            uniqid('key2', true) => uniqid('value2', true),
315
        ];
316
        $keys = array_keys($values);
317
        $ttl_date = \DateInterval::createFromDateString('1 day');
318
319
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
320
321
        // This does not test if ttl is correctly set to 86400 sec.
322
        self::assertTrue($psrCache->setMultiple($values, $ttl_date));
323
        self::assertSame($values, $psrCache->getMultiple($keys));
324
    }
325
326
    public function testSetMultipleWithNonPositiveTTL()
327
    {
328
        $values = [
329
            uniqid('key1', true) => uniqid('value1', true),
330
            uniqid('key2', true) => uniqid('value2', true),
331
        ];
332
        $keys = array_keys($values);
333
334
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
335
        $psrCache->setMultiple($values);
336
337
        $volatile = [$keys[0] => uniqid('value3', true)];
338
        $psrCache->setMultiple($volatile, -1);
339
340
        self::assertNull($psrCache->get($keys[0]));
341
        self::assertNotNull($psrCache->get($keys[1]));
342
    }
343
344
    /**
345
     * @param mixed $ttl
346
     * @dataProvider invalidTTLs
347
     */
348
    public function testSetMultipleWithInvalidTTL($ttl)
349
    {
350
        $values = [
351
            uniqid('key1', true) => uniqid('value1', true),
352
            uniqid('key2', true) => uniqid('value2', true),
353
        ];
354
355
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
356
357
        $this->expectException(InvalidArgumentException::class);
358
        $psrCache->setMultiple($values, $ttl);
359
    }
360
361 View Code Duplication
    public function testSetMultipleThrowsExceptionWhenNotArrayOrTraversable()
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...
362
    {
363
        $this->expectException(InvalidArgumentException::class);
364
365
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
366
        $psrCache->setMultiple(uniqid('string', true));
0 ignored issues
show
Documentation introduced by
uniqid('string', true) is of type string, but the function expects a array|object<Traversable>.

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...
367
    }
368
369
    public function testSetMultipleAcceptsGenerator()
370
    {
371
        $key0 = uniqid('key0', true);
372
        $key1 = uniqid('key1', true);
373
        $values = [
374
            $key0 => uniqid('value0', true),
375
            $key1 => uniqid('value1', true),
376
        ];
377
378
        $generator = function () use ($values) {
379
            foreach ($values as $k => $v) {
380
                yield $k => $v;
381
            }
382
        };
383
384
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
385
        $psrCache->setMultiple($generator());
386
387
        self::assertSame($values[$key0], $psrCache->get($key0));
388
        self::assertSame($values[$key1], $psrCache->get($key1));
389
    }
390
391 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...
392
    {
393
        $keys = [
394
            uniqid('key1', true),
395
            uniqid('key2', true),
396
        ];
397
398
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
399
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
400
        $doctrineCache->expects(self::once())->method('deleteMultiple')->with($keys)->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...
401
402
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 399 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...
403
        self::assertTrue($psrCache->deleteMultiple($keys));
404
    }
405
406 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...
407
    {
408
        $keys = [
409
            uniqid('key1', true),
410
            uniqid('key2', true),
411
        ];
412
413
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
414
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
415
        $doctrineCache->expects(self::once())->method('deleteMultiple')->with($keys)->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...
416
417
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 414 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...
418
        self::assertFalse($psrCache->deleteMultiple($keys));
419
    }
420
421 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...
422
    {
423
        $key = uniqid('key', true);
424
425
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
426
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
427
        $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...
428
429
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 426 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...
430
        self::assertTrue($psrCache->has($key));
431
    }
432
}
433