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() { |
||
| 46 | |||
| 47 | function testCustomRules() { |
||
| 48 | $this->logInWithPermission('ADMIN'); |
||
| 49 | $form = $this->objFromFixture('UserDefinedForm', 'custom-rules-form'); |
||
| 50 | |||
| 51 | $checkbox = $form->Fields()->find('ClassName', 'EditableCheckbox'); |
||
| 52 | $field = $form->Fields()->find('ClassName', 'EditableTextField'); |
||
| 53 | |||
| 54 | $rules = $checkbox->DisplayRules(); |
||
| 55 | |||
| 56 | // form has 2 fields - a checkbox and a text field |
||
| 57 | // it has 1 rule - when ticked the checkbox hides the text field |
||
| 58 | $this->assertEquals(1, $rules->Count()); |
||
| 59 | $this->assertEquals($rules, $checkbox->EffectiveDisplayRules()); |
||
| 60 | |||
| 61 | $checkboxRule = $rules->First(); |
||
| 62 | $checkboxRule->ConditionFieldID = $field->ID; |
||
| 63 | |||
| 64 | $this->assertEquals($checkboxRule->Display, 'Hide'); |
||
| 65 | $this->assertEquals($checkboxRule->ConditionOption, 'HasValue'); |
||
| 66 | $this->assertEquals($checkboxRule->FieldValue, '6'); |
||
| 67 | |||
| 68 | // If field is required then all custom rules are disabled |
||
| 69 | $checkbox->Required = true; |
||
| 70 | $this->assertEquals(0, $checkbox->EffectiveDisplayRules()->count()); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @covers EditableOption::getValue |
||
| 75 | */ |
||
| 76 | public function testEditableOptionEmptyValue() { |
||
| 92 | |||
| 93 | View Code Duplication | function testEditableDropdownField() { |
|
| 104 | |||
| 105 | View Code Duplication | function testEditableRadioField() { |
|
| 115 | |||
| 116 | function testMultipleOptionDuplication() { |
||
| 129 | |||
| 130 | public function testFileField() { |
||
| 131 | $fileField = $this->objFromFixture('EditableFileField', 'file-field'); |
||
| 132 | $formField = $fileField->getFormField(); |
||
| 133 | |||
| 134 | $this->assertContains('jpg', $formField->getValidator()->getAllowedExtensions()); |
||
| 135 | $this->assertNotContains('notallowedextension', $formField->getValidator()->getAllowedExtensions()); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function testFileFieldAllowedExtensionsBlacklist() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Verify that unique names are automatically generated for each formfield |
||
| 148 | */ |
||
| 149 | public function testUniqueName() { |
||
| 166 | |||
| 167 | } |
||
| 168 |
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.