Conditions | 1 |
Paths | 1 |
Total Lines | 51 |
Code Lines | 31 |
Lines | 18 |
Ratio | 35.29 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
21 | public function testObjectToUpdateOnSiteTreeReorganise() |
||
22 | { |
||
23 | // build a mock of the extension overriding flushChanges to prevent writing to the queue |
||
24 | $mockExtension = $this->getMockBuilder(SiteTreePublishingEngine::class) |
||
25 | ->setMethods([ |
||
26 | 'flushChanges', |
||
27 | ]) |
||
28 | ->getMock(); |
||
29 | |||
30 | $getURL = function ($value) { |
||
31 | return $value->URLSegment; |
||
32 | }; |
||
33 | |||
34 | // IF YOU'RE OF A NERVOUS DISPOSITION, LOOK AWAY NOT |
||
35 | // stub the flushChanges method and make sure that each call is able to assert the correct items are in the |
||
36 | $mockExtension->expects($this->exactly(3))->method('flushChanges')->willReturnOnConsecutiveCalls( |
||
37 | View Code Duplication | new \PHPUnit_Framework_MockObject_Stub_ReturnCallback(function () use ($mockExtension, $getURL) { |
|
|
|||
38 | $this->assertEmpty($mockExtension->getToDelete()); |
||
39 | $mockExtension->setToDelete([]); |
||
40 | $this->assertEquals(['stub'], array_map($getURL, $mockExtension->getToUpdate())); |
||
41 | $mockExtension->setToUpdate([]); |
||
42 | }), |
||
43 | View Code Duplication | new \PHPUnit_Framework_MockObject_Stub_ReturnCallback(function () use ($mockExtension, $getURL) { |
|
44 | $this->assertEquals(['stub'], array_map($getURL, $mockExtension->getToDelete())); |
||
45 | $mockExtension->setToDelete([]); |
||
46 | $this->assertEmpty($mockExtension->getToUpdate()); |
||
47 | $mockExtension->setToUpdate([]); |
||
48 | }), |
||
49 | View Code Duplication | new \PHPUnit_Framework_MockObject_Stub_ReturnCallback(function () use ($mockExtension, $getURL) { |
|
50 | $this->assertEmpty($mockExtension->getToDelete()); |
||
51 | $mockExtension->setToDelete([]); |
||
52 | $this->assertEquals(['stub-a-lub-a-dub-dub'], array_map($getURL, $mockExtension->getToUpdate())); |
||
53 | $mockExtension->setToUpdate([]); |
||
54 | }) |
||
55 | ); |
||
56 | |||
57 | // register our extension instance so it's applied to all SiteTree objects |
||
58 | Injector::inst()->registerService($mockExtension, SiteTreePublishingEngine::class); |
||
59 | |||
60 | $page = new PublishablePage; |
||
61 | $page->URLSegment = 'stub'; |
||
62 | |||
63 | // publish the page |
||
64 | $page->write(); |
||
65 | $page->publishRecursive(); |
||
66 | |||
67 | // change the URL and go again |
||
68 | $page->URLSegment = 'stub-a-lub-a-dub-dub'; |
||
69 | $page->write(); |
||
70 | $page->publishRecursive(); |
||
71 | } |
||
72 | |||
191 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.