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 |
||
23 | abstract class StatementRepositoryTest extends TestCase |
||
24 | { |
||
25 | const UUID_REGEXP = '/^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i'; |
||
26 | |||
27 | /** |
||
28 | * @var StatementRepository |
||
29 | */ |
||
30 | private $statementRepository; |
||
31 | |||
32 | protected function setUp() |
||
33 | { |
||
34 | $this->statementRepository = $this->createStatementRepository(); |
||
35 | $this->cleanDatabase(); |
||
36 | } |
||
37 | |||
38 | protected function tearDown() |
||
39 | { |
||
40 | $this->cleanDatabase(); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException |
||
45 | */ |
||
46 | public function testFetchingNonExistingStatementThrowsException() |
||
47 | { |
||
48 | $this->statementRepository->findStatementById(StatementId::fromString('12345678-1234-5678-8234-567812345678')); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException |
||
53 | */ |
||
54 | public function testFetchingStatementAsVoidedStatementThrowsException() |
||
55 | { |
||
56 | $statement = StatementFixtures::getTypicalStatement()->withId(null); |
||
57 | $statementId = $this->statementRepository->storeStatement($statement); |
||
58 | |||
59 | $this->statementRepository->findVoidedStatementById($statementId); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @dataProvider getStatementsWithoutId |
||
64 | */ |
||
65 | View Code Duplication | public function testUuidIsGeneratedForNewStatementIfNotPresent(Statement $statement) |
|
|
|||
66 | { |
||
67 | $statement = $statement->withId(null); |
||
68 | $statementId = $this->statementRepository->storeStatement($statement); |
||
69 | |||
70 | $this->assertNull($statement->getId()); |
||
71 | $this->assertRegExp(self::UUID_REGEXP, $statementId->getValue()); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @dataProvider getStatementsWithId |
||
76 | */ |
||
77 | public function testUuidIsNotGeneratedForNewStatementIfPresent(Statement $statement) |
||
78 | { |
||
79 | $statementId = $this->statementRepository->storeStatement($statement); |
||
80 | |||
81 | $this->assertEquals($statement->getId(), $statementId); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @dataProvider getStatementsWithId |
||
86 | */ |
||
87 | public function testCreatedStatementCanBeRetrievedByOriginalId(Statement $statement) |
||
88 | { |
||
89 | $this->statementRepository->storeStatement($statement); |
||
90 | |||
91 | View Code Duplication | if ($statement->getVerb()->isVoidVerb()) { |
|
92 | $fetchedStatement = $this->statementRepository->findVoidedStatementById($statement->getId()); |
||
93 | } else { |
||
94 | $fetchedStatement = $this->statementRepository->findStatementById($statement->getId()); |
||
95 | } |
||
96 | |||
97 | $this->assertTrue($statement->equals($fetchedStatement)); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @dataProvider getStatementsWithoutId |
||
102 | */ |
||
103 | public function testCreatedStatementCanBeRetrievedByGeneratedId(Statement $statement) |
||
104 | { |
||
105 | $statement =$statement->withId(null); |
||
106 | $statementId = $this->statementRepository->storeStatement($statement); |
||
107 | |||
108 | View Code Duplication | if ($statement->getVerb()->isVoidVerb()) { |
|
109 | $fetchedStatement = $this->statementRepository->findVoidedStatementById($statementId); |
||
110 | } else { |
||
111 | $fetchedStatement = $this->statementRepository->findStatementById($statementId); |
||
112 | } |
||
113 | |||
114 | $this->assertNull($statement->getId()); |
||
115 | $this->assertTrue($statement->equals($fetchedStatement->withId(null))); |
||
116 | } |
||
117 | |||
118 | View Code Duplication | public function getStatementsWithId() |
|
119 | { |
||
120 | $fixtures = array(); |
||
121 | |||
122 | foreach (get_class_methods('Xabbuh\XApi\DataFixtures\StatementFixtures') as $method) { |
||
123 | $statement = call_user_func(array('Xabbuh\XApi\DataFixtures\StatementFixtures', $method)); |
||
124 | |||
125 | if ($statement instanceof Statement) { |
||
126 | $fixtures[$method] = array($statement->withId(StatementId::fromString(StatementFixtures::DEFAULT_STATEMENT_ID))); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | return $fixtures; |
||
131 | } |
||
132 | |||
133 | View Code Duplication | public function getStatementsWithoutId() |
|
134 | { |
||
135 | $fixtures = array(); |
||
136 | |||
137 | foreach (get_class_methods('Xabbuh\XApi\DataFixtures\StatementFixtures') as $method) { |
||
138 | $statement = call_user_func(array('Xabbuh\XApi\DataFixtures\StatementFixtures', $method)); |
||
139 | |||
140 | if ($statement instanceof Statement) { |
||
141 | $fixtures[$method] = array($statement->withId(null)); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | return $fixtures; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException |
||
150 | */ |
||
151 | public function testFetchingNonExistingVoidStatementThrowsException() |
||
152 | { |
||
153 | $this->statementRepository->findVoidedStatementById(StatementId::fromString('12345678-1234-5678-8234-567812345678')); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException |
||
158 | */ |
||
159 | public function testFetchingVoidStatementAsStatementThrowsException() |
||
160 | { |
||
161 | $statement = StatementFixtures::getVoidingStatement()->withId(null); |
||
162 | $statementId = $this->statementRepository->storeStatement($statement); |
||
163 | |||
164 | $this->statementRepository->findStatementById($statementId); |
||
165 | } |
||
166 | |||
167 | View Code Duplication | public function testUuidIsGeneratedForNewVoidStatementIfNotPresent() |
|
168 | { |
||
169 | $statement = StatementFixtures::getVoidingStatement()->withId(null); |
||
170 | $statementId = $this->statementRepository->storeStatement($statement); |
||
171 | |||
172 | $this->assertNull($statement->getId()); |
||
173 | $this->assertRegExp(self::UUID_REGEXP, $statementId->getValue()); |
||
174 | } |
||
175 | |||
176 | public function testUuidIsNotGeneratedForNewVoidStatementIfPresent() |
||
177 | { |
||
178 | $statement = StatementFixtures::getVoidingStatement(); |
||
179 | $statementId = $this->statementRepository->storeStatement($statement); |
||
180 | |||
181 | $this->assertEquals($statement->getId(), $statementId); |
||
182 | } |
||
183 | |||
184 | public function testCreatedVoidStatementCanBeRetrievedByOriginalId() |
||
185 | { |
||
186 | $statement = StatementFixtures::getVoidingStatement(); |
||
187 | $this->statementRepository->storeStatement($statement); |
||
188 | $fetchedStatement = $this->statementRepository->findVoidedStatementById($statement->getId()); |
||
189 | |||
190 | $this->assertTrue($statement->equals($fetchedStatement)); |
||
191 | } |
||
192 | |||
193 | public function testCreatedVoidStatementCanBeRetrievedByGeneratedId() |
||
194 | { |
||
195 | $statement = StatementFixtures::getVoidingStatement()->withId(null); |
||
196 | $statementId = $this->statementRepository->storeStatement($statement); |
||
197 | $fetchedStatement = $this->statementRepository->findVoidedStatementById($statementId); |
||
198 | |||
199 | $this->assertNull($statement->getId()); |
||
200 | $this->assertTrue($statement->equals($fetchedStatement->withId(null))); |
||
201 | } |
||
202 | |||
203 | abstract protected function createStatementRepository(); |
||
204 | |||
205 | abstract protected function cleanDatabase(); |
||
206 | } |
||
207 |
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.