Completed
Push — legacy ( c3f933 )
by Simonas
95:41
created

FilterEndpoint::getBoolInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\ElasticsearchBundle\DSL\SearchEndpoint;
13
14
use ONGR\ElasticsearchBundle\DSL\Filter\BoolFilter;
15
use ONGR\ElasticsearchBundle\DSL\Query\FilteredQuery;
16
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
17
18
/**
19
 * Search filter dsl endpoint.
20
 */
21 View Code Duplication
class FilterEndpoint extends QueryEndpoint
0 ignored issues
show
Duplication introduced by
This class 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...
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
27
    {
28
        if ($this->getBuilder()) {
29
            $query = new FilteredQuery();
30
            !$this->isBool() ? : $this->getBuilder()->setParameters($this->getParameters());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface ONGR\ElasticsearchBundle\DSL\BuilderInterface as the method setParameters() does only exist in the following implementations of said interface: ONGR\ElasticsearchBundle...ion\AbstractAggregation, ONGR\ElasticsearchBundle...regation\AvgAggregation, ONGR\ElasticsearchBundle...\CardinalityAggregation, ONGR\ElasticsearchBundle...ion\ChildrenAggregation, ONGR\ElasticsearchBundle...on\DateRangeAggregation, ONGR\ElasticsearchBundle...xtendedStatsAggregation, ONGR\ElasticsearchBundle...ation\FilterAggregation, ONGR\ElasticsearchBundle...tion\FiltersAggregation, ONGR\ElasticsearchBundle...on\GeoBoundsAggregation, ONGR\ElasticsearchBundle...\GeoDistanceAggregation, ONGR\ElasticsearchBundle...\GeoHashGridAggregation, ONGR\ElasticsearchBundle...ation\GlobalAggregation, ONGR\ElasticsearchBundle...on\HistogramAggregation, ONGR\ElasticsearchBundle...on\Ipv4RangeAggregation, ONGR\ElasticsearchBundle...regation\MaxAggregation, ONGR\ElasticsearchBundle...regation\MinAggregation, ONGR\ElasticsearchBundle...tion\MissingAggregation, ONGR\ElasticsearchBundle...ation\NestedAggregation, ONGR\ElasticsearchBundle...centileRanksAggregation, ONGR\ElasticsearchBundle...\PercentilesAggregation, ONGR\ElasticsearchBundle...gation\RangeAggregation, ONGR\ElasticsearchBundle...everseNestedAggregation, ONGR\ElasticsearchBundle...gation\StatsAggregation, ONGR\ElasticsearchBundle...regation\SumAggregation, ONGR\ElasticsearchBundle...gation\TermsAggregation, ONGR\ElasticsearchBundle...tion\TopHitsAggregation, ONGR\ElasticsearchBundle...n\ValueCountAggregation, ONGR\ElasticsearchBundle\DSL\Bool\Bool, ONGR\ElasticsearchBundle\DSL\Filter\AndFilter, ONGR\ElasticsearchBundle\DSL\Filter\BoolFilter, ONGR\ElasticsearchBundle...er\GeoBoundingBoxFilter, ONGR\ElasticsearchBundle...ilter\GeoDistanceFilter, ONGR\ElasticsearchBundle...\GeoDistanceRangeFilter, ONGR\ElasticsearchBundle...Filter\GeoPolygonFilter, ONGR\ElasticsearchBundle\DSL\Filter\GeoShapeFilter, ONGR\ElasticsearchBundle...ilter\GeohashCellFilter, ONGR\ElasticsearchBundle\DSL\Filter\HasChildFilter, ONGR\ElasticsearchBundle...\Filter\HasParentFilter, ONGR\ElasticsearchBundle\DSL\Filter\IdsFilter, ONGR\ElasticsearchBundle\DSL\Filter\MissingFilter, ONGR\ElasticsearchBundle\DSL\Filter\NestedFilter, ONGR\ElasticsearchBundle\DSL\Filter\NotFilter, ONGR\ElasticsearchBundle\DSL\Filter\OrFilter, ONGR\ElasticsearchBundle\DSL\Filter\PrefixFilter, ONGR\ElasticsearchBundle\DSL\Filter\QueryFilter, ONGR\ElasticsearchBundle\DSL\Filter\RangeFilter, ONGR\ElasticsearchBundle\DSL\Filter\RegexpFilter, ONGR\ElasticsearchBundle\DSL\Filter\ScriptFilter, ONGR\ElasticsearchBundle\DSL\Filter\TermFilter, ONGR\ElasticsearchBundle\DSL\Filter\TermsFilter, ONGR\ElasticsearchBundle\DSL\Query\BoolQuery, ONGR\ElasticsearchBundle...\Query\CommonTermsQuery, ONGR\ElasticsearchBundle...uery\ConstantScoreQuery, ONGR\ElasticsearchBundle\DSL\Query\DisMaxQuery, ONGR\ElasticsearchBundle\DSL\Query\FilteredQuery, ONGR\ElasticsearchBundle...uery\FunctionScoreQuery, ONGR\ElasticsearchBundle...FuzzyLikeThisFieldQuery, ONGR\ElasticsearchBundle...uery\FuzzyLikeThisQuery, ONGR\ElasticsearchBundle\DSL\Query\FuzzyQuery, ONGR\ElasticsearchBundle\DSL\Query\HasChildQuery, ONGR\ElasticsearchBundle\DSL\Query\HasParentQuery, ONGR\ElasticsearchBundle\DSL\Query\IdsQuery, ONGR\ElasticsearchBundle\DSL\Query\MatchAllQuery, ONGR\ElasticsearchBundle\DSL\Query\MatchQuery, ONGR\ElasticsearchBundle...Query\MoreLikeThisQuery, ONGR\ElasticsearchBundle\DSL\Query\MultiMatchQuery, ONGR\ElasticsearchBundle\DSL\Query\PrefixQuery, ONGR\ElasticsearchBundle...\Query\QueryStringQuery, ONGR\ElasticsearchBundle\DSL\Query\RangeQuery, ONGR\ElasticsearchBundle\DSL\Query\RegexpQuery, ONGR\ElasticsearchBundle...\SimpleQueryStringQuery, ONGR\ElasticsearchBundle...ery\Span\SpanFirstQuery, ONGR\ElasticsearchBundle...Span\SpanMultiTermQuery, ONGR\ElasticsearchBundle...uery\Span\SpanNearQuery, ONGR\ElasticsearchBundle...Query\Span\SpanNotQuery, ONGR\ElasticsearchBundle...\Query\Span\SpanOrQuery, ONGR\ElasticsearchBundle...uery\Span\SpanTermQuery, ONGR\ElasticsearchBundle\DSL\Query\TermQuery, ONGR\ElasticsearchBundle\DSL\Query\TermsQuery, ONGR\ElasticsearchBundle\DSL\Query\WildcardQuery.

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...
31
            $query->setFilter($this->getBuilder());
32
            $this->addReference('filtered_query', $query);
0 ignored issues
show
Documentation introduced by
$query is of type object<ONGR\Elasticsearc...SL\Query\FilteredQuery>, but the function expects a array|string|object<stdClass>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getOrder()
40
    {
41
        return 1;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function getBoolInstance()
48
    {
49
        return new BoolFilter();
50
    }
51
}
52