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 |
||
| 14 | class NotBuilderTest extends \PHPUnit_Framework_TestCase |
||
| 15 | { |
||
| 16 | public function testConstruct() |
||
| 22 | |||
| 23 | public function testBuildReturnsNotExpression() |
||
| 24 | { |
||
| 25 | $not = $this->createNot(); |
||
| 26 | $registry = $this->createRegistry($not); |
||
| 27 | |||
| 28 | $builder = new NotBuilder($registry); |
||
| 29 | |||
| 30 | $query = $builder->build($not, new QueryBuilder()); |
||
| 31 | |||
| 32 | $this->assertInstanceOf(BoolQuery::class, $query); |
||
| 33 | |||
| 34 | $this->assertArrayHasKey('bool', $query->toArray()); |
||
| 35 | $this->assertArrayHasKey('must_not', $query->toArray()['bool']); |
||
| 36 | $this->assertCount(1, $query->toArray()['bool']['must_not']); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return Not |
||
| 41 | */ |
||
| 42 | private function createNot() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param Not $not |
||
| 51 | * |
||
| 52 | * @return Registry |
||
| 53 | */ |
||
| 54 | private function createRegistry($not) |
||
| 69 | |||
| 70 | |||
| 71 | View Code Duplication | public function testBuildThrowExceptionIfNotNotSpecification() |
|
| 81 | } |
||
| 82 |
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.