| Conditions | 1 |
| Paths | 1 |
| Total Lines | 51 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 57 | public function testMemory() |
||
| 58 | { |
||
| 59 | $decorator = new CacheMemory( |
||
| 60 | $server = $this->createMock(Server::class), |
||
| 61 | $clock = $this->createMock(Clock::class), |
||
| 62 | new ElapsedPeriod(42) |
||
| 63 | ); |
||
| 64 | $server |
||
| 65 | ->expects($this->exactly(2)) |
||
| 66 | ->method('memory') |
||
| 67 | ->will( |
||
| 68 | $this->onConsecutiveCalls( |
||
| 69 | $memory1 = new Memory(new Bytes(42), new Bytes(42), new Bytes(42), new Bytes(42), new Bytes(42), new Bytes(42)), |
||
| 70 | $memory2 = new Memory(new Bytes(42), new Bytes(42), new Bytes(42), new Bytes(42), new Bytes(42), new Bytes(42)) |
||
| 71 | ) |
||
| 72 | ); |
||
| 73 | $clock |
||
| 74 | ->expects($this->at(0)) |
||
| 75 | ->method('now') |
||
| 76 | ->willReturn( |
||
| 77 | $first = $this->createMock(PointInTime::class) |
||
| 78 | ); |
||
| 79 | $clock |
||
| 80 | ->expects($this->at(1)) |
||
| 81 | ->method('now') |
||
| 82 | ->willReturn( |
||
| 83 | $second = $this->createMock(PointInTime::class) |
||
| 84 | ); |
||
| 85 | $second |
||
| 86 | ->expects($this->once()) |
||
| 87 | ->method('elapsedSince') |
||
| 88 | ->with($first) |
||
| 89 | ->willReturn(new ElapsedPeriod(24)); |
||
| 90 | $clock |
||
| 91 | ->expects($this->at(2)) |
||
| 92 | ->method('now') |
||
| 93 | ->willReturn( |
||
| 94 | $third = $this->createMock(PointInTime::class) |
||
| 95 | ); |
||
| 96 | $third |
||
| 97 | ->expects($this->once()) |
||
| 98 | ->method('elapsedSince') |
||
| 99 | ->with($first) |
||
| 100 | ->willReturn(new ElapsedPeriod(50)); |
||
| 101 | $clock |
||
| 102 | ->expects($this->exactly(3)) |
||
| 103 | ->method('now'); |
||
| 104 | |||
| 105 | $this->assertSame($memory1, $decorator->memory()); //put in cache |
||
| 106 | $this->assertSame($memory1, $decorator->memory()); //load cache |
||
| 107 | $this->assertSame($memory2, $decorator->memory()); //stale |
||
| 108 | } |
||
| 164 |