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 | abstract class BaseRepositoryTest extends BaseTest |
||
8 | { |
||
9 | abstract public function getRepository(); |
||
10 | |||
11 | abstract public function getMockObject(array $except = []); |
||
12 | |||
13 | public function getFlushedMockObject(array $except = []) |
||
23 | |||
24 | public function getMockArray(array $except = []) |
||
28 | |||
29 | public function getFlushedMockArray(array $except = []) |
||
33 | |||
34 | View Code Duplication | public function testFindAll() |
|
35 | { |
||
36 | $entity = $this->getFlushedMockObject(); |
||
37 | |||
38 | $findAll = $this->getRepository()->findAll()->getResult(); |
||
39 | |||
40 | $this->assertGreaterThan(0, count($findAll)); |
||
41 | $this->assertInstanceOf($this->getRepository()->getEntityName(), $findAll[0]); |
||
42 | } |
||
43 | |||
44 | public function testFindBy() |
||
55 | |||
56 | View Code Duplication | public function testFindOneBy() |
|
67 | |||
68 | View Code Duplication | public function testFind() |
|
79 | |||
80 | /** |
||
81 | * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
82 | */ |
||
83 | public function testRemove() |
||
94 | } |
||
95 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.