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\CMS\Controllers\CMSPageEditController; |
||
7 | use SilverStripe\CMS\Model\SiteTree; |
||
8 | use SilverStripe\ContentReview\Extensions\SiteTreeContentReview; |
||
9 | use SilverStripe\ContentReview\Extensions\ContentReviewOwner; |
||
10 | use SilverStripe\ContentReview\Extensions\ContentReviewCMSExtension; |
||
11 | use SilverStripe\ContentReview\Extensions\ContentReviewDefaultSettings; |
||
12 | use SilverStripe\Control\HTTPRequest; |
||
13 | use SilverStripe\Control\HTTPResponse_Exception; |
||
14 | use SilverStripe\Forms\FieldList; |
||
15 | use SilverStripe\Forms\Form; |
||
16 | use SilverStripe\Security\Group; |
||
17 | use SilverStripe\Security\Member; |
||
18 | use SilverStripe\SiteConfig\SiteConfig; |
||
19 | |||
20 | /** |
||
21 | * @mixin PHPUnit_Framework_TestCase |
||
22 | */ |
||
23 | class ContentReviewCMSPageEditControllerTest extends ContentReviewBaseTest |
||
24 | { |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected static $fixture_file = 'ContentReviewTest.yml'; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected static $required_extensions = [ |
||
34 | SiteTree::class => [SiteTreeContentReview::class], |
||
35 | Group::class => [ContentReviewOwner::class], |
||
36 | Member::class => [ContentReviewOwner::class], |
||
37 | CMSPageEditController::class => [ContentReviewCMSExtension::class], |
||
38 | SiteConfig::class => [ContentReviewDefaultSettings::class], |
||
39 | ]; |
||
40 | |||
41 | View Code Duplication | public function testReviewedThrowsExceptionWithNoRecordID() |
|
55 | |||
56 | View Code Duplication | public function testReviewedThrowsExceptionWithWrongRecordID() |
|
70 | |||
71 | public function testReviewedWithAuthor() |
||
92 | |||
93 | public function testSaveReview() |
||
94 | { |
||
95 | /** @var Member $author */ |
||
96 | $author = $this->objFromFixture(Member::class, "author"); |
||
97 | |||
98 | $this->logInAs($author); |
||
99 | |||
100 | /** @var Page|SiteTreeContentReview $page */ |
||
101 | $page = $this->objFromFixture(Page::class, "home"); |
||
102 | |||
103 | $data = array( |
||
104 | "action_savereview" => 1, |
||
136 |
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.