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 |
||
8 | class CountOutputWalkerTest extends PaginationTestCase |
||
9 | { |
||
10 | public function testCountQuery() |
||
21 | |||
22 | public function testCountQuery_MixedResultsWithName() |
||
33 | |||
34 | View Code Duplication | public function testCountQuery_GroupBy(): void |
|
35 | { |
||
36 | $query = $this->entityManager->createQuery( |
||
37 | 'SELECT p.name FROM Doctrine\Tests\ORM\Tools\Pagination\Person p GROUP BY p.name'); |
||
38 | $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, CountOutputWalker::class); |
||
39 | $query->setFirstResult(null)->setMaxResults(null); |
||
40 | |||
41 | $this->assertSame( |
||
42 | "SELECT COUNT(*) AS dctrn_count FROM (SELECT p0_.name AS name_0 FROM Person p0_ GROUP BY p0_.name) dctrn_table", $query->getSQL() |
||
43 | ); |
||
44 | } |
||
45 | |||
46 | View Code Duplication | public function testCountQuery_Having(): void |
|
47 | { |
||
48 | $query = $this->entityManager->createQuery( |
||
49 | 'SELECT g, u, count(u.id) AS userCount FROM Doctrine\Tests\ORM\Tools\Pagination\Group g LEFT JOIN g.users u GROUP BY g.id HAVING userCount > 0'); |
||
50 | $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, CountOutputWalker::class); |
||
51 | $query->setFirstResult(null)->setMaxResults(null); |
||
52 | |||
53 | $this->assertSame( |
||
54 | "SELECT COUNT(*) AS dctrn_count FROM (SELECT count(u0_.id) AS sclr_0, g1_.id AS id_1, u0_.id AS id_2 FROM groups g1_ LEFT JOIN user_group u2_ ON g1_.id = u2_.group_id LEFT JOIN User u0_ ON u0_.id = u2_.user_id GROUP BY g1_.id HAVING sclr_0 > 0) dctrn_table", $query->getSQL() |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | public function testCountQueryOrderBySqlServer() |
||
74 | } |
||
75 | |||
76 |