| Conditions | 2 |
| Paths | 2 |
| Total Lines | 53 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 72 | public function testLeastRecentlyUsedShiftForLimitedCacheSize() { |
||
| 73 | |||
| 74 | $instance = new FixedInMemoryLruCache( 5 ); |
||
| 75 | $instance->save( 'abcde', array( 'abcde' ) ); |
||
| 76 | |||
| 77 | $this->assertEquals( |
||
| 78 | array( 'abcde' ), |
||
| 79 | $instance->fetch( 'abcde' ) |
||
| 80 | ); |
||
| 81 | |||
| 82 | foreach ( array( 'éèêë', 'アイウエオ', 'АБВГД', 'αβγδε', '12345' ) as $alphabet ) { |
||
| 83 | $instance->save( $alphabet, array( $alphabet ) ); |
||
| 84 | } |
||
| 85 | |||
| 86 | // 'éèêë' was added and removes 'abcde' from the cache |
||
| 87 | $this->assertFalse( $instance->fetch( 'abcde' ) ); |
||
| 88 | |||
| 89 | $stats = $instance->getStats(); |
||
| 90 | |||
| 91 | $this->assertEquals( |
||
| 92 | 5, |
||
| 93 | $stats['count'] |
||
| 94 | ); |
||
| 95 | |||
| 96 | $this->assertEquals( |
||
| 97 | 6, |
||
| 98 | $stats['inserts'] |
||
| 99 | ); |
||
| 100 | |||
| 101 | // 'éèêë' moves to the top (last postion as most recently used) and |
||
| 102 | // 'アイウエオ' becomes the next LRU candidate |
||
| 103 | $this->assertEquals( |
||
| 104 | array( 'éèêë' ), |
||
| 105 | $instance->fetch( 'éèêë' ) |
||
| 106 | ); |
||
| 107 | |||
| 108 | $instance->save( '@#$%&', '@#$%&' ); |
||
| 109 | $this->assertFalse( $instance->fetch( 'アイウエオ' ) ); |
||
| 110 | |||
| 111 | // АБВГД would be the next LRU slot but setting it again will move it to MRU |
||
| 112 | // and push αβγδε into the next LRU position |
||
| 113 | $instance->save( 'АБВГД', 'АБВГД' ); |
||
| 114 | |||
| 115 | $instance->save( '-+=<>', '-+=<>' ); |
||
| 116 | $this->assertFalse( $instance->fetch( 'αβγδε' ) ); |
||
| 117 | |||
| 118 | $stats = $instance->getStats(); |
||
| 119 | |||
| 120 | $this->assertEquals( |
||
| 121 | 5, |
||
| 122 | $stats['count'] |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 196 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.