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 | use SilverStripe\Comments\Model\Comment\SecurityToken as CommentSecurityToken; |
||
8 | use SilverStripe\Comments\Tests\Stubs\CommentableItem; |
||
9 | use SilverStripe\Comments\Tests\CommentTestHelper; |
||
10 | use SilverStripe\Control\Controller; |
||
11 | use SilverStripe\Core\Config\Config; |
||
12 | use SilverStripe\Core\Email\Email; |
||
13 | use SilverStripe\Dev\FunctionalTest; |
||
14 | use SilverStripe\ORM\DataObject; |
||
15 | use SilverStripe\Security\Member; |
||
16 | use SilverStripe\Security\SecurityToken; |
||
17 | |||
18 | /** |
||
19 | * @package comments |
||
20 | * @subpackage tests |
||
21 | */ |
||
22 | class CommentingControllerTest extends FunctionalTest |
||
23 | { |
||
24 | /** |
||
25 | * {@inheritDoc} |
||
26 | */ |
||
27 | public static $fixture_file = 'CommentsTest.yml'; |
||
28 | |||
29 | /** |
||
30 | * {@inheritDoc} |
||
31 | */ |
||
32 | protected $extraDataObjects = array( |
||
33 | CommentableItem::class |
||
34 | ); |
||
35 | |||
36 | protected $securityEnabled; |
||
37 | |||
38 | public function tearDown() |
||
47 | |||
48 | public function setUp() |
||
56 | |||
57 | public function testApproveUnmoderatedComment() |
||
79 | |||
80 | public function testSetGetOwnerController() |
||
88 | |||
89 | View Code Duplication | public function testHam() |
|
112 | |||
113 | View Code Duplication | public function testSpam() |
|
134 | |||
135 | public function testRSS() |
||
167 | |||
168 | // This is returning a 404 which looks logical code wise but also a bit weird. |
||
169 | // Test module on a clean install and check what the actual URL is first |
||
170 | /* public function testReply() { |
||
171 | $this->logInWithPermission('CMS_ACCESS_CommentAdmin'); |
||
172 | $comment = $this->objFromFixture('Comment', 'firstComA'); |
||
173 | $item = $this->objFromFixture('CommentableItem', 'first'); |
||
174 | |||
175 | $st = new CommentSecurityToken($comment); |
||
176 | $url = 'comments/reply/' . $item->ID.'?ParentCommentID=' . $comment->ID; |
||
177 | error_log($url); |
||
178 | $response = $this->get($url); |
||
179 | error_log(print_r($response,1)); |
||
180 | |||
181 | $this->assertEquals(200, $response->getStatusCode()); |
||
182 | |||
183 | } |
||
184 | */ |
||
185 | /* |
||
186 | public function testCommentsFormLoadMemberData() { |
||
187 | Config::inst()->update('CommentableItem', 'comments', array( |
||
188 | 'use_preview' => false |
||
189 | )); |
||
190 | $this->logInAs('visitor'); |
||
191 | SecurityToken::inst()->disable(); |
||
192 | $parent = $this->objFromFixture('CommentableItem', 'first'); |
||
193 | $parent->CommentsRequireLogin = true; |
||
194 | $parent->PostingRequiredPermission = true; |
||
195 | //$parent->write(); |
||
196 | $commController = new CommentingController(); |
||
197 | $commController->setOwnerRecord($parent); |
||
198 | |||
199 | $form = $commController->CommentsForm(); |
||
200 | $commentsFields = $form->Fields()->first()->FieldList(); |
||
201 | $expected = array('Name', 'Email', 'URL', 'Comment', 'PreviewComment'); |
||
202 | CommentTestHelper::assertFieldNames($this, $expected, $commentsFields); |
||
203 | } |
||
204 | */ |
||
205 | |||
206 | public function testCommentsFormUsePreview() |
||
237 | |||
238 | public function testCommentsForm() |
||
239 | { |
||
240 | $this->autoFollowRedirection = true; |
||
241 | |||
242 | // Delete the newly added children of firstComA so as not to change this test |
||
243 | $this->objFromFixture(Comment::class, 'firstComAChild1')->delete(); |
||
244 | $this->objFromFixture(Comment::class, 'firstComAChild2')->delete(); |
||
245 | $this->objFromFixture(Comment::class, 'firstComAChild3')->delete(); |
||
246 | |||
247 | SecurityToken::inst()->disable(); |
||
248 | $this->autoFollowRedirection = false; |
||
249 | $parent = $this->objFromFixture(CommentableItem::class, 'first'); |
||
250 | |||
251 | // Test posting to base comment |
||
252 | $response = $this->post( |
||
253 | 'comments/CommentsForm', |
||
254 | array( |
||
255 | 'Name' => 'Poster', |
||
256 | 'Email' => '[email protected]', |
||
257 | 'Comment' => 'My Comment', |
||
258 | 'ParentID' => $parent->ID, |
||
259 | 'ParentClassName' => CommentableItem::class, |
||
260 | 'action_doPostComment' => 'Post' |
||
261 | ) |
||
262 | ); |
||
263 | $this->assertEquals(302, $response->getStatusCode()); |
||
264 | // $this->assertStringStartsWith('CommentableItemController#comment-', $response->getHeader('Location')); |
||
265 | $this->assertDOSEquals( |
||
266 | array( |
||
267 | array( |
||
268 | 'Name' => 'Poster', |
||
269 | 'Email' => '[email protected]', |
||
270 | 'Comment' => 'My Comment', |
||
271 | 'ParentID' => $parent->ID, |
||
272 | 'ParentClass' => CommentableItem::class, |
||
273 | ) |
||
274 | ), |
||
275 | Comment::get()->filter('Email', '[email protected]') |
||
276 | ); |
||
277 | |||
278 | // Test posting to parent comment |
||
279 | $parentComment = $this->objFromFixture(Comment::class, 'firstComA'); |
||
280 | $this->assertEquals(0, $parentComment->ChildComments()->count()); |
||
326 |
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.