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 |
||
6 | use SilverStripe\Core\Config\Config; |
||
7 | use SilverStripe\Dev\SapphireTest; |
||
8 | use SilverStripe\Forms\FieldList; |
||
9 | use SilverStripe\Forms\Form; |
||
10 | use SilverStripe\Forms\TextField; |
||
11 | use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension; |
||
12 | use SilverStripe\SpamProtection\Tests\FormSpamProtectionExtensionTest\FooProtector; |
||
13 | use SilverStripe\SpamProtection\Tests\FormSpamProtectionExtensionTest\BarProtector; |
||
14 | use SilverStripe\SpamProtection\Tests\FormSpamProtectionExtensionTest\BazProtector; |
||
15 | |||
16 | /** |
||
17 | * @package spamprotection |
||
18 | */ |
||
19 | class FormSpamProtectionExtensionTest extends SapphireTest |
||
20 | { |
||
21 | protected $usesDatabase = false; |
||
22 | |||
23 | /** |
||
24 | * @var Form |
||
25 | */ |
||
26 | protected $form = null; |
||
27 | |||
28 | protected function setUp() |
||
44 | |||
45 | public function testEnableSpamProtection() |
||
57 | |||
58 | public function testEnableSpamProtectionCustomProtector() |
||
66 | |||
67 | View Code Duplication | public function testEnableSpamProtectionCustomTitle() |
|
76 | |||
77 | View Code Duplication | public function testCustomOptions() |
|
87 | |||
88 | public function testConfigurableName() |
||
107 | |||
108 | View Code Duplication | public function testInsertBefore() |
|
109 | { |
||
110 | $form = $this->form->enableSpamProtection(array( |
||
111 | 'protector' => FooProtector::class, |
||
112 | 'insertBefore' => 'URL' |
||
113 | )); |
||
114 | |||
115 | $fields = $form->Fields(); |
||
116 | $this->assertEquals('Title', $fields[0]->Title()); |
||
117 | $this->assertEquals('Comment', $fields[1]->Title()); |
||
137 |
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.