Completed
Pull Request — master (#1875)
by romain
09:47
created

StatsTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 55.17 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 6
dl 32
loc 58
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testStatsAggregation() 0 15 1
A testStatsAggregationWithMissing() 16 16 1
A _getIndexForTest() 16 16 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Elastica\Test\Aggregation;
4
5
use Elastica\Aggregation\Stats;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Query;
9
10
/**
11
 * @internal
12
 */
13
class StatsTest extends BaseAggregationTest
14
{
15
    /**
16
     * @group functional
17
     */
18
    public function testStatsAggregation(): void
19
    {
20
        $agg = new Stats('stats');
21
        $agg->setField('price');
22
23
        $query = new Query();
24
        $query->addAggregation($agg);
25
        $results = $this->_getIndexForTest()->search($query)->getAggregation('stats');
26
27
        $this->assertEquals(4, $results['count']);
28
        $this->assertEquals(1, $results['min']);
29
        $this->assertEquals(8, $results['max']);
30
        $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results['avg']);
31
        $this->assertEquals((5 + 8 + 1 + 3), $results['sum']);
32
    }
33
34
    /**
35
     * @group functional
36
     */
37 View Code Duplication
    public function testStatsAggregationWithMissing(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
38
    {
39
        $agg = new Stats('stats');
40
        $agg->setField('price');
41
        $agg->setMissing(10);
42
43
        $query = new Query();
44
        $query->addAggregation($agg);
45
        $results = $this->_getIndexForTest()->search($query)->getAggregation('stats');
46
47
        $this->assertEquals(5, $results['count']);
48
        $this->assertEquals(1, $results['min']);
49
        $this->assertEquals(10, $results['max']);
50
        $this->assertEquals((5 + 8 + 1 + 3 + 10) / 5.0, $results['avg']);
51
        $this->assertEquals((5 + 8 + 1 + 3 + 10), $results['sum']);
52
    }
53
54 View Code Duplication
    protected function _getIndexForTest(): Index
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
55
    {
56
        $index = $this->_createIndex();
57
58
        $index->addDocuments([
59
            new Document(1, ['price' => 5]),
60
            new Document(2, ['price' => 8]),
61
            new Document(3, ['price' => 1]),
62
            new Document(4, ['price' => 3]),
63
            new Document(5, ['anything' => 'anything']),
64
        ]);
65
66
        $index->refresh();
67
68
        return $index;
69
    }
70
}
71