| Conditions | 1 |
| Paths | 1 |
| Total Lines | 120 |
| Code Lines | 74 |
| 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 |
||
| 166 | public function testRoles() |
||
| 167 | { |
||
| 168 | /** |
||
| 169 | * @var Blog $firstBlog |
||
| 170 | */ |
||
| 171 | $firstBlog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog'); |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @var Blog $fourthBlog |
||
| 175 | */ |
||
| 176 | $fourthBlog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FourthBlog'); |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var BlogPost $postA |
||
| 180 | */ |
||
| 181 | $postA = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'PostA'); |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @var BlogPost $postB |
||
| 185 | */ |
||
| 186 | $postB = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'PostB'); |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @var BlogPost $postC |
||
| 190 | */ |
||
| 191 | $postC = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'PostC'); |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var Member $editor |
||
| 195 | */ |
||
| 196 | $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'BlogEditor'); |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @var Member $writer |
||
| 200 | */ |
||
| 201 | $writer = $this->objFromFixture('SilverStripe\\Security\\Member', 'Writer'); |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @var Member $contributor |
||
| 205 | */ |
||
| 206 | $contributor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Contributor'); |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @var Member $visitor |
||
| 210 | */ |
||
| 211 | $visitor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Visitor'); |
||
| 212 | |||
| 213 | $this->assertEquals('Editor', $fourthBlog->RoleOf($editor)); |
||
| 214 | $this->assertEquals('Contributor', $fourthBlog->RoleOf($contributor)); |
||
| 215 | $this->assertEquals('Writer', $fourthBlog->RoleOf($writer)); |
||
| 216 | $this->assertEmpty($fourthBlog->RoleOf($visitor)); |
||
| 217 | $this->assertEquals('Author', $postA->RoleOf($writer)); |
||
| 218 | $this->assertEquals('Author', $postA->RoleOf($contributor)); |
||
| 219 | $this->assertEquals('Editor', $postA->RoleOf($editor)); |
||
| 220 | $this->assertEmpty($postA->RoleOf($visitor)); |
||
| 221 | |||
| 222 | // Test RoleOf with string values given |
||
| 223 | $this->assertEquals('Editor', $fourthBlog->RoleOf((string)(int)$editor->ID)); |
||
| 224 | $this->assertEquals('Contributor', $fourthBlog->RoleOf((string)(int)$contributor->ID)); |
||
| 225 | $this->assertEquals('Writer', $fourthBlog->RoleOf((string)(int)$writer->ID)); |
||
| 226 | $this->assertEmpty($fourthBlog->RoleOf((string)(int)$visitor->ID)); |
||
| 227 | $this->assertEquals('Author', $postA->RoleOf((string)(int)$writer->ID)); |
||
| 228 | $this->assertEquals('Author', $postA->RoleOf((string)(int)$contributor->ID)); |
||
| 229 | $this->assertEquals('Editor', $postA->RoleOf((string)(int)$editor->ID)); |
||
| 230 | $this->assertEmpty($postA->RoleOf((string)(int)$visitor->ID)); |
||
| 231 | |||
| 232 | // Test RoleOf with int values given |
||
| 233 | $this->assertEquals('Editor', $fourthBlog->RoleOf((int)$editor->ID)); |
||
| 234 | $this->assertEquals('Contributor', $fourthBlog->RoleOf((int)$contributor->ID)); |
||
| 235 | $this->assertEquals('Writer', $fourthBlog->RoleOf((int)$writer->ID)); |
||
| 236 | $this->assertEmpty($fourthBlog->RoleOf((int)$visitor->ID)); |
||
| 237 | $this->assertEquals('Author', $postA->RoleOf((int)$writer->ID)); |
||
| 238 | $this->assertEquals('Author', $postA->RoleOf((int)$contributor->ID)); |
||
| 239 | $this->assertEquals('Editor', $postA->RoleOf((int)$editor->ID)); |
||
| 240 | $this->assertEmpty($postA->RoleOf((int)$visitor->ID)); |
||
| 241 | |||
| 242 | $this->assertTrue($fourthBlog->canEdit($editor)); |
||
| 243 | $this->assertFalse($firstBlog->canEdit($editor)); |
||
| 244 | $this->assertTrue($fourthBlog->canAddChildren($editor)); |
||
| 245 | $this->assertFalse($firstBlog->canAddChildren($editor)); |
||
| 246 | $this->assertTrue($postA->canEdit($editor)); |
||
| 247 | $this->assertTrue($postB->canEdit($editor)); |
||
| 248 | $this->assertTrue($postC->canEdit($editor)); |
||
| 249 | $this->assertTrue($postA->canPublish($editor)); |
||
| 250 | $this->assertTrue($postB->canPublish($editor)); |
||
| 251 | $this->assertTrue($postC->canPublish($editor)); |
||
| 252 | |||
| 253 | $this->assertFalse($fourthBlog->canEdit($writer)); |
||
| 254 | $this->assertFalse($firstBlog->canEdit($writer)); |
||
| 255 | $this->assertTrue($fourthBlog->canAddChildren($writer)); |
||
| 256 | $this->assertFalse($firstBlog->canAddChildren($writer)); |
||
| 257 | $this->assertTrue($postA->canEdit($writer)); |
||
| 258 | $this->assertFalse($postB->canEdit($writer)); |
||
| 259 | $this->assertTrue($postC->canEdit($writer)); |
||
| 260 | $this->assertTrue($postA->canPublish($writer)); |
||
| 261 | $this->assertFalse($postB->canPublish($writer)); |
||
| 262 | $this->assertTrue($postC->canPublish($writer)); |
||
| 263 | |||
| 264 | $this->assertFalse($fourthBlog->canEdit($contributor)); |
||
| 265 | $this->assertFalse($firstBlog->canEdit($contributor)); |
||
| 266 | $this->assertTrue($fourthBlog->canAddChildren($contributor)); |
||
| 267 | $this->assertFalse($firstBlog->canAddChildren($contributor)); |
||
| 268 | $this->assertTrue($postA->canEdit($contributor)); |
||
| 269 | $this->assertFalse($postB->canEdit($contributor)); |
||
| 270 | $this->assertTrue($postC->canEdit($contributor)); |
||
| 271 | $this->assertFalse($postA->canPublish($contributor)); |
||
| 272 | $this->assertFalse($postB->canPublish($contributor)); |
||
| 273 | $this->assertFalse($postC->canPublish($contributor)); |
||
| 274 | |||
| 275 | $this->assertFalse($fourthBlog->canEdit($visitor)); |
||
| 276 | $this->assertFalse($firstBlog->canEdit($visitor)); |
||
| 277 | $this->assertFalse($fourthBlog->canAddChildren($visitor)); |
||
| 278 | $this->assertFalse($firstBlog->canAddChildren($visitor)); |
||
| 279 | $this->assertFalse($postA->canEdit($visitor)); |
||
| 280 | $this->assertFalse($postB->canEdit($visitor)); |
||
| 281 | $this->assertFalse($postC->canEdit($visitor)); |
||
| 282 | $this->assertFalse($postA->canPublish($visitor)); |
||
| 283 | $this->assertFalse($postB->canPublish($visitor)); |
||
| 284 | $this->assertFalse($postC->canPublish($visitor)); |
||
| 285 | } |
||
| 286 | |||
| 362 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: