Completed
Push — master ( b05eaa...0a1951 )
by Paweł
16:26 queued 07:46
created

ExpressionBuilder::hasParameterName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\GridBundle\Doctrine\ORM;
13
14
use Doctrine\ORM\QueryBuilder;
15
use Sylius\Component\Grid\Data\ExpressionBuilderInterface;
16
17
/**
18
 * @author Paweł Jędrzejewski <[email protected]>
19
 */
20
class ExpressionBuilder implements ExpressionBuilderInterface
21
{
22
    /**
23
     * @var QueryBuilder
24
     */
25
    private $queryBuilder;
26
27
    /**
28
     * @param QueryBuilder $queryBuilder
29
     */
30
    public function __construct(QueryBuilder $queryBuilder)
31
    {
32
        $this->queryBuilder = $queryBuilder;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function andX(...$expressions)
39
    {
40
        return $this->queryBuilder->expr()->andX(...$expressions);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queryBuild...>andX(...$expressions); (Doctrine\ORM\Query\Expr\Andx) is incompatible with the return type declared by the interface Sylius\Component\Grid\Da...nBuilderInterface::andX of type Sylius\Component\Grid\Da...ressionBuilderInterface.

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...
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function orX(...$expressions)
47
    {
48
        return $this->queryBuilder->expr()->orX(...$expressions);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queryBuild...->orX(...$expressions); (Doctrine\ORM\Query\Expr\Orx) is incompatible with the return type declared by the interface Sylius\Component\Grid\Da...onBuilderInterface::orX of type Sylius\Component\Grid\Da...ressionBuilderInterface.

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...
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function comparison($field, $operator, $value)
55
    {
56
        throw new \BadMethodCallException('Not supported yet.');
57
        // TODO: Implement comparison() method.
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function equals($field, $value)
64
    {
65
        $parameterName = $this->getParameterName($field);
66
        $this->queryBuilder->setParameter($parameterName, $value);
67
68
        return $this->queryBuilder->expr()->eq($this->getFieldName($field), ':'.$parameterName);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queryBuild... ':' . $parameterName); (Doctrine\ORM\Query\Expr\Comparison) is incompatible with the return type declared by the interface Sylius\Component\Grid\Da...uilderInterface::equals of type Sylius\Component\Grid\Da...ressionBuilderInterface.

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...
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function notEquals($field, $value)
75
    {
76
        $parameterName = $this->getParameterName($field);
77
        $this->queryBuilder->setParameter($parameterName, $value);
78
79
        return $this->queryBuilder->expr()->neq($this->getFieldName($field), ':'.$parameterName);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queryBuild... ':' . $parameterName); (Doctrine\ORM\Query\Expr\Comparison) is incompatible with the return type declared by the interface Sylius\Component\Grid\Da...derInterface::notEquals of type Sylius\Component\Grid\Da...ressionBuilderInterface.

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...
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function lessThan($field, $value)
86
    {
87
        $parameterName = $this->getParameterName($field);
88
        $this->queryBuilder->setParameter($parameterName, $value);
89
90
        $this->queryBuilder->andWhere($this->getFieldName($field).' < :'.$parameterName);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function lessThanOrEqual($field, $value)
97
    {
98
        $parameterName = $this->getParameterName($field);
99
        $this->queryBuilder->setParameter($parameterName, $value);
100
101
        $this->queryBuilder->andWhere($this->getFieldName($field).' <= :'.$parameterName);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function greaterThan($field, $value)
108
    {
109
        $parameterName = $this->getParameterName($field);
110
        $this->queryBuilder->setParameter($parameterName, $value);
111
112
        $this->queryBuilder->andWhere($this->getFieldName($field).' > :'.$parameterName);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function greaterThanOrEqual($field, $value)
119
    {
120
        $parameterName = $this->getParameterName($field);
121
        $this->queryBuilder->setParameter($parameterName, $value);
122
123
        $this->queryBuilder->andWhere($this->getFieldName($field).' >= :'.$parameterName);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function in($field, array $values)
130
    {
131
        return $this->queryBuilder->expr()->in($this->getFieldName($field), $values);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queryBuild...Name($field), $values); (Doctrine\ORM\Query\Expr\Func) is incompatible with the return type declared by the interface Sylius\Component\Grid\Da...ionBuilderInterface::in of type Sylius\Component\Grid\Da...ressionBuilderInterface.

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...
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function notIn($field, array $values)
138
    {
139
        return $this->queryBuilder->expr()->notIn($this->getFieldName($field), $values);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queryBuild...Name($field), $values); (Doctrine\ORM\Query\Expr\Func) is incompatible with the return type declared by the interface Sylius\Component\Grid\Da...BuilderInterface::notIn of type Sylius\Component\Grid\Da...ressionBuilderInterface.

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...
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function isNull($field)
146
    {
147
        return $this->queryBuilder->expr()->isNull($this->getFieldName($field));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queryBuild...>getFieldName($field)); (string) is incompatible with the return type declared by the interface Sylius\Component\Grid\Da...uilderInterface::isNull of type Sylius\Component\Grid\Da...ressionBuilderInterface.

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...
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function isNotNull($field)
154
    {
155
        return $this->queryBuilder->expr()->isNotNull($this->getFieldName($field));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queryBuild...>getFieldName($field)); (string) is incompatible with the return type declared by the interface Sylius\Component\Grid\Da...derInterface::isNotNull of type Sylius\Component\Grid\Da...ressionBuilderInterface.

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...
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function like($field, $pattern)
162
    {
163
        return $this->queryBuilder->expr()->like($this->getFieldName($field), $this->queryBuilder->expr()->literal($pattern));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queryBuild...()->literal($pattern)); (Doctrine\ORM\Query\Expr\Comparison) is incompatible with the return type declared by the interface Sylius\Component\Grid\Da...nBuilderInterface::like of type Sylius\Component\Grid\Da...ressionBuilderInterface.

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...
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function notLike($field, $pattern)
170
    {
171
        return $this->queryBuilder->expr()->notLike($this->getFieldName($field), $this->queryBuilder->expr()->literal($pattern));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queryBuild...()->literal($pattern)); (Doctrine\ORM\Query\Expr\Comparison) is incompatible with the return type declared by the interface Sylius\Component\Grid\Da...ilderInterface::notLike of type Sylius\Component\Grid\Da...ressionBuilderInterface.

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...
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177
    public function orderBy($field, $direction)
178
    {
179
        return $this->queryBuilder->orderBy($this->getFieldName($field), $direction);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queryBuild...e($field), $direction); (Doctrine\ORM\QueryBuilder) is incompatible with the return type declared by the interface Sylius\Component\Grid\Da...ilderInterface::orderBy of type Sylius\Component\Grid\Da...ressionBuilderInterface.

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...
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function addOrderBy($field, $direction)
186
    {
187
        return $this->queryBuilder->addOrderBy($this->getFieldName($field), $direction);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->queryBuild...e($field), $direction); (Doctrine\ORM\QueryBuilder) is incompatible with the return type declared by the interface Sylius\Component\Grid\Da...erInterface::addOrderBy of type Sylius\Component\Grid\Da...ressionBuilderInterface.

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...
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    private function getFieldName($field)
194
    {
195
        if (false === strpos($field, '.')) {
196
            return $this->queryBuilder->getRootAlias().'.'.$field;
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ORM\QueryBuilder::getRootAlias() has been deprecated with message: Please use $qb->getRootAliases() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
197
        }
198
199
        return $field;
200
    }
201
202
    /**
203
     * @param string $field
204
     *
205
     * @return string
206
     */
207
    private function getParameterName($field)
208
    {
209
        $parameterName = str_replace('.', '_', $field);
210
211
        $i = 1;
212
        while ($this->hasParameterName($parameterName)) {
213
            $parameterName .= $i;
214
        }
215
216
        return $parameterName;
217
    }
218
219
    /**
220
     * @param string $parameterName
221
     *
222
     * @return bool
223
     */
224
    private function hasParameterName($parameterName)
225
    {
226
        return null !== $this->queryBuilder->getParameter($parameterName);
227
    }
228
}
229