HighlightEndpoint::getHighlight()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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\ElasticsearchDSL\SearchEndpoint;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
16
17
/**
18
 * Search highlight dsl endpoint.
19
 */
20
class HighlightEndpoint extends AbstractSearchEndpoint
21
{
22
    /**
23
     * Endpoint name
24
     */
25
    const NAME = 'highlight';
26
27
    /**
28
     * @var BuilderInterface
29
     */
30
    private $highlight;
31
32
    /**
33
     * @var string Key for highlight storing.
34
     */
35
    private $key;
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
41
    {
42
        if ($this->highlight) {
43
            return $this->highlight->toArray();
44
        }
45
46
        return null;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function add(BuilderInterface $builder, $key = null)
53
    {
54
        if ($this->highlight) {
55
            throw new \OverflowException('Only one highlight can be set');
56
        }
57
58
        $this->key = $key;
0 ignored issues
show
Documentation Bug introduced by
It seems like $key can also be of type array. However, the property $key is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
        $this->highlight = $builder;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getAll($boolType = null)
66
    {
67
        return [$this->key => $this->highlight];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array($this->key => $this->highlight); (array<string,ONGR\Elasti...chDSL\BuilderInterface>) is incompatible with the return type of the parent method ONGR\ElasticsearchDSL\Se...tSearchEndpoint::getAll of type ONGR\ElasticsearchDSL\BuilderInterface[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
68
    }
69
70
    /**
71
     * @return BuilderInterface
72
     */
73
    public function getHighlight()
74
    {
75
        return $this->highlight;
76
    }
77
}
78