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 |
||
| 7 | class EditableFormFieldTest extends FunctionalTest { |
||
|
|
|||
| 8 | |||
| 9 | static $fixture_file = 'userforms/tests/EditableFormFieldTest.yml'; |
||
| 10 | |||
| 11 | function testFormFieldPermissions() { |
||
| 12 | $text = $this->objFromFixture('EditableTextField', 'basic-text'); |
||
| 13 | |||
| 14 | $this->logInWithPermission('ADMIN'); |
||
| 15 | $this->assertTrue($text->canCreate()); |
||
| 16 | $this->assertTrue($text->canView()); |
||
| 17 | $this->assertTrue($text->canEdit()); |
||
| 18 | $this->assertTrue($text->canDelete()); |
||
| 19 | |||
| 20 | $text->setReadonly(true); |
||
| 21 | $this->assertTrue($text->canView()); |
||
| 22 | $this->assertFalse($text->canEdit()); |
||
| 23 | $this->assertFalse($text->canDelete()); |
||
| 24 | |||
| 25 | $text->setReadonly(false); |
||
| 26 | $this->assertTrue($text->canView()); |
||
| 27 | $this->assertTrue($text->canEdit()); |
||
| 28 | $this->assertTrue($text->canDelete()); |
||
| 29 | |||
| 30 | $member = Member::currentUser(); |
||
| 31 | $member->logout(); |
||
| 32 | |||
| 33 | $this->logInWithPermission('SITETREE_VIEW_ALL'); |
||
| 34 | $this->assertFalse($text->canCreate()); |
||
| 35 | |||
| 36 | $text->setReadonly(false); |
||
| 37 | $this->assertTrue($text->canView()); |
||
| 38 | $this->assertFalse($text->canEdit()); |
||
| 39 | $this->assertFalse($text->canDelete()); |
||
| 40 | |||
| 41 | $text->setReadonly(true); |
||
| 42 | $this->assertTrue($text->canView()); |
||
| 43 | $this->assertFalse($text->canEdit()); |
||
| 44 | $this->assertFalse($text->canDelete()); |
||
| 45 | } |
||
| 46 | |||
| 47 | function testCustomRules() { |
||
| 72 | |||
| 73 | View Code Duplication | function testEditableDropdownField() { |
|
| 74 | $dropdown = $this->objFromFixture('EditableDropdown', 'basic-dropdown'); |
||
| 75 | |||
| 76 | $field = $dropdown->getFormField(); |
||
| 77 | |||
| 78 | |||
| 79 | $this->assertThat($field, $this->isInstanceOf('DropdownField')); |
||
| 80 | $values = $field->getSource(); |
||
| 81 | |||
| 82 | $this->assertEquals(array('Option 1' => 'Option 1', 'Option 2' => 'Option 2'), $values); |
||
| 83 | } |
||
| 84 | |||
| 85 | View Code Duplication | function testEditableRadioField() { |
|
| 86 | $radio = $this->objFromFixture('EditableRadioField', 'radio-field'); |
||
| 87 | |||
| 88 | $field = $radio->getFormField(); |
||
| 89 | |||
| 90 | $this->assertThat($field, $this->isInstanceOf('OptionsetField')); |
||
| 91 | $values = $field->getSource(); |
||
| 92 | |||
| 93 | $this->assertEquals(array('Option 5' => 'Option 5', 'Option 6' => 'Option 6'), $values); |
||
| 94 | } |
||
| 95 | |||
| 96 | function testMultipleOptionDuplication() { |
||
| 97 | $dropdown = $this->objFromFixture('EditableDropdown','basic-dropdown'); |
||
| 98 | |||
| 99 | $clone = $dropdown->duplicate(); |
||
| 100 | |||
| 101 | $this->assertEquals($clone->Options()->Count(), $dropdown->Options()->Count()); |
||
| 102 | |||
| 103 | foreach($clone->Options() as $option) { |
||
| 104 | $orginal = $dropdown->Options()->find('Title', $option->Title); |
||
| 105 | |||
| 106 | $this->assertEquals($orginal->Sort, $option->Sort); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | public function testFileField() { |
||
| 117 | |||
| 118 | public function testFileFieldAllowedExtensionsBlacklist() { |
||
| 119 | Config::inst()->update('EditableFileField', 'allowed_extensions_blacklist', array('jpg')); |
||
| 120 | $fileField = $this->objFromFixture('EditableFileField', 'file-field'); |
||
| 121 | $formField = $fileField->getFormField(); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Verify that unique names are automatically generated for each formfield |
||
| 128 | */ |
||
| 129 | public function testUniqueName() { |
||
| 146 | |||
| 147 | public function testLengthRange() { |
||
| 176 | |||
| 177 | |||
| 178 | } |
||
| 179 |
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.