GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 619fa8...db31ee )
by Anderson
02:20
created

ElasticWalker::getEntityElasticField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
3
namespace DoctrineElastic\Query;
4
5
use Doctrine\ORM\Query\AST\FromClause;
6
use Doctrine\ORM\Query\AST\GroupByClause;
7
use Doctrine\ORM\Query\AST\HavingClause;
8
use Doctrine\ORM\Query\AST\OrderByClause;
9
use Doctrine\ORM\Query\AST\OrderByItem;
10
use Doctrine\ORM\Query\AST\SelectClause;
11
use Doctrine\ORM\Query\AST\SelectStatement;
12
use Doctrine\ORM\Query\AST\WhereClause;
13
use DoctrineElastic\Elastic\ElasticQuery;
14
use DoctrineElastic\Elastic\SearchParams;
15
use DoctrineElastic\Hydrate\AnnotationEntityHydrator;
16
use DoctrineElastic\Mapping\Field;
17
use DoctrineElastic\Query\Walker\Helper\WalkerHelper;
18
use DoctrineElastic\Query\Walker\WhereWalker;
19
20
/**
21
 * Main walker for queries
22
 *
23
 * @uahtor Ands
24
 */
25
class ElasticWalker {
26
27
    /** @var ElasticQuery */
28
    protected $query;
29
30
    /** @var string */
31
    private $_className;
32
33
    /** @var WalkerHelper */
34
    private $walkerHelper;
35
36
    /** @var SelectStatement */
37
    private $_ast;
38
39
    public function __construct(ElasticQuery $query, SelectStatement $AST, $className) {
40
        $this->query = $query;
41
        $this->walkerHelper = new WalkerHelper();
42
        $this->_ast = $AST;
43
        $this->_className = $className;
44
    }
45
46
    /**
47
     * @return SearchParams
48
     */
49
    public function walkSelectStatement() {
50
        $searchParams = new SearchParams();
51
        $size = $this->query->getMaxResults();
52
        $offset = $this->query->getFirstResult();
53
54
        $persister = $this->query->getEntityManager()->getUnitOfWork()->getEntityPersister($this->_className);
55
        $type = $persister->getEntityType();
56
57
        $searchParams->setType($type->getName());
58
        $searchParams->setIndex($type->getIndex());
59
60
        $this->walkSelectClause($this->_ast->selectClause, $searchParams);
0 ignored issues
show
Unused Code introduced by
The call to the method DoctrineElastic\Query\El...ker::walkSelectClause() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
61
        $this->walkFromClause($this->_ast->fromClause, $searchParams);
0 ignored issues
show
Unused Code introduced by
The call to the method DoctrineElastic\Query\El...alker::walkFromClause() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
62
63
        if ($this->_ast->whereClause) {
64
            $this->walkWhereClause($this->_ast->whereClause, $searchParams);
65
        }
66
67
        if ($this->_ast->groupByClause) {
68
            $this->walkGroupByClause($this->_ast->groupByClause, $searchParams);
0 ignored issues
show
Unused Code introduced by
The call to the method DoctrineElastic\Query\El...er::walkGroupByClause() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
69
        }
70
71
        if ($this->_ast->havingClause) {
72
            $this->walkHavingClause($this->_ast->havingClause, $searchParams);
0 ignored issues
show
Unused Code introduced by
The call to the method DoctrineElastic\Query\El...ker::walkHavingClause() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
73
        }
74
75
        if ($this->_ast->orderByClause) {
76
            $this->walkOrderByClause($this->_ast->orderByClause, $searchParams);
77
        }
78
79
        $searchParams->setSize($size);
80
        $searchParams->setFrom($offset);
81
82
        return $searchParams;
83
    }
84
85
    private function walkSelectClause(SelectClause $selectClause, SearchParams $searchParams) {
0 ignored issues
show
Unused Code introduced by
The parameter $selectClause is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $searchParams is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
87
    }
88
89
    private function walkFromClause(FromClause $fromClause, SearchParams $searchParams) {
0 ignored issues
show
Unused Code introduced by
The parameter $fromClause is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $searchParams is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
91
    }
92
93
    private function walkWhereClause(WhereClause $whereClause, SearchParams $searchParams) {
94
        $whereWalker = new WhereWalker($this->query, $this->_className, $this->walkerHelper);
95
        $whereWalker->walk($whereClause, $searchParams);
96
    }
97
98
    private function walkGroupByClause(GroupByClause $groupByClause, SearchParams $searchParams) {
0 ignored issues
show
Unused Code introduced by
The parameter $groupByClause is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $searchParams is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
100
    }
101
102
    private function walkHavingClause(HavingClause $havingClause, SearchParams $searchParams) {
0 ignored issues
show
Unused Code introduced by
The parameter $havingClause is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $searchParams is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
104
    }
105
106
    private function walkOrderByClause(OrderByClause $orderByClause, SearchParams $searchParams) {
107
        $sort = [];
108
109
        /** @var OrderByItem $item */
110
        foreach ($orderByClause->orderByItems as $item) {
111
            $order = $item->type;
112
            $propertyName = $item->expression->field;
113
            $ESField = $this->getEntityElasticField($propertyName, $this->_className);
114
115
            if (is_null($ESField)) {
116
                continue;
117
            }
118
119
            $sort[$ESField->name] = strtolower($order);
120
        }
121
122
        if (!empty($sort)) {
123
            $searchParams->setSort($sort);
124
        }
125
    }
126
127
    /**
128
     * @param $propertyName
129
     * @param $className
130
     * @return Field
131
     */
132
    private function getEntityElasticField($propertyName, $className) {
133
        /** @var Field[] $fields */
134
        $fields = (new AnnotationEntityHydrator())->extractSpecAnnotations($className, Field::class);
135
136
        return isset($fields[$propertyName]) ? $fields[$propertyName]: null;
137
    }
138
}
139