Completed
Push — master ( 9552d6...4b2b1b )
by Simonas
08:12
created

CompositeAggregation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A addSource() 0 8 1
A getArray() 0 6 1
A getType() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
16
use ONGR\ElasticsearchDSL\BuilderInterface;
17
18
/**
19
 * Class representing composite aggregation.
20
 *
21
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html
22
 */
23
class CompositeAggregation extends AbstractAggregation
24
{
25
    use BucketingTrait;
26
27
    /**
28
     * @var BuilderInterface[]
29
     */
30
    private $sources = [];
31
32
    /**
33
     * Inner aggregations container init.
34
     *
35
     * @param string             $name
36
     * @param BuilderInterface[] $sources
37
     */
38
    public function __construct($name, $sources = [])
39
    {
40
        parent::__construct($name);
41
42
        foreach ($sources as $agg) {
43
            $this->addSource($agg);
44
        }
45
    }
46
47
    /**
48
     * @param BuilderInterface $agg
49
     *
50
     * @throws \LogicException
51
     *
52
     * @return self
53
     */
54
    public function addSource(BuilderInterface $agg)
55
    {
56
        $this->sources[] = [
57
            $agg->getName() => [ $agg->getType() => $agg->getArray() ]
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface ONGR\ElasticsearchDSL\BuilderInterface as the method getName() does only exist in the following implementations of said interface: ONGR\ElasticsearchDSL\Ag...ion\AbstractAggregation, ONGR\ElasticsearchDSL\Ag...acencyMatrixAggregation, ONGR\ElasticsearchDSL\Ag...ateHistogramAggregation, ONGR\ElasticsearchDSL\Ag...ing\ChildrenAggregation, ONGR\ElasticsearchDSL\Ag...ng\CompositeAggregation, ONGR\ElasticsearchDSL\Ag...ateHistogramAggregation, ONGR\ElasticsearchDSL\Ag...ng\DateRangeAggregation, ONGR\ElasticsearchDSL\Ag...ifiedSamplerAggregation, ONGR\ElasticsearchDSL\Ag...eting\FilterAggregation, ONGR\ElasticsearchDSL\Ag...ting\FiltersAggregation, ONGR\ElasticsearchDSL\Ag...\GeoDistanceAggregation, ONGR\ElasticsearchDSL\Ag...\GeoHashGridAggregation, ONGR\ElasticsearchDSL\Ag...eting\GlobalAggregation, ONGR\ElasticsearchDSL\Ag...ng\HistogramAggregation, ONGR\ElasticsearchDSL\Ag...ng\Ipv4RangeAggregation, ONGR\ElasticsearchDSL\Ag...ting\MissingAggregation, ONGR\ElasticsearchDSL\Ag...eting\NestedAggregation, ONGR\ElasticsearchDSL\Ag...keting\RangeAggregation, ONGR\ElasticsearchDSL\Ag...everseNestedAggregation, ONGR\ElasticsearchDSL\Ag...ting\SamplerAggregation, ONGR\ElasticsearchDSL\Ag...ificantTermsAggregation, ONGR\ElasticsearchDSL\Ag...nificantTextAggregation, ONGR\ElasticsearchDSL\Ag...keting\TermsAggregation, ONGR\ElasticsearchDSL\Ag...n\Matrix\MaxAggregation, ONGR\ElasticsearchDSL\Ag...n\Metric\AvgAggregation, ONGR\ElasticsearchDSL\Ag...\CardinalityAggregation, ONGR\ElasticsearchDSL\Ag...xtendedStatsAggregation, ONGR\ElasticsearchDSL\Ag...ic\GeoBoundsAggregation, ONGR\ElasticsearchDSL\Ag...\GeoCentroidAggregation, ONGR\ElasticsearchDSL\Ag...n\Metric\MaxAggregation, ONGR\ElasticsearchDSL\Ag...n\Metric\MinAggregation, ONGR\ElasticsearchDSL\Ag...centileRanksAggregation, ONGR\ElasticsearchDSL\Ag...\PercentilesAggregation, ONGR\ElasticsearchDSL\Ag...riptedMetricAggregation, ONGR\ElasticsearchDSL\Ag...Metric\StatsAggregation, ONGR\ElasticsearchDSL\Ag...n\Metric\SumAggregation, ONGR\ElasticsearchDSL\Ag...tric\TopHitsAggregation, ONGR\ElasticsearchDSL\Ag...c\ValueCountAggregation, ONGR\ElasticsearchDSL\Ag...ractPipelineAggregation, ONGR\ElasticsearchDSL\Ag...ne\AvgBucketAggregation, ONGR\ElasticsearchDSL\Ag...BucketScriptAggregation, ONGR\ElasticsearchDSL\Ag...cketSelectorAggregation, ONGR\ElasticsearchDSL\Ag...e\BucketSortAggregation, ONGR\ElasticsearchDSL\Ag...umulativeSumAggregation, ONGR\ElasticsearchDSL\Ag...e\DerivativeAggregation, ONGR\ElasticsearchDSL\Ag...dStatsBucketAggregation, ONGR\ElasticsearchDSL\Ag...ne\MaxBucketAggregation, ONGR\ElasticsearchDSL\Ag...ne\MinBucketAggregation, ONGR\ElasticsearchDSL\Ag...ovingAverageAggregation, ONGR\ElasticsearchDSL\Ag...vingFunctionAggregation, ONGR\ElasticsearchDSL\Ag...ntilesBucketAggregation, ONGR\ElasticsearchDSL\Ag...DifferencingAggregation, ONGR\ElasticsearchDSL\Ag...\StatsBucketAggregation, ONGR\ElasticsearchDSL\Ag...ne\SumBucketAggregation, ONGR\ElasticsearchDSL\InnerHit\NestedInnerHit, ONGR\ElasticsearchDSL\InnerHit\ParentInnerHit, ONGR\ElasticsearchDSL\Suggest\Suggest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
Bug introduced by
It seems like you code against a concrete implementation and not the interface ONGR\ElasticsearchDSL\BuilderInterface as the method getArray() does only exist in the following implementations of said interface: ONGR\ElasticsearchDSL\Ag...ion\AbstractAggregation, ONGR\ElasticsearchDSL\Ag...acencyMatrixAggregation, ONGR\ElasticsearchDSL\Ag...ateHistogramAggregation, ONGR\ElasticsearchDSL\Ag...ing\ChildrenAggregation, ONGR\ElasticsearchDSL\Ag...ng\CompositeAggregation, ONGR\ElasticsearchDSL\Ag...ateHistogramAggregation, ONGR\ElasticsearchDSL\Ag...ng\DateRangeAggregation, ONGR\ElasticsearchDSL\Ag...ifiedSamplerAggregation, ONGR\ElasticsearchDSL\Ag...eting\FilterAggregation, ONGR\ElasticsearchDSL\Ag...ting\FiltersAggregation, ONGR\ElasticsearchDSL\Ag...\GeoDistanceAggregation, ONGR\ElasticsearchDSL\Ag...\GeoHashGridAggregation, ONGR\ElasticsearchDSL\Ag...eting\GlobalAggregation, ONGR\ElasticsearchDSL\Ag...ng\HistogramAggregation, ONGR\ElasticsearchDSL\Ag...ng\Ipv4RangeAggregation, ONGR\ElasticsearchDSL\Ag...ting\MissingAggregation, ONGR\ElasticsearchDSL\Ag...eting\NestedAggregation, ONGR\ElasticsearchDSL\Ag...keting\RangeAggregation, ONGR\ElasticsearchDSL\Ag...everseNestedAggregation, ONGR\ElasticsearchDSL\Ag...ting\SamplerAggregation, ONGR\ElasticsearchDSL\Ag...ificantTermsAggregation, ONGR\ElasticsearchDSL\Ag...nificantTextAggregation, ONGR\ElasticsearchDSL\Ag...keting\TermsAggregation, ONGR\ElasticsearchDSL\Ag...n\Matrix\MaxAggregation, ONGR\ElasticsearchDSL\Ag...n\Metric\AvgAggregation, ONGR\ElasticsearchDSL\Ag...\CardinalityAggregation, ONGR\ElasticsearchDSL\Ag...xtendedStatsAggregation, ONGR\ElasticsearchDSL\Ag...ic\GeoBoundsAggregation, ONGR\ElasticsearchDSL\Ag...\GeoCentroidAggregation, ONGR\ElasticsearchDSL\Ag...n\Metric\MaxAggregation, ONGR\ElasticsearchDSL\Ag...n\Metric\MinAggregation, ONGR\ElasticsearchDSL\Ag...centileRanksAggregation, ONGR\ElasticsearchDSL\Ag...\PercentilesAggregation, ONGR\ElasticsearchDSL\Ag...riptedMetricAggregation, ONGR\ElasticsearchDSL\Ag...Metric\StatsAggregation, ONGR\ElasticsearchDSL\Ag...n\Metric\SumAggregation, ONGR\ElasticsearchDSL\Ag...tric\TopHitsAggregation, ONGR\ElasticsearchDSL\Ag...c\ValueCountAggregation, ONGR\ElasticsearchDSL\Ag...ractPipelineAggregation, ONGR\ElasticsearchDSL\Ag...ne\AvgBucketAggregation, ONGR\ElasticsearchDSL\Ag...BucketScriptAggregation, ONGR\ElasticsearchDSL\Ag...cketSelectorAggregation, ONGR\ElasticsearchDSL\Ag...e\BucketSortAggregation, ONGR\ElasticsearchDSL\Ag...umulativeSumAggregation, ONGR\ElasticsearchDSL\Ag...e\DerivativeAggregation, ONGR\ElasticsearchDSL\Ag...dStatsBucketAggregation, ONGR\ElasticsearchDSL\Ag...ne\MaxBucketAggregation, ONGR\ElasticsearchDSL\Ag...ne\MinBucketAggregation, ONGR\ElasticsearchDSL\Ag...ovingAverageAggregation, ONGR\ElasticsearchDSL\Ag...vingFunctionAggregation, ONGR\ElasticsearchDSL\Ag...ntilesBucketAggregation, ONGR\ElasticsearchDSL\Ag...DifferencingAggregation, ONGR\ElasticsearchDSL\Ag...\StatsBucketAggregation, ONGR\ElasticsearchDSL\Ag...ne\SumBucketAggregation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
58
        ];
59
60
        return $this;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getArray()
67
    {
68
        return [
69
            'sources' => $this->sources,
70
        ];
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getType()
77
    {
78
        return 'composite';
79
    }
80
}
81