Conditions | 1 |
Paths | 1 |
Total Lines | 120 |
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 |
||
151 | public function testRoles() |
||
152 | { |
||
153 | /** |
||
154 | * @var Blog $firstBlog |
||
155 | */ |
||
156 | $firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
||
157 | |||
158 | /** |
||
159 | * @var Blog $fourthBlog |
||
160 | */ |
||
161 | $fourthBlog = $this->objFromFixture(Blog::class, 'FourthBlog'); |
||
162 | |||
163 | /** |
||
164 | * @var BlogPost $postA |
||
165 | */ |
||
166 | $postA = $this->objFromFixture(BlogPost::class, 'PostA'); |
||
167 | |||
168 | /** |
||
169 | * @var BlogPost $postB |
||
170 | */ |
||
171 | $postB = $this->objFromFixture(BlogPost::class, 'PostB'); |
||
172 | |||
173 | /** |
||
174 | * @var BlogPost $postC |
||
175 | */ |
||
176 | $postC = $this->objFromFixture(BlogPost::class, 'PostC'); |
||
177 | |||
178 | /** |
||
179 | * @var Member $editor |
||
180 | */ |
||
181 | $editor = $this->objFromFixture(Member::class, 'BlogEditor'); |
||
182 | |||
183 | /** |
||
184 | * @var Member $writer |
||
185 | */ |
||
186 | $writer = $this->objFromFixture(Member::class, 'Writer'); |
||
187 | |||
188 | /** |
||
189 | * @var Member $contributor |
||
190 | */ |
||
191 | $contributor = $this->objFromFixture(Member::class, 'Contributor'); |
||
192 | |||
193 | /** |
||
194 | * @var Member $visitor |
||
195 | */ |
||
196 | $visitor = $this->objFromFixture(Member::class, 'Visitor'); |
||
197 | |||
198 | $this->assertEquals('Editor', $fourthBlog->RoleOf($editor)); |
||
199 | $this->assertEquals('Contributor', $fourthBlog->RoleOf($contributor)); |
||
200 | $this->assertEquals('Writer', $fourthBlog->RoleOf($writer)); |
||
201 | $this->assertEmpty($fourthBlog->RoleOf($visitor)); |
||
202 | $this->assertEquals('Author', $postA->RoleOf($writer)); |
||
203 | $this->assertEquals('Author', $postA->RoleOf($contributor)); |
||
204 | $this->assertEquals('Editor', $postA->RoleOf($editor)); |
||
205 | $this->assertEmpty($postA->RoleOf($visitor)); |
||
206 | |||
207 | // Test RoleOf with string values given |
||
208 | $this->assertEquals('Editor', $fourthBlog->RoleOf((string)(int)$editor->ID)); |
||
209 | $this->assertEquals('Contributor', $fourthBlog->RoleOf((string)(int)$contributor->ID)); |
||
210 | $this->assertEquals('Writer', $fourthBlog->RoleOf((string)(int)$writer->ID)); |
||
211 | $this->assertEmpty($fourthBlog->RoleOf((string)(int)$visitor->ID)); |
||
212 | $this->assertEquals('Author', $postA->RoleOf((string)(int)$writer->ID)); |
||
213 | $this->assertEquals('Author', $postA->RoleOf((string)(int)$contributor->ID)); |
||
214 | $this->assertEquals('Editor', $postA->RoleOf((string)(int)$editor->ID)); |
||
215 | $this->assertEmpty($postA->RoleOf((string)(int)$visitor->ID)); |
||
216 | |||
217 | // Test RoleOf with int values given |
||
218 | $this->assertEquals('Editor', $fourthBlog->RoleOf((int)$editor->ID)); |
||
219 | $this->assertEquals('Contributor', $fourthBlog->RoleOf((int)$contributor->ID)); |
||
220 | $this->assertEquals('Writer', $fourthBlog->RoleOf((int)$writer->ID)); |
||
221 | $this->assertEmpty($fourthBlog->RoleOf((int)$visitor->ID)); |
||
222 | $this->assertEquals('Author', $postA->RoleOf((int)$writer->ID)); |
||
223 | $this->assertEquals('Author', $postA->RoleOf((int)$contributor->ID)); |
||
224 | $this->assertEquals('Editor', $postA->RoleOf((int)$editor->ID)); |
||
225 | $this->assertEmpty($postA->RoleOf((int)$visitor->ID)); |
||
226 | |||
227 | $this->assertTrue($fourthBlog->canEdit($editor)); |
||
228 | $this->assertFalse($firstBlog->canEdit($editor)); |
||
229 | $this->assertTrue($fourthBlog->canAddChildren($editor)); |
||
230 | $this->assertFalse($firstBlog->canAddChildren($editor)); |
||
231 | $this->assertTrue($postA->canEdit($editor)); |
||
232 | $this->assertTrue($postB->canEdit($editor)); |
||
233 | $this->assertTrue($postC->canEdit($editor)); |
||
234 | $this->assertTrue($postA->canPublish($editor)); |
||
235 | $this->assertTrue($postB->canPublish($editor)); |
||
236 | $this->assertTrue($postC->canPublish($editor)); |
||
237 | |||
238 | $this->assertFalse($fourthBlog->canEdit($writer)); |
||
239 | $this->assertFalse($firstBlog->canEdit($writer)); |
||
240 | $this->assertTrue($fourthBlog->canAddChildren($writer)); |
||
241 | $this->assertFalse($firstBlog->canAddChildren($writer)); |
||
242 | $this->assertTrue($postA->canEdit($writer)); |
||
243 | $this->assertFalse($postB->canEdit($writer)); |
||
244 | $this->assertTrue($postC->canEdit($writer)); |
||
245 | $this->assertTrue($postA->canPublish($writer)); |
||
246 | $this->assertFalse($postB->canPublish($writer)); |
||
247 | $this->assertTrue($postC->canPublish($writer)); |
||
248 | |||
249 | $this->assertFalse($fourthBlog->canEdit($contributor)); |
||
250 | $this->assertFalse($firstBlog->canEdit($contributor)); |
||
251 | $this->assertTrue($fourthBlog->canAddChildren($contributor)); |
||
252 | $this->assertFalse($firstBlog->canAddChildren($contributor)); |
||
253 | $this->assertTrue($postA->canEdit($contributor)); |
||
254 | $this->assertFalse($postB->canEdit($contributor)); |
||
255 | $this->assertTrue($postC->canEdit($contributor)); |
||
256 | $this->assertFalse($postA->canPublish($contributor)); |
||
257 | $this->assertFalse($postB->canPublish($contributor)); |
||
258 | $this->assertFalse($postC->canPublish($contributor)); |
||
259 | |||
260 | $this->assertFalse($fourthBlog->canEdit($visitor)); |
||
261 | $this->assertFalse($firstBlog->canEdit($visitor)); |
||
262 | $this->assertFalse($fourthBlog->canAddChildren($visitor)); |
||
263 | $this->assertFalse($firstBlog->canAddChildren($visitor)); |
||
264 | $this->assertFalse($postA->canEdit($visitor)); |
||
265 | $this->assertFalse($postB->canEdit($visitor)); |
||
266 | $this->assertFalse($postC->canEdit($visitor)); |
||
267 | $this->assertFalse($postA->canPublish($visitor)); |
||
268 | $this->assertFalse($postB->canPublish($visitor)); |
||
269 | $this->assertFalse($postC->canPublish($visitor)); |
||
270 | } |
||
271 | |||
382 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: