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 |
||
100 | public function testLoadAverage() |
||
101 | { |
||
102 | $decorator = new CacheLoadAverage( |
||
103 | $server = $this->createMock(Server::class), |
||
104 | $clock = $this->createMock(Clock::class), |
||
105 | new ElapsedPeriod(42) |
||
106 | ); |
||
107 | $server |
||
108 | ->expects($this->exactly(2)) |
||
109 | ->method('loadAverage') |
||
110 | ->will( |
||
111 | $this->onConsecutiveCalls( |
||
112 | $load1 = new LoadAverage(1, 5, 15), |
||
113 | $load2 = new LoadAverage(1, 5, 15) |
||
114 | ) |
||
115 | ); |
||
116 | $clock |
||
117 | ->expects($this->at(0)) |
||
118 | ->method('now') |
||
119 | ->willReturn( |
||
120 | $first = $this->createMock(PointInTime::class) |
||
121 | ); |
||
122 | $clock |
||
123 | ->expects($this->at(1)) |
||
124 | ->method('now') |
||
125 | ->willReturn( |
||
126 | $second = $this->createMock(PointInTime::class) |
||
127 | ); |
||
128 | $second |
||
129 | ->expects($this->once()) |
||
130 | ->method('elapsedSince') |
||
131 | ->with($first) |
||
132 | ->willReturn(new ElapsedPeriod(24)); |
||
133 | $clock |
||
134 | ->expects($this->at(2)) |
||
135 | ->method('now') |
||
136 | ->willReturn( |
||
137 | $third = $this->createMock(PointInTime::class) |
||
138 | ); |
||
139 | $third |
||
140 | ->expects($this->once()) |
||
141 | ->method('elapsedSince') |
||
142 | ->with($first) |
||
143 | ->willReturn(new ElapsedPeriod(50)); |
||
144 | $clock |
||
145 | ->expects($this->exactly(3)) |
||
146 | ->method('now'); |
||
147 | |||
148 | $this->assertSame($load1, $decorator->loadAverage()); //put in cache |
||
149 | $this->assertSame($load1, $decorator->loadAverage()); //load cache |
||
150 | $this->assertSame($load2, $decorator->loadAverage()); //stale |
||
151 | } |
||
171 |