QueryWorker   D
last analyzed

Complexity

Total Complexity 131

Size/Duplication

Total Lines 814
Duplicated Lines 4.18 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 34
loc 814
rs 4.4444
c 0
b 0
f 0
wmc 131
lcom 2
cbo 0

34 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getEntityName() 0 4 1
A getPrimaryKeyEntity() 0 4 1
A getQuery() 0 10 2
A getResult() 0 10 2
A getOneResult() 0 4 1
A toArray() 0 14 3
A getClassMetaData() 0 4 1
A count() 0 4 1
C withFilters() 0 42 13
A paginate() 0 12 3
B whereFieldJoin() 0 25 3
A andWhere() 11 11 2
A orWhere() 11 11 2
A manyToManyJoin() 0 11 2
A makeExpressions() 0 16 3
B addGroupBy() 0 19 5
A andHaving() 0 4 1
A orHaving() 0 4 1
A addOrderBy() 12 12 2
A fkAddOrderBy() 0 8 1
C select() 0 27 7
A getPathRepository() 0 4 1
A getMetaRepository() 0 6 2
C associationQueryFields() 0 75 20
B addQueryField() 0 18 5
C fkAssociation() 0 45 11
A fieldJoin() 0 17 3
A fkArrayAssociation() 0 14 3
A getFkMetaAlias() 0 11 2
A getSelectExpression() 0 10 3
A getFullFieldName() 0 4 1
C makeExpression() 0 70 21
A tableAlias() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like QueryWorker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use QueryWorker, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Bludata\Doctrine\ORM\Repositories;
4
5
use Doctrine\ORM\Query;
6
7
class QueryWorker
8
{
9
    const CUSTOM_FILTERS_KEY = 'custom';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
10
    const DEFAULT_TABLE_ALIAS = 't';
11
12
    /**
13
     * @var Doctrine\ORM\EntityRepository
14
     */
15
    protected $repository;
16
17
    /**
18
     * @var Doctrine\ORM\QueryBuilder
19
     */
20
    protected $queryBuilder;
21
22
    /**
23
     * @var Doctrine\ORM\Mapping\ClassMetadata
24
     */
25
    protected $classMetadata;
26
27
    /**
28
     * @var array
29
     */
30
    protected $entitys = [];
31
32
    /**
33
     * @var array
34
     */
35
    protected $queryFields = [];
36
37
    public function __construct($repository)
38
    {
39
        $this->repository = $repository;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
40
        $this->queryBuilder = $this->repository->createQueryBuilder(self::DEFAULT_TABLE_ALIAS);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 95 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
41
        $this->classMetadata = $this->repository->getClassMetadata();
42
    }
43
44
    /**
45
     * Retorna o nome da entity.
46
     *
47
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
48
     */
49
    public function getEntityName()
50
    {
51
        $this->repository->getEntityName();
52
    }
53
54
    /**
55
     * Retorna a chave primária da entity.
56
     *
57
     * @return string
58
     */
59
    public function getPrimaryKeyEntity()
60
    {
61
        return $this->getClassMetaData()->identifier[0];
62
    }
63
64
    /**
65
     * @return Doctrine\ORM\Query
66
     */
67
    public function getQuery()
68
    {
69
        $query = $this->queryBuilder->getQuery();
70
71
        if (method_exists($query, 'setHint')) {
72
            $query->setHint(Query::HINT_INCLUDE_META_COLUMNS, true);
73
        }
74
75
        return $query;
76
    }
77
78
    /**
79
     * Retorna um array com os objetos do resultado de $this->queryBuilder.
80
     *
81
     * @return array
82
     */
83
    public function getResult()
84
    {
85
        $query = $this->getQuery();
86
87
        if (method_exists($query, 'getResult')) {
88
            return $query->getResult();
89
        }
90
91
        return $query->execute();
92
    }
93
94
    /**
95
     * Retorna um objeto do resultado de $this->queryBuilder.
96
     *
97
     * @return Bludata\Doctrine\ORM\Entities\BaseEntity | null
98
     */
99
    public function getOneResult()
100
    {
101
        return $this->getQuery()->getOneOrNullResult();
102
    }
103
104
    /**
105
     * Converte os objetos de $this->getResult() em array.
106
     *
107
     * @return array
108
     */
109
    public function toArray(array $options = null)
110
    {
111
        $array = [];
112
113
        foreach ($this->getResult() as $item) {
114
            if (method_exists($item, 'toArray')) {
115
                array_push($array, $item->toArray($options));
116
            } else {
117
                array_push($array, $item);
118
            }
119
        }
120
121
        return $array;
122
    }
123
124
    /**
125
     * @return Doctrine\ORM\Mapping\ClassMetadata
126
     */
127
    public function getClassMetaData()
128
    {
129
        return $this->classMetadata;
130
    }
131
132
    /**
133
     * Retorna a quantidade de elementos em $this->getResult().
134
     *
135
     * @return int
136
     */
137
    public function count()
138
    {
139
        return count($this->getResult());
140
    }
141
142
    /**
143
     * Aplica filtros em $this->queryBuilder.
144
     *
145
     * @param array $filters
0 ignored issues
show
Documentation introduced by
Should the type for parameter $filters not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
146
     *
147
     * @return Bludata\Doctrine\ORM\Repositories\QueryWorker
0 ignored issues
show
Documentation introduced by
Should the return type not be QueryWorker?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
148
     */
149
    public function withFilters(array $filters = null)
150
    {
151
        if ($filters) {
152
            foreach ($filters as $filter) {
153
                switch ($filter['type']) {
154
                    case 'select':
155
                        $this->select($filter['fields']);
156
                        break;
157
                    case 'andWhere':
158
                        $this->andWhere($filter['field'], $filter['operation'], $filter['value']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 98 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
159
                        break;
160
                    case 'orWhere':
161
                        $this->orWhere($filter['field'], $filter['operation'], $filter['value']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 97 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
162
                        break;
163
                    case 'andHaving':
164
                        $this->andHaving($filter['field'], $filter['operation'], $filter['value']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 99 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
165
                        break;
166
                    case 'orHaving':
167
                        $this->orHaving($filter['field'], $filter['operation'], $filter['value']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 98 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
168
                        break;
169
                    case 'addGroupBy':
170
                        $this->addGroupBy($filter['field']);
171
                        break;
172
                    case 'addOrderBy':
173
                        $this->addOrderBy($filter['field'], $filter['order']);
174
                        break;
175
                    case 'fkAddOrderBy':
176
                        $this->fkAddOrderBy($filter['field'], $filter['fkField'], $filter['order']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 100 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
177
                        break;
178
                    case 'paginate':
179
                        if (isset($filter['page'])) {
180
                            $this->paginate($filter['limit'], $filter['page']);
181
                        } else {
182
                            $this->paginate($filter['limit']);
183
                        }
184
                        break;
185
                }
186
            }
187
        }
188
189
        return $this;
190
    }
191
192
    /**
193
     * Set the page with paginate attribute.
194
     *
195
     * @param int $page
196
     * @param int $limit
197
     *
198
     * @return $this
199
     */
200
    public function paginate($limit = 25, $page = 0)
201
    {
202
        if ($limit > 0) {
203
            $this->queryBuilder->setMaxResults($limit);
204
        }
205
206
        if ($page > 0) {
207
            $this->queryBuilder->setFirstResult($page * $limit);
208
        }
209
210
        return $this;
211
    }
212
213
    /**
214
     * Add  joins.
215
     *
216
     * @param string $field
217
     *
218
     * @return $array
0 ignored issues
show
Documentation introduced by
The doc-type $array could not be parsed: Unknown type name "$array" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
219
     */
220
    private function whereFieldJoin($field, $value = null, $operation = null)
221
    {
222
        $arr = explode('.', $field);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
223
        $tempField = end($arr);
224
        $alias = prev($arr);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
225
            // verifica se está solicitando um many to many
226
            $meta = $this->getClassMetaData();
227
        if (!empty($meta->associationMappings[$alias]['joinTable'])) {
228
            $table = $this->getFullFieldName($meta->associationMappings[$alias]['fieldName'], self::DEFAULT_TABLE_ALIAS);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
229
            $alias = $this->tableAlias();
230
            if (!$operation) {
231
                $operation = '=';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $operation. This often makes code more readable.
Loading history...
232
            }
233
            $condicao = $this->makeExpression($tempField, $operation, $value, $alias);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 86 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
234
            $this->queryBuilder->leftJoin($table, $alias, 'WITH', $condicao);
235
236
            return ['alias' => $alias, 'field' => $tempField];
237
        }
238
            //monta os joins
239
            $this->associationQueryFields($field);
240
            //monta os dados do where
241
            $field = $tempField;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $field. This often makes code more readable.
Loading history...
242
243
        return ['alias' => $alias, 'field' => $field];
244
    }
245
246
    /**
247
     * Add a "and where" filter.
248
     *
249
     * @param string $field
250
     * @param string $operation
251
     * @param string $value
0 ignored issues
show
Documentation introduced by
Should the type for parameter $value not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
252
     *
253
     * @return $this
254
     */
255 View Code Duplication
    public function andWhere($field, $operation, $value = null, $alias = self::DEFAULT_TABLE_ALIAS)
0 ignored issues
show
Duplication introduced by
This method 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...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 99 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
256
    {
257
        if (strpos($field, '.') > 0) {
258
            $newValues = $this->whereFieldJoin($field, $value, $operation);
259
            $alias = $newValues['alias'];
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $alias. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
260
            $field = $newValues['field'];
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $field. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
261
        }
262
        $this->queryBuilder->andWhere($this->makeExpression($field, $operation, $value, $alias));
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 255 can also be of type string; however, Bludata\Doctrine\ORM\Rep...orker::makeExpression() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 97 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
263
264
        return $this;
265
    }
266
267
    /**
268
     * Add a "or where" filter.
269
     *
270
     * @param string $field
271
     * @param string $operation
272
     * @param string $value
0 ignored issues
show
Documentation introduced by
Should the type for parameter $value not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
273
     *
274
     * @return $this
275
     */
276 View Code Duplication
    public function orWhere($field, $operation, $value = null, $alias = self::DEFAULT_TABLE_ALIAS)
0 ignored issues
show
Duplication introduced by
This method 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...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 98 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
277
    {
278
        if (strpos($field, '.') > 0) {
279
            $newValues = $this->whereFieldJoin($field, $value, $operation);
280
            $alias = $newValues['alias'];
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $alias. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
281
            $field = $newValues['field'];
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $field. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
282
        }
283
        $this->queryBuilder->orWhere($this->makeExpression($field, $operation, $value, $alias));
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 276 can also be of type string; however, Bludata\Doctrine\ORM\Rep...orker::makeExpression() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 96 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
284
285
        return $this;
286
    }
287
288
    /**
289
     * Add a join filter.
290
     *
291
     * @param array  $meta
292
     * @param string $alias
293
     *
294
     * @return $this
295
     */
296
    public function manyToManyJoin($meta, $alias, $defaultAlias = self::DEFAULT_TABLE_ALIAS)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 92 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
297
    {
298
        $table = $this->getFullFieldName($meta->associationMappings[$alias]['fieldName'], $defaultAlias);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 105 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
299
300
        if (!in_array($table, $this->entitys)) {
301
            $this->queryBuilder->join($table, $alias);
302
            $this->entitys[] = $table;
303
        }
304
305
        return $this;
306
    }
307
308
    /**
309
     * Create an array of expressions.
310
     *
311
     * @param array $conditions
312
     *
313
     * @return $this
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
314
     */
315
    private function makeExpressions($conditions, $alias = self::DEFAULT_TABLE_ALIAS)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 85 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
316
    {
317
        $expressions = [];
318
        foreach ($conditions as $attr) {
319
            $field = $attr['field'];
320
            if (strpos($field, '.') > 0) {
321
                $newValues = $this->whereFieldJoin($field, $attr['value'], $attr['operation']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 95 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
322
                $alias = $newValues['alias'];
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $alias. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
323
                $field = $newValues['field'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
324
            }
325
326
            $expressions[] = $this->makeExpression($field, $attr['operation'], $attr['value'], $alias);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 103 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
327
        }
328
329
        return $expressions;
330
    }
331
332
    /**
333
     * Add a "group by" key.
334
     *
335
     * @param string $field
336
     */
337
    public function addGroupBy($field)
338
    {
339
        $alias = self::DEFAULT_TABLE_ALIAS;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
340
        $newAliasField = $this->fieldJoin($this->getClassMetaData(), $field, $alias);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 85 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
341
        $alias = $newAliasField['alias'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
342
        $field = $newAliasField['field'];
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $field. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
343
        if (count($this->queryFields) > 0) {
344
            if (!in_array($this->getFullFieldName($field, $alias), $this->queryFields)) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 89 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
345
                $this->queryFields[] = $this->getFullFieldName($field, $alias);
346
            }
347
            foreach ($this->queryFields as $item) {
348
                if (strpos($item, '(') === false) {
349
                    $this->queryBuilder->addGroupBy($item);
350
                }
351
            }
352
        }
353
354
        return $this;
355
    }
356
357
    /**
358
     * Add a "and having" filter.
359
     *
360
     * @param string $field
361
     * @param string $operation
362
     * @param string $value
0 ignored issues
show
Documentation introduced by
Should the type for parameter $value not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
363
     *
364
     * @return $this
365
     */
366
    public function andHaving($field, $operation, $value = null)
0 ignored issues
show
Unused Code introduced by
The parameter $field 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 $operation 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 $value 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...
367
    {
368
        throw new \Exception('Not implemented');
369
    }
370
371
    /**
372
     * Add a "or having" filter.
373
     *
374
     * @param string $field
375
     * @param string $order
0 ignored issues
show
Bug introduced by
There is no parameter named $order. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
376
     */
377
    public function orHaving($field, $operation, $value = null)
0 ignored issues
show
Unused Code introduced by
The parameter $field 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 $operation 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 $value 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...
378
    {
379
        throw new \Exception('Not implemented');
380
    }
381
382
    /**
383
     * Add a "order by" filter.
384
     *
385
     * @param string $field
386
     * @param string $order
387
     */
388 View Code Duplication
    public function addOrderBy($field, $order = 'ASC')
0 ignored issues
show
Duplication introduced by
This method 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...
389
    {
390
        $alias = self::DEFAULT_TABLE_ALIAS;
391
        if (strpos($field, '.') > 0) {
392
            $newValues = $this->whereFieldJoin($field);
393
            $alias = $newValues['alias'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
394
            $field = $newValues['field'];
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $field. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
395
        }
396
        $this->queryBuilder->addOrderBy($this->getFullFieldName($field, $alias), $order);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 89 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
397
398
        return $this;
399
    }
400
401
    /**
402
     * Add a "order by" filter.
403
     *
404
     * @param string $field
405
     * @param string $order
406
     */
407
    public function fkAddOrderBy($field, $fkField, $order = 'ASC')
408
    {
409
        $alias = $this->tableAlias();
410
        $this->queryBuilder->join($this->getFullFieldName($field), $alias);
411
        $this->queryBuilder->addOrderBy($this->getFullFieldName($fkField, $alias), $order);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 91 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
412
413
        return $this;
414
    }
415
416
    /**
417
     * Add a select statement.
418
     *
419
     * @param associationField.fkField
420
     * @param $field
421
     */
422
    public function select($fields)
423
    {
424
        foreach ($fields as $key => $value) {
425
            if (is_int($key)) {
426
                $valor = $value;
427
                if (is_array($value)) {
428
                    if (!empty($value['expression'])) {
429
                        //é uma expressão
430
                        $expression = ['expression' => $value['expression'], 'alias' => $value['alias']];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 105 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
431
                        $valor = $value['field'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
432
                        $this->associationQueryFields($valor, $expression);
0 ignored issues
show
Documentation introduced by
$expression is of type array<string,?,{"expression":"?","alias":"?"}>, but the function expects a integer.

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...
433
                    }
434
                } else {
435
                    $this->associationQueryFields($valor);
436
                }
437
            } elseif (is_array($value)) {
438
                $alias = $this->tableAlias();
439
                $this->queryBuilder->join($this->getFullFieldName($key, self::DEFAULT_TABLE_ALIAS), $alias);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 108 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
440
                foreach ($value as $valueField) {
441
                    $this->queryFields[] = $this->getFullFieldName($valueField, $alias);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 88 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
442
                }
443
            }
444
        }
445
        $this->queryBuilder->select(implode(',', $this->queryFields));
446
447
        return $this;
448
    }
449
450
    /**
451
     * get the repository.
452
     *
453
     * @param associationField.fkField
454
     */
455
    private function getPathRepository($newEntity)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
456
    {
457
        return app()->getRepositoryInterface($newEntity);
458
    }
459
460
    /**
461
     * get the class metadata.
462
     *
463
     * @param associationField.fkField
464
     */
465
    private function getMetaRepository($entity)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
466
    {
467
        $repository = $this->getPathRepository($entity);
468
469
        return $repository ? $repository->getClassMetaData() : [];
470
    }
471
472
    /**
473
     * Add association join and select fields.
474
     *
475
     * @param associationField.fkField
476
     * @param $field
477
     */
478
    public function associationQueryFields($value, $expression = 0)
479
    {
480
        $pos = strpos($value, '.');
481
        if ($pos > 0) {
482
            $fk = substr($value, 0, $pos);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
483
            $field = substr($value, ($pos + 1));
484
            $alias = $fk;
485
            if (substr_count($value, '.') > 1) {
486
                // tem mais de um join para chegar ao campo
487
                $count = 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
488
                $arr = explode('.', $value);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
489
                $field = end($arr);
0 ignored issues
show
Unused Code introduced by
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
490
                $fkTemp = $fk;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
491
                $arrLength = count($arr);
492
                foreach ($arr as $key => $entity) {
493
                    if ($count == 0) {
494
                        $campo = $this->fkAssociation($this->getClassMetaData(), $entity, $arr[$count + 1], $entity, self::DEFAULT_TABLE_ALIAS);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 144 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
495
                        if ($campo && !in_array($campo, $this->queryFields)) {
496
                            if ($expression == 0) {
497
                                $this->queryFields[] = $campo;
498
                            }
499
                        }
500
                    } elseif (($count + 1) < $arrLength) {
501
                        if ($this->getPathRepository(ucfirst($fkTemp))) {
502
                            $meta = $this->getMetaRepository(ucfirst($fkTemp));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
503
                            $campo = $this->fkAssociation($meta, $entity, $arr[$count + 1], $entity, $fkTemp);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 110 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
504
                        } else {
505
                            //busca a entidade correta
506
                            if ($count > 1) {
507
                                $metaAnterior = null;
508
                                $fkAnterior = ucfirst($arr[$count - 2]);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
509
                                //verifica se a entidade anterior era um oneToMany
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 82 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
510
                                if (count($this->getPathRepository($fkAnterior)) == 0) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 88 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
511
                                    //verifica se havia uma entidade antes
512
                                    if (!empty($arr[$count - 3])) {
513
                                        $fkAnterior = ucfirst($arr[$count - 3]);
514
                                    } else {
515
                                        // pega a entidade default
516
                                        $fkAnterior = self::DEFAULT_TABLE_ALIAS;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
517
                                        $metaAnterior = $meta;
0 ignored issues
show
Bug introduced by
The variable $meta does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
518
                                    }
519
                                }
520
                                $metaAlias = $this->getFkMetaAlias($fkAnterior, $fkTemp, $metaAnterior);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 104 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
521
                            } else {
522
                                $metaAlias = $this->getFkMetaAlias(self::DEFAULT_TABLE_ALIAS, $fkTemp, $this->getClassMetaData());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
523
                            }
524
                            $fkTemp = $metaAlias['alias'];
525
                            $meta = $this->getMetaRepository(ucfirst($fkTemp));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
526
                            $campo = $this->fkAssociation($meta, $entity, $arr[$count + 1], $entity, $fkTemp);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 110 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
527
                        }
528
                        if ($campo && ($expression == 0 || ($expression != 0 && $count == ($arrLength - 1)))) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 111 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
529
                            $this->addQueryField($campo, $expression);
530
                        }
531
                    }
532
                    $fkTemp = $entity;
533
                    ++$count;
534
                }
535
            } else {
536
                if (empty($this->getClassMetaData()->associationMappings[$fk]) && count($this->getClassMetaData()->subClasses) > 0) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
537
                    //ignorado não é possível criar o join, provavelmente está no pai chamando o filho.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 103 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
538
                    return $this;
539
                }
540
                // realiza o join para retornar o valor do campo
541
                $campo = $this->fkAssociation($this->getClassMetaData(), $fk, $field, $alias, self::DEFAULT_TABLE_ALIAS);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
542
                $this->addQueryField($campo, $expression);
543
            }
544
        } else {
545
            $valor = $this->getFullFieldName($value);
546
            if (!empty($this->getClassMetaData()->associationMappings[$value])) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 81 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
547
                // é uma FK, retorna o ID
548
                $valor = 'IDENTITY('.$valor.') '.$value;
549
            }
550
            $this->addQueryField($valor, $expression);
551
        }
552
    }
553
554
    /**
555
     * Add a field or expression in the select array.
556
     *
557
     * @param string field
558
     * @param mix expression
559
     */
560
    private function addQueryField($campo, $expression = 0)
561
    {
562
        if ($campo && !in_array($campo, $this->queryFields)) {
563
            if ($expression == 0) {
564
                $this->queryFields[] = $campo;
565
            } else {
566
                //verifica se pode adicionar a expressão
567
                if (strpos($campo, ')') === false) {
568
                    $this->getSelectExpression($expression['expression'], $campo, $expression['alias']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 104 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
569
                } else {
570
                    $parts = explode(')', $campo);
571
                    //remove o alias
572
                    array_pop($parts);
573
                    $this->getSelectExpression($expression['expression'], implode(')', $parts), $expression['alias']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 118 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
574
                }
575
            }
576
        }
577
    }
578
579
    /**
580
     * Add a join statement.
581
     *
582
     * @todo  ignorar registros com deletedAt == true
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
583
     * @todo  validar a associação a partir do aluno: processos.ordensServico.servicoOrdemServico.itensServicoOrdemServico.itemServico
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 134 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
584
     *
585
     * @param $meta - getClassMetaData
586
     * @param $fk
587
     * @param $field
588
     * @param $alias
589
     * @param $defaultAlias
590
     */
591
    public function fkAssociation($meta, $fk, $field, $alias, $defaultAlias)
592
    {
593
        if ($association = $meta->associationMappings[$fk]) {
594
            if (!in_array($association['targetEntity'], $this->entitys)) {
595
                if (!empty($association['joinColumns'])) {
596
                    $condition = $this->getFullFieldName($association['fieldName'], $defaultAlias).' = '.$this->getFullFieldName($association['joinColumns'][0]['referencedColumnName'], $alias);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 193 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
597
                    $this->queryBuilder->leftJoin($association['targetEntity'], $alias, 'WITH', $condition);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 108 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
598
                    $this->queryBuilder->andWhere($condition);
599
                    $this->entitys[] = $association['targetEntity'];
600
                } else {
601
                    //está buscando de um arrayCollection
602
                    $repository = explode('\\', $association['targetEntity']);
603
                    if (!$association['mappedBy']) {
604
                        if (!empty($association['joinTable'])) {
605
                            $this->manyToManyJoin($meta, $fk, $defaultAlias);
606
607
                            return $this->getFullFieldName($field, $fk);
608
                        }
609
610
                        return;
611
                    }
612
                    $meta = $this->getMetaRepository(end($repository));
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $meta. This often makes code more readable.
Loading history...
613
614
                    return $this->fkArrayAssociation($meta, $association['mappedBy'], $field, lcfirst(end($repository)), $defaultAlias, $association['targetEntity']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 166 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
615
                }
616
            }
617
618
            //verifica se o campo existe
619
            if ($this->getPathRepository(ucfirst($fk))) {
620
                $meta = $this->getMetaRepository(ucfirst($fk));
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $meta. This often makes code more readable.
Loading history...
621
            } else {
622
                //busca a entidade correta
623
                $metaAlias = $this->getFkMetaAlias($defaultAlias, $fk, $meta);
624
                $meta = $metaAlias['meta'];
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $meta. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
625
                $alias = $metaAlias['alias'];
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $alias. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
626
            }
627
            if (empty($meta->associationMappings[$field]) && empty($meta->fieldMappings[$field])) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 99 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
628
                return;
629
            } elseif (!empty($meta->associationMappings[$field]) && empty($meta->associationMappings[$field]['joinColumns'])) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
630
                return;
631
            }
632
            //retorna o campo
633
            return '('.$this->getFullFieldName($field, $alias).') AS '.$this->getFullFieldName($field, $alias, '_');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 116 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
634
        }
635
    }
636
637
    /**
638
     * Add joins and return the new field and alias.
639
     *
640
     * @param $meta - getClassMetaData
641
     * @param $field
642
     * @param $alias
643
     *
644
     * @return array
645
     */
646
    public function fieldJoin($meta, $field, $alias)
647
    {
648
        if (strpos($field, '.') > 0) {
649
            $arr = explode('.', $field);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
650
            $fieldTemp = end($arr);
651
            $alias = prev($arr);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $alias. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
652
            if (!empty($meta->associationMappings[$alias]['joinTable'])) {
653
                $this->manyToManyJoin($meta, $alias);
654
            } else {
655
                //monta os joins
656
                $this->associationQueryFields($field);
657
            }
658
            $field = $fieldTemp;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $field. This often makes code more readable.
Loading history...
659
        }
660
661
        return ['alias' => $alias, 'field' => $field];
662
    }
663
664
    /**
665
     * @TODO GROUP_CONCAT()
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
666
     * Add a join statement from array collection
667
     *
668
     * @param $meta - getClassMetaData
669
     * @param $fk
670
     * @param $field
671
     * @param $alias
672
     * @param $defaultAlias
673
     * @param $targetEntity
674
     */
675
    public function fkArrayAssociation($meta, $fk, $field, $alias, $defaultAlias, $targetEntity)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 96 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
676
    {
677
        if ($association = $meta->associationMappings[$fk]) {
678
            if (!in_array($targetEntity, $this->entitys)) {
679
                $condition = $this->getFullFieldName($association['fieldName'], $alias).' = '.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 94 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
680
                $this->getFullFieldName($association['joinColumns'][0]['referencedColumnName'], $defaultAlias);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 111 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
681
                $this->queryBuilder->join($targetEntity, $alias, 'WITH', $condition);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 85 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
682
                $this->queryBuilder->andWhere($condition);
683
                $this->entitys[] = $targetEntity;
684
            }
685
686
            return '('.$this->getFullFieldName($field, $alias).') AS '.$this->getFullFieldName($field, $alias, '_');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 116 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
687
        }
688
    }
689
690
    /**
691
     * @param string $alias
692
     * @param string $fk
693
     * @param array  $meta
0 ignored issues
show
Documentation introduced by
Should the type for parameter $meta not be array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
694
     *
695
     * @return array
696
     */
697
    public function getFkMetaAlias($alias, $fk, $meta = null)
698
    {
699
        if (self::DEFAULT_TABLE_ALIAS != $alias) {
700
            $meta = $this->getMetaRepository(ucfirst($alias));
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $meta. This often makes code more readable.
Loading history...
701
        }
702
        $repository = explode('\\', $meta->associationMappings[$fk]['targetEntity']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 85 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
703
        $meta = $this->getMetaRepository(end($repository));
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $meta. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
704
        $alias = lcfirst(end($repository));
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $alias. This often makes code more readable.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
705
706
        return ['meta' => $meta, 'alias' => $alias];
707
    }
708
709
    /**
710
     * @param mixed  $field
711
     * @param string $expression
712
     * @param string $alias
713
     */
714
    private function getSelectExpression($expression, $field, $alias, $fieldAlias = self::DEFAULT_TABLE_ALIAS)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 110 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
715
    {
716
        $validExpressions = ['SUM', 'MIN', 'MAX', 'AVG', 'COUNT'];
717
        if (in_array(trim(strtoupper($expression)), $validExpressions)) {
718
            if (strpos($field, '.') === false) {
719
                $field = getFullFieldName($field, $fieldAlias);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $field. This often makes code more readable.
Loading history...
720
            }
721
            $this->queryFields[] = sprintf('%s(%s) AS %s', $expression, $field, $alias);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 88 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
722
        }
723
    }
724
725
    /**
726
     * @param $field
727
     * @param string $alias
728
     *
729
     * @return string
730
     */
731
    protected function getFullFieldName($field, $alias = self::DEFAULT_TABLE_ALIAS, $separator = '.')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 101 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
732
    {
733
        return sprintf('%s%s%s', $alias, $separator, $field);
734
    }
735
736
    /**
737
     * @param $field
738
     * @param $operation
739
     * @param null   $value
740
     * @param string $alias
741
     */
742
    protected function makeExpression($field, $operation, $value = null, $alias = self::DEFAULT_TABLE_ALIAS)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 108 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
743
    {
744
        if (!is_array($value)) {
745
            $value = $this->queryBuilder->expr()->literal($value);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $value. This often makes code more readable.
Loading history...
746
        }
747
        if ($field) {
748
            $field = $this->getFullFieldName($field, $alias);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $field. This often makes code more readable.
Loading history...
749
        }
750
        $expression = null;
751
        switch (strtolower($operation)) {
752
            case '>':
753
                $expression = $this->queryBuilder->expr()->gt($field, $value);
754
                break;
755
            case '=':
756
                $expression = $this->queryBuilder->expr()->eq($field, $value);
757
                break;
758
            case '<':
759
                $expression = $this->queryBuilder->expr()->lt($field, $value);
760
                break;
761
            case '>=':
762
                $expression = $this->queryBuilder->expr()->gte($field, $value);
763
                break;
764
            case '<=':
765
                $expression = $this->queryBuilder->expr()->lte($field, $value);
766
                break;
767
            case '<>':
768
                $expression = $this->queryBuilder->expr()->neq($field, $value);
769
                break;
770
            case 'isnull':
771
                $expression = $this->queryBuilder->expr()->isNull($field);
772
                break;
773
            case 'isnotnull':
774
                $expression = $this->queryBuilder->expr()->isNotNull($field);
775
                break;
776
            case 'in':
777
                $expression = $this->queryBuilder->expr()->in($field, $value);
778
                break;
779
            case 'orx':
780
                $expression = $this->queryBuilder->expr()->orX()->addMultiple($this->makeExpressions($value, $alias));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 118 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
781
                break;
782
            case 'andx':
783
                $expression = $this->queryBuilder->expr()->andX()->addMultiple($this->makeExpressions($value, $alias));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 119 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
784
                break;
785
            case 'notin':
786
                $expression = $this->queryBuilder->expr()->notIn($field, $value);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 81 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
787
                break;
788
            case 'contains':
789
                /*
790
                 * @todo implementar o metodo contains
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
791
                 */
792
                // $expression = $this->queryBuilder->expr()->contains($field, $value);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 87 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
793
                break;
794
            case 'like':
795
                $expression = $this->queryBuilder->expr()->like('LOWER('.$field.')', strtolower($value));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 105 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
796
                break;
797
            case 'notlike':
798
                $expression = $this->queryBuilder->expr()->notLike($field, $value);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 83 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
799
                break;
800
            case 'isinstanceof':
801
                $expression = $alias.' INSTANCE OF '.$value;
802
                break;
803
            case 'between':
804
                $expression = $this->queryBuilder->expr()->between($field, $this->queryBuilder->expr()->literal($value[0]), $this->queryBuilder->expr()->literal($value[1]));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 173 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
805
                break;
806
            case 'dateparteq':
807
                $expression = $this->queryBuilder->expr()->eq("DATEPART('".$value['format']."', ".$field.')', $value['value']);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
808
        }
809
810
        return $expression;
811
    }
812
813
    /**
814
     * @return string
815
     */
816
    protected function tableAlias()
817
    {
818
        return self::DEFAULT_TABLE_ALIAS.count($this->queryBuilder->getAllAliases());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 85 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
819
    }
820
}
821