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 |
||
10 | class RepositoryTest extends \PHPUnit_Framework_TestCase |
||
11 | { |
||
12 | View Code Duplication | public function testThatFindCallsFindOnFinder() |
|
|
|||
13 | { |
||
14 | $testQuery = 'Test Query'; |
||
15 | |||
16 | $finderMock = $this->getFinderMock($testQuery); |
||
17 | $repository = new Repository($finderMock); |
||
18 | $repository->find($testQuery); |
||
19 | } |
||
20 | |||
21 | public function testThatFindCallsFindOnFinderWithLimit() |
||
22 | { |
||
23 | $testQuery = 'Test Query'; |
||
24 | $testLimit = 20; |
||
25 | |||
26 | $finderMock = $this->getFinderMock($testQuery, $testLimit); |
||
27 | $repository = new Repository($finderMock); |
||
28 | $repository->find($testQuery, $testLimit); |
||
29 | } |
||
30 | |||
31 | View Code Duplication | public function testThatFindPaginatedCallsFindPaginatedOnFinder() |
|
32 | { |
||
33 | $testQuery = 'Test Query'; |
||
34 | |||
35 | $finderMock = $this->getFinderMock($testQuery, array(), 'findPaginated'); |
||
36 | $repository = new Repository($finderMock); |
||
37 | $repository->findPaginated($testQuery); |
||
38 | } |
||
39 | |||
40 | View Code Duplication | public function testThatCreatePaginatorCreatesAPaginatorViaFinder() |
|
48 | |||
49 | View Code Duplication | public function testThatFindHybridCallsFindHybridOnFinder() |
|
50 | { |
||
51 | $testQuery = 'Test Query'; |
||
57 | |||
58 | View Code Duplication | public function testThatFindRawResultCallsFindRawResultOnFinder() |
|
66 | |||
67 | View Code Duplication | public function testThatFindRawPaginatedCallsFindRawPaginatedOnFinder() |
|
75 | |||
76 | View Code Duplication | public function testThatCreateRawPaginatorCreatesAPaginatorViaFinder() |
|
84 | |||
85 | /** |
||
86 | * @param string $testQuery |
||
87 | * @param mixed $testLimit |
||
88 | * @param string $method |
||
89 | * |
||
90 | * @return \FOS\ElasticaBundle\Finder\TransformedFinder |
||
91 | */ |
||
92 | private function getFinderMock($testQuery, $testLimit = null, $method = 'find') |
||
103 | } |
||
104 |
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.