| 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 |
||
| 39 | public function testCpu() |
||
| 40 | { |
||
| 41 | $decorator = new CacheCpu( |
||
| 42 | $server = $this->createMock(Server::class), |
||
| 43 | $clock = $this->createMock(Clock::class), |
||
| 44 | new ElapsedPeriod(42) |
||
| 45 | ); |
||
| 46 | $server |
||
| 47 | ->expects($this->exactly(2)) |
||
| 48 | ->method('cpu') |
||
| 49 | ->will( |
||
| 50 | $this->onConsecutiveCalls( |
||
| 51 | $cpu1 = new Cpu(new Percentage(33), new Percentage(33), new Percentage(34), new Cores(1)), |
||
| 52 | $cpu2 = new Cpu(new Percentage(33), new Percentage(33), new Percentage(34), new Cores(1)) |
||
| 53 | ) |
||
| 54 | ); |
||
| 55 | $clock |
||
| 56 | ->expects($this->at(0)) |
||
| 57 | ->method('now') |
||
| 58 | ->willReturn( |
||
| 59 | $first = $this->createMock(PointInTime::class) |
||
| 60 | ); |
||
| 61 | $clock |
||
| 62 | ->expects($this->at(1)) |
||
| 63 | ->method('now') |
||
| 64 | ->willReturn( |
||
| 65 | $second = $this->createMock(PointInTime::class) |
||
| 66 | ); |
||
| 67 | $second |
||
| 68 | ->expects($this->once()) |
||
| 69 | ->method('elapsedSince') |
||
| 70 | ->with($first) |
||
| 71 | ->willReturn(new ElapsedPeriod(24)); |
||
| 72 | $clock |
||
| 73 | ->expects($this->at(2)) |
||
| 74 | ->method('now') |
||
| 75 | ->willReturn( |
||
| 76 | $third = $this->createMock(PointInTime::class) |
||
| 77 | ); |
||
| 78 | $third |
||
| 79 | ->expects($this->once()) |
||
| 80 | ->method('elapsedSince') |
||
| 81 | ->with($first) |
||
| 82 | ->willReturn(new ElapsedPeriod(50)); |
||
| 83 | $clock |
||
| 84 | ->expects($this->exactly(3)) |
||
| 85 | ->method('now'); |
||
| 86 | |||
| 87 | $this->assertSame($cpu1, $decorator->cpu()); //put in cache |
||
| 88 | $this->assertSame($cpu1, $decorator->cpu()); //load cache |
||
| 89 | $this->assertSame($cpu2, $decorator->cpu()); //stale |
||
| 90 | } |
||
| 171 |