| Conditions | 1 |
| Paths | 1 |
| Total Lines | 120 |
| Code Lines | 74 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 173 | public function testRoles() |
||
| 174 | { |
||
| 175 | /** |
||
| 176 | * @var Blog $firstBlog |
||
| 177 | */ |
||
| 178 | $firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @var Blog $fourthBlog |
||
| 182 | */ |
||
| 183 | $fourthBlog = $this->objFromFixture(Blog::class, 'FourthBlog'); |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @var BlogPost $postA |
||
| 187 | */ |
||
| 188 | $postA = $this->objFromFixture(BlogPost::class, 'PostA'); |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @var BlogPost $postB |
||
| 192 | */ |
||
| 193 | $postB = $this->objFromFixture(BlogPost::class, 'PostB'); |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @var BlogPost $postC |
||
| 197 | */ |
||
| 198 | $postC = $this->objFromFixture(BlogPost::class, 'PostC'); |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @var Member $editor |
||
| 202 | */ |
||
| 203 | $editor = $this->objFromFixture(Member::class, 'BlogEditor'); |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @var Member $writer |
||
| 207 | */ |
||
| 208 | $writer = $this->objFromFixture(Member::class, 'Writer'); |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @var Member $contributor |
||
| 212 | */ |
||
| 213 | $contributor = $this->objFromFixture(Member::class, 'Contributor'); |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @var Member $visitor |
||
| 217 | */ |
||
| 218 | $visitor = $this->objFromFixture(Member::class, 'Visitor'); |
||
| 219 | |||
| 220 | $this->assertEquals('Editor', $fourthBlog->RoleOf($editor)); |
||
| 221 | $this->assertEquals('Contributor', $fourthBlog->RoleOf($contributor)); |
||
| 222 | $this->assertEquals('Writer', $fourthBlog->RoleOf($writer)); |
||
| 223 | $this->assertEmpty($fourthBlog->RoleOf($visitor)); |
||
| 224 | $this->assertEquals('Author', $postA->RoleOf($writer)); |
||
| 225 | $this->assertEquals('Author', $postA->RoleOf($contributor)); |
||
| 226 | $this->assertEquals('Editor', $postA->RoleOf($editor)); |
||
| 227 | $this->assertEmpty($postA->RoleOf($visitor)); |
||
| 228 | |||
| 229 | // Test RoleOf with string values given |
||
| 230 | $this->assertEquals('Editor', $fourthBlog->RoleOf((string)(int)$editor->ID)); |
||
| 231 | $this->assertEquals('Contributor', $fourthBlog->RoleOf((string)(int)$contributor->ID)); |
||
| 232 | $this->assertEquals('Writer', $fourthBlog->RoleOf((string)(int)$writer->ID)); |
||
| 233 | $this->assertEmpty($fourthBlog->RoleOf((string)(int)$visitor->ID)); |
||
| 234 | $this->assertEquals('Author', $postA->RoleOf((string)(int)$writer->ID)); |
||
| 235 | $this->assertEquals('Author', $postA->RoleOf((string)(int)$contributor->ID)); |
||
| 236 | $this->assertEquals('Editor', $postA->RoleOf((string)(int)$editor->ID)); |
||
| 237 | $this->assertEmpty($postA->RoleOf((string)(int)$visitor->ID)); |
||
| 238 | |||
| 239 | // Test RoleOf with int values given |
||
| 240 | $this->assertEquals('Editor', $fourthBlog->RoleOf((int)$editor->ID)); |
||
| 241 | $this->assertEquals('Contributor', $fourthBlog->RoleOf((int)$contributor->ID)); |
||
| 242 | $this->assertEquals('Writer', $fourthBlog->RoleOf((int)$writer->ID)); |
||
| 243 | $this->assertEmpty($fourthBlog->RoleOf((int)$visitor->ID)); |
||
| 244 | $this->assertEquals('Author', $postA->RoleOf((int)$writer->ID)); |
||
| 245 | $this->assertEquals('Author', $postA->RoleOf((int)$contributor->ID)); |
||
| 246 | $this->assertEquals('Editor', $postA->RoleOf((int)$editor->ID)); |
||
| 247 | $this->assertEmpty($postA->RoleOf((int)$visitor->ID)); |
||
| 248 | |||
| 249 | $this->assertTrue($fourthBlog->canEdit($editor)); |
||
| 250 | $this->assertFalse($firstBlog->canEdit($editor)); |
||
| 251 | $this->assertTrue($fourthBlog->canAddChildren($editor)); |
||
| 252 | $this->assertFalse($firstBlog->canAddChildren($editor)); |
||
| 253 | $this->assertTrue($postA->canEdit($editor)); |
||
| 254 | $this->assertTrue($postB->canEdit($editor)); |
||
| 255 | $this->assertTrue($postC->canEdit($editor)); |
||
| 256 | $this->assertTrue($postA->canPublish($editor)); |
||
| 257 | $this->assertTrue($postB->canPublish($editor)); |
||
| 258 | $this->assertTrue($postC->canPublish($editor)); |
||
| 259 | |||
| 260 | $this->assertFalse($fourthBlog->canEdit($writer)); |
||
| 261 | $this->assertFalse($firstBlog->canEdit($writer)); |
||
| 262 | $this->assertTrue($fourthBlog->canAddChildren($writer)); |
||
| 263 | $this->assertFalse($firstBlog->canAddChildren($writer)); |
||
| 264 | $this->assertTrue($postA->canEdit($writer)); |
||
| 265 | $this->assertFalse($postB->canEdit($writer)); |
||
| 266 | $this->assertTrue($postC->canEdit($writer)); |
||
| 267 | $this->assertTrue($postA->canPublish($writer)); |
||
| 268 | $this->assertFalse($postB->canPublish($writer)); |
||
| 269 | $this->assertTrue($postC->canPublish($writer)); |
||
| 270 | |||
| 271 | $this->assertFalse($fourthBlog->canEdit($contributor)); |
||
| 272 | $this->assertFalse($firstBlog->canEdit($contributor)); |
||
| 273 | $this->assertTrue($fourthBlog->canAddChildren($contributor)); |
||
| 274 | $this->assertFalse($firstBlog->canAddChildren($contributor)); |
||
| 275 | $this->assertTrue($postA->canEdit($contributor)); |
||
| 276 | $this->assertFalse($postB->canEdit($contributor)); |
||
| 277 | $this->assertTrue($postC->canEdit($contributor)); |
||
| 278 | $this->assertFalse($postA->canPublish($contributor)); |
||
| 279 | $this->assertFalse($postB->canPublish($contributor)); |
||
| 280 | $this->assertFalse($postC->canPublish($contributor)); |
||
| 281 | |||
| 282 | $this->assertFalse($fourthBlog->canEdit($visitor)); |
||
| 283 | $this->assertFalse($firstBlog->canEdit($visitor)); |
||
| 284 | $this->assertFalse($fourthBlog->canAddChildren($visitor)); |
||
| 285 | $this->assertFalse($firstBlog->canAddChildren($visitor)); |
||
| 286 | $this->assertFalse($postA->canEdit($visitor)); |
||
| 287 | $this->assertFalse($postB->canEdit($visitor)); |
||
| 288 | $this->assertFalse($postC->canEdit($visitor)); |
||
| 289 | $this->assertFalse($postA->canPublish($visitor)); |
||
| 290 | $this->assertFalse($postB->canPublish($visitor)); |
||
| 291 | $this->assertFalse($postC->canPublish($visitor)); |
||
| 292 | } |
||
| 293 | |||
| 406 |
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: