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 | class BlogCategoryTest extends FunctionalTest |
||
|
|||
7 | { |
||
8 | /** |
||
9 | * @var string |
||
10 | */ |
||
11 | public static $fixture_file = 'blog.yml'; |
||
12 | |||
13 | /** |
||
14 | * {@inheritdoc} |
||
15 | */ |
||
16 | public function setUp() |
||
17 | { |
||
18 | parent::setUp(); |
||
19 | |||
20 | SS_Datetime::set_mock_now('2013-10-10 20:00:00'); |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * {@inheritdoc} |
||
25 | */ |
||
26 | public function tearDown() |
||
27 | { |
||
28 | SS_Datetime::clear_mock_now(); |
||
29 | |||
30 | parent::tearDown(); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Tests that any blog posts returned from $category->BlogPosts() many_many are published, |
||
35 | * both by normal 'save & publish' functionality and by publish date. |
||
36 | */ |
||
37 | View Code Duplication | public function testBlogPosts() |
|
38 | { |
||
39 | $member = Member::currentUser(); |
||
40 | |||
41 | if ($member) { |
||
42 | $member->logout(); |
||
43 | } |
||
44 | |||
45 | $this->objFromFixture('BlogPost', 'FirstBlogPost'); |
||
46 | |||
47 | /** |
||
48 | * @var BlogCategory $category |
||
49 | */ |
||
50 | $category = $this->objFromFixture('BlogCategory', 'FirstCategory'); |
||
51 | |||
52 | $this->assertEquals(5, $category->BlogPosts()->count(), 'Category blog post count'); |
||
53 | } |
||
54 | |||
55 | public function testCanView() |
||
56 | { |
||
57 | $this->useDraftSite(); |
||
58 | |||
59 | $this->objFromFixture('Member', 'Admin'); |
||
60 | |||
61 | $editor = $this->objFromFixture('Member', 'Editor'); |
||
62 | $category = $this->objFromFixture('BlogCategory', 'SecondCategory'); |
||
63 | |||
64 | $this->assertFalse($category->canView($editor), 'Editor should not be able to view category.'); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * The first blog can be viewed by anybody. |
||
69 | */ |
||
70 | View Code Duplication | public function testCanEdit() |
|
92 | |||
93 | View Code Duplication | public function testCanCreate() |
|
94 | { |
||
95 | $this->useDraftSite(); |
||
96 | |||
105 | |||
106 | View Code Duplication | public function testCanDelete() |
|
126 | |||
127 | View Code Duplication | public function testDuplicateCategories() { |
|
149 | } |
||
150 |
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.