Completed
Push — master ( ed9104...e0bfc4 )
by Simonas
02:44
created

src/BuilderBag.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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;
13
14
/**
15
 * Container for named builders.
16
 */
17
class BuilderBag
18
{
19
    /**
20
     * @var BuilderInterface[]
21
     */
22
    private $bag = [];
23
24
    /**
25
     * @param BuilderInterface[] $builders
26
     */
27
    public function __construct($builders = [])
28
    {
29
        foreach ($builders as $builder) {
30
            $this->add($builder);
31
        }
32
    }
33
34
    /**
35
     * Adds a builder.
36
     *
37
     * @param BuilderInterface $builder
38
     *
39
     * @return string
40
     */
41
    public function add(BuilderInterface $builder)
42
    {
43
        if (method_exists($builder, 'getName')) {
44
            $name = $builder->getName();
0 ignored issues
show
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...ing\ChildrenAggregation, ONGR\ElasticsearchDSL\Ag...ateHistogramAggregation, ONGR\ElasticsearchDSL\Ag...ng\DateRangeAggregation, 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...keting\TermsAggregation, 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...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...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...
45
        } else {
46
            $name = uniqid();
47
        }
48
49
        $this->bag[$name] = $builder;
50
51
        return $name;
52
    }
53
54
    /**
55
     * Checks if builder exists by a specific name.
56
     *
57
     * @param string $name Builder name.
58
     *
59
     * @return bool
60
     */
61
    public function has($name)
62
    {
63
        return isset($this->bag[$name]);
64
    }
65
66
    /**
67
     * Removes a builder by name.
68
     *
69
     * @param string $name Builder name.
70
     */
71
    public function remove($name)
72
    {
73
        unset($this->bag[$name]);
74
    }
75
76
    /**
77
     * Clears contained builders.
78
     */
79
    public function clear()
80
    {
81
        $this->bag = [];
82
    }
83
84
    /**
85
     * Returns a builder by name.
86
     *
87
     * @param string $name Builder name.
88
     *
89
     * @return BuilderInterface
90
     */
91
    public function get($name)
92
    {
93
        return $this->bag[$name];
94
    }
95
96
    /**
97
     * Returns all builders contained.
98
     *
99
     * @param string|null $type Builder type.
100
     *
101
     * @return BuilderInterface[]
102
     */
103
    public function all($type = null)
104
    {
105
        return array_filter(
106
            $this->bag,
107
            /** @var BuilderInterface $builder */
108
            function (BuilderInterface $builder) use ($type) {
109
                return $type === null || $builder->getType() == $type;
110
            }
111
        );
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function toArray()
118
    {
119
        $output = [];
120
        foreach ($this->all() as $builder) {
121
            $output = array_merge($output, $builder->toArray());
122
        }
123
124
        return $output;
125
    }
126
}
127