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 | class ModelTest extends PHPUnit_Framework_TestCase |
||
8 | { |
||
9 | /** |
||
10 | * Instance of DatabaseConnection used in test. |
||
11 | */ |
||
12 | protected $databaseConnection; |
||
13 | |||
14 | protected $model; |
||
15 | |||
16 | protected $sqlStatement; |
||
17 | |||
18 | public function setUp() |
||
34 | |||
35 | public function testGetTableNameReturnsCorrectTableName() |
||
44 | |||
45 | public function testGetReturnsAnObjectWhenIdIsFoundInDatabase() |
||
55 | |||
56 | /** |
||
57 | * @expectedException Pyjac\ORM\Exception\ModelNotFoundException |
||
58 | */ |
||
59 | public function testGetThrowsModelNotFoundExceptionWhenIdNotFoundInDatabase() |
||
60 | { |
||
61 | $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
||
62 | $this->sqlStatement->shouldReceive('setFetchMode'); |
||
63 | $this->sqlStatement->shouldReceive('execute'); |
||
64 | $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0); |
||
65 | |||
66 | $this->assertInstanceOf('stdClass', $this->model->get(1)); |
||
67 | } |
||
68 | |||
69 | public function testAllReturnsAnArrayOfObjectsWhenValuesAreInDatabase() |
||
76 | |||
77 | View Code Duplication | public function testUpdateChangesTheValueOfObjectInDatabase() |
|
78 | { |
||
79 | $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']); |
||
80 | $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
||
81 | $this->sqlStatement->shouldReceive('execute'); |
||
82 | $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
||
83 | |||
84 | $this->assertEquals(1, $this->model->update()); |
||
85 | } |
||
86 | |||
87 | View Code Duplication | public function testCreateObjectInDatabase() |
|
88 | { |
||
89 | $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']); |
||
90 | $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
||
91 | $this->sqlStatement->shouldReceive('execute'); |
||
92 | $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
||
93 | |||
94 | $this->assertEquals(1, $this->model->create()); |
||
95 | } |
||
96 | |||
97 | View Code Duplication | public function testSaveShouldCreateNewModelInDatabaseWhenIdNotPresent() |
|
105 | |||
106 | View Code Duplication | public function testSaveShouldUpdateModelInDatabaseIfIdIsPresent() |
|
107 | { |
||
108 | $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']); |
||
109 | $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
||
110 | $this->sqlStatement->shouldReceive('execute'); |
||
111 | $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
||
112 | $this->assertEquals(1, $this->model->save()); |
||
113 | } |
||
114 | |||
115 | public function testGetPropertiesReturnsExpectedArrayValues() |
||
122 | |||
123 | public function testDeleteReturnsTrueWhenModelIsSuccessfullyRemovedFromDatabase() |
||
124 | { |
||
125 | $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
||
126 | $this->sqlStatement->shouldReceive('execute'); |
||
127 | $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
||
128 | |||
129 | $this->assertEquals(true, $this->model->delete(1)); |
||
130 | } |
||
131 | |||
132 | public function testDeleteReturnsFalseWhenModelWasNotDeletedFromDatabase() |
||
133 | { |
||
134 | $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
||
135 | $this->sqlStatement->shouldReceive('execute'); |
||
136 | $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0); |
||
137 | |||
138 | $this->assertEquals(false, $this->model->delete(1)); |
||
139 | } |
||
140 | |||
141 | public function testMagicMethodsReturnCorrectResult() |
||
151 | } |
||
152 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.