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 |
||
17 | class BlogTagTest extends FunctionalTest |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritDoc} |
||
21 | * @var string |
||
22 | */ |
||
23 | protected static $fixture_file = 'blog.yml'; |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | public function setUp() |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | public function tearDown() |
||
44 | |||
45 | /** |
||
46 | * Tests that any blog posts returned from $tag->BlogPosts() many_many are published, both by |
||
47 | * normal 'save & publish' functionality and by publish date. |
||
48 | */ |
||
49 | View Code Duplication | public function testBlogPosts() |
|
66 | |||
67 | /** |
||
68 | * @see https://github.com/silverstripe/silverstripe-blog/issues/376 |
||
69 | */ |
||
70 | View Code Duplication | public function testAllowMultibyteUrlSegment() |
|
82 | |||
83 | /** |
||
84 | * The first blog can be viewed by anybody. |
||
85 | */ |
||
86 | public function testCanView() |
||
103 | |||
104 | View Code Duplication | public function testCanEdit() |
|
105 | { |
||
106 | $this->useDraftSite(); |
||
107 | |||
108 | $admin = $this->objFromFixture('Member', 'Admin'); |
||
109 | $editor = $this->objFromFixture('Member', 'Editor'); |
||
110 | |||
111 | $tag = $this->objFromFixture('BlogTag', 'FirstTag'); |
||
112 | |||
113 | $this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.'); |
||
114 | $this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.'); |
||
115 | |||
116 | $tag = $this->objFromFixture('BlogTag', 'SecondTag'); |
||
117 | |||
118 | $this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.'); |
||
119 | $this->assertFalse($tag->canEdit($editor), 'Editor should not be able to edit tag.'); |
||
120 | |||
121 | $tag = $this->objFromFixture('BlogTag', 'ThirdTag'); |
||
122 | |||
123 | $this->assertTrue($tag->canEdit($admin), 'Admin should always be able to edit tags.'); |
||
124 | $this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.'); |
||
125 | } |
||
126 | |||
127 | View Code Duplication | public function testCanCreate() |
|
139 | |||
140 | View Code Duplication | public function testCanDelete() |
|
141 | { |
||
142 | $this->useDraftSite(); |
||
143 | |||
144 | $admin = $this->objFromFixture('Member', 'Admin'); |
||
145 | $editor = $this->objFromFixture('Member', 'Editor'); |
||
146 | |||
147 | $tag = $this->objFromFixture('BlogTag', 'FirstTag'); |
||
148 | |||
149 | $this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.'); |
||
150 | $this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.'); |
||
151 | |||
152 | $tag = $this->objFromFixture('BlogTag', 'SecondTag'); |
||
153 | |||
154 | $this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.'); |
||
155 | $this->assertFalse($tag->canDelete($editor), 'Editor should not be able to delete tag.'); |
||
156 | |||
157 | $tag = $this->objFromFixture('BlogTag', 'ThirdTag'); |
||
158 | |||
159 | $this->assertTrue($tag->canDelete($admin), 'Admin should always be able to delete tags.'); |
||
160 | $this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.'); |
||
161 | } |
||
162 | |||
163 | public function testDuplicateTagsForURLSegment() |
||
180 | |||
181 | View Code Duplication | public function testDuplicateTags() |
|
206 | |||
207 | public function testBlogTagUrlSegmentsAreAutomaticallyUpdated() |
||
208 | { |
||
218 | |||
219 | } |
||
220 |
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.