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 | /** |
||
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.