Completed
Push — master ( 2a17c4...6c9334 )
by Simonas
02:29
created

src/SearchEndpoint/InnerHitsEndpoint.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\SearchEndpoint;
13
14
use ONGR\ElasticsearchDSL\InnerHit\NestedInnerHit;
15
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
16
17
/**
18
 * Search inner hits dsl endpoint.
19
 */
20 View Code Duplication
class InnerHitsEndpoint extends AbstractSearchEndpoint
21
{
22
    /**
23
     * Endpoint name
24
     */
25
    const NAME = 'inner_hits';
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
31
    {
32
        $output = [];
33
        if (count($this->getAll()) > 0) {
34
            /** @var NestedInnerHit $innerHit */
35
            foreach ($this->getAll() as $innerHit) {
36
                $output[$innerHit->getName()] = $innerHit->toArray();
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...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...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...
37
            }
38
        }
39
40
        return $output;
41
    }
42
}
43