Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 3 | class DMSDocumentCartCheckoutPageTest extends FunctionalTest |
||
|
|
|||
| 4 | { |
||
| 5 | protected static $fixture_file = 'DMSDocumentCartTest.yml'; |
||
| 6 | /** |
||
| 7 | * @var DMSDocumentCartCheckoutPage_Controller |
||
| 8 | */ |
||
| 9 | protected $controller; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @var DMSDocumentCart |
||
| 13 | */ |
||
| 14 | protected $cart; |
||
| 15 | |||
| 16 | public function setUp() |
||
| 17 | { |
||
| 18 | parent::setUp(); |
||
| 19 | $this->controller = DMSDocumentCartCheckoutPage_Controller::create(); |
||
| 20 | $this->cart = $this->controller->getCart(); |
||
| 21 | DMSDocumentCartCheckoutPage_Controller::add_extension('StubDMSDocumentCheckoutPageExtension'); |
||
| 22 | Injector::inst()->registerService(new StubEmail(), 'Email'); |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Tests DMSDocumentRequest form has a FieldList |
||
| 27 | */ |
||
| 28 | public function testDMSDocumentRequestForm() |
||
| 29 | { |
||
| 30 | $form = $this->controller->DMSDocumentRequestForm(); |
||
| 31 | $this->assertInstanceOf('FieldList', $form->Fields()); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Tests if the DMSDocumentRequestForm is extensible |
||
| 36 | */ |
||
| 37 | public function testDMSDocumentRequestFormIsExtensible() |
||
| 38 | { |
||
| 39 | $controller = DMSDocumentCartCheckoutPage_Controller::create(); |
||
| 40 | $form = $controller->DMSDocumentRequestForm(); |
||
| 41 | $this->assertNotNull( |
||
| 42 | $form->Fields()->fieldByName('NewTextField'), |
||
| 43 | 'DMSDocumentRequestForm() is extensible as it included the field from the extension' |
||
| 44 | ); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Tests if print requests are incremented |
||
| 49 | */ |
||
| 50 | View Code Duplication | public function testTrackTimestampedPrintRequest() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Tests if a Cart is received |
||
| 63 | */ |
||
| 64 | public function testGetCart() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Tests whether the cart items are updated from the controller |
||
| 71 | */ |
||
| 72 | public function testUpdateCartItems() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Tests whether the recipient details are updated from the controller |
||
| 87 | */ |
||
| 88 | public function testUpdateCartReceiverInfo() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Tests whether emails are sent. Emails are mocked so not actually sent. |
||
| 105 | */ |
||
| 106 | public function testSend() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Tests whether email sending is extensible. |
||
| 132 | */ |
||
| 133 | public function testSendIsExtensible() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Test to see whether the cart is empty after a request is sent. |
||
| 144 | */ |
||
| 145 | public function testDoRequestSend() |
||
| 173 | } |
||
| 174 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.