getQueryBuilder()   F
last analyzed

Complexity

Conditions 23
Paths 9

Size

Total Lines 115

Duplication

Lines 12
Ratio 10.43 %

Importance

Changes 0
Metric Value
dl 12
loc 115
rs 3.3333
c 0
b 0
f 0
cc 23
nc 9
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\AdminList;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Query\QueryBuilder;
7
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractDoctrineDBALAdminListConfigurator;
8
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\EnumerationFilterType;
9
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\StringFilterType;
10
use Kunstmaan\TranslatorBundle\Entity\Translation;
11
12
/**
13
 * TranslationAdminListConfigurator
14
 */
15
class TranslationAdminListConfigurator extends AbstractDoctrineDBALAdminListConfigurator
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $locales;
21
22
    /**
23
     * @var string
24
     */
25
    protected $locale;
26
27
    /**
28
     * @param Connection $connection
29
     * @param array      $locales
30
     */
31
    public function __construct(Connection $connection, array $locales)
32
    {
33
        parent::__construct($connection);
34
        $this->locales = $locales;
35
        $this->setCountField('CONCAT(b.translation_id)');
36
    }
37
38
    /**
39
     * Configure filters
40
     */
41
    public function buildFilters()
42
    {
43
        $this->addFilter('status', new StringFilterType('status'), 'kuma_translator.adminlist.filter.status');
44
        $this->addFilter('domain', new StringFilterType('domain'), 'kuma_translator.adminlist.filter.domain');
45
        $this->addFilter('keyword', new StringFilterType('keyword'), 'kuma_translator.adminlist.filter.keyword');
46
        $this->addFilter('text', new StringFilterType('text'), 'kuma_translator.adminlist.filter.text');
47
        $this->addFilter('locale', new EnumerationFilterType('locale'), 'kuma_translator.adminlist.filter.locale', array_combine(
48
            $this->locales, $this->locales
49
        ));
50
    }
51
52
    /**
53
     * Configure the visible columns
54
     */
55
    public function buildFields()
56
    {
57
        $this->addField('domain', 'kuma_translator.adminlist.header.domain', true);
0 ignored issues
show
Documentation introduced by Krisztián Ferenczi
true is of type boolean, but the function expects a string.

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...
58
        $this->addField('keyword', 'kuma_translator.adminlist.header.keyword', true);
0 ignored issues
show
Documentation introduced by Krisztián Ferenczi
true is of type boolean, but the function expects a string.

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...
59
        $this->addField('status', 'kuma_translator.adminlist.header.status', true);
0 ignored issues
show
Documentation introduced by Kevin Jossart
true is of type boolean, but the function expects a string.

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...
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function canAdd()
66
    {
67
        return true;
68
    }
69
70
    /**
71
     * @param object|array $item
72
     *
73
     * @return bool
74
     */
75
    public function canEdit($item)
76
    {
77
        return false;
78
    }
79
80
    /**
81
     * @param object|array $item
82
     *
83
     * @return bool
84
     */
85
    public function canEditInline($item)
86
    {
87
        return true;
88
    }
89
90
    /**
91
     * Override path convention (because settings is a virtual admin subtree)
92
     *
93
     * @param string $suffix
0 ignored issues
show
Documentation introduced by Wim Vandersmissen
Should the type for parameter $suffix 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...
94
     *
95
     * @return string
96
     */
97 View Code Duplication
    public function getPathByConvention($suffix = null)
0 ignored issues
show
Duplication introduced by Wim Vandersmissen
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...
98
    {
99
        if (empty($suffix)) {
100
            return sprintf('%s_settings_%ss', $this->getBundleName(), strtolower($this->getEntityName()));
101
        }
102
103
        return sprintf('%s_settings_%ss_%s', $this->getBundleName(), strtolower($this->getEntityName()), $suffix);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getAdminType($item)
110
    {
111
        return null;
112
    }
113
114
    public function getBundleName()
115
    {
116
        return 'KunstmaanTranslatorBundle';
117
    }
118
119
    public function getEntityName()
120
    {
121
        return 'Translation';
122
    }
123
124
    public function getControllerPath()
125
    {
126
        return 'KunstmaanTranslatorBundle:Index';
127
    }
128
129
    /**
130
     * @return QueryBuilder|null
131
     */
132
    public function getQueryBuilder()
133
    {
134
        if (is_null($this->queryBuilder)) {
135
            $this->queryBuilder = new QueryBuilder($this->connection);
136
            $this->queryBuilder
137
                ->select('DISTINCT b.translation_id AS id, b.keyword, b.domain, b.status')
138
                ->from('kuma_translation', 'b')
139
                ->andWhere('b.status != :statusstring')
140
                ->setParameter('statusstring', Translation::STATUS_DISABLED);
141
142
            // Apply filters
143
            $filters = $this->getFilterBuilder()->getCurrentFilters();
144
            $locales = array();
145
146
            $textValue = $textComparator = null;
147
            foreach ($filters as $filter) {
148
                if ($filter->getType() instanceof EnumerationFilterType && $filter->getColumnName() == 'locale') {
149
                    // Override default enumeration filter handling ... catch selected locales here
150
                    $data = $filter->getData();
151
                    $comparator = $filter->getType()->getComparator();
152
                    if ($comparator == 'in') {
153
                        $locales = $data['value'];
154
                    } elseif ($comparator == 'notin') {
155
                        $locales = array_diff($this->locales, $data['value']);
156
                    }
157
                } elseif ($filter->getType() instanceof StringFilterType && $filter->getColumnName() == 'text') {
158
                    // Override default text filter handling ...
159
                    $data = $filter->getData();
160
                    $textValue = $data['value'];
161
                    $textComparator = $data['comparator'];
162
                } else {
163
                    /* @var AbstractDBALFilterType $type */
164
                    $type = $filter->getType();
165
                    $type->setQueryBuilder($this->queryBuilder);
166
                    $filter->apply();
167
                }
168
            }
169
170
            if (!empty($locales)) {
171
                $this->locales = $locales;
172
            }
173
            $this->locales = array_unique($this->locales);
174
175
            // Field building hack...
176
            foreach ($this->locales as $locale) {
177
                $this->addField($locale, strtoupper($locale), false, 'KunstmaanTranslatorBundle:Translator:inline_edit.html.twig');
0 ignored issues
show
Documentation introduced by Wim Vandersmissen
false is of type boolean, but the function expects a string.

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...
178
            }
179
180
            // Field filter hack...
181
            $this->addFilter('locale', new EnumerationFilterType('locale'), 'kuma_translator.adminlist.filter.locale', array_combine(
182
                $this->locales, $this->locales
183
            ));
184
185
            // Add join for every locale
186
            foreach ($this->locales as $locale) {
187
                $this->queryBuilder->addSelect('t_' . $locale . '.`text` AS ' . $locale);
188
                $this->queryBuilder->addSelect('t_' . $locale . '.id AS ' . $locale . '_id');
189
                $this->queryBuilder->leftJoin('b', 'kuma_translation', 't_' . $locale,
190
                  'b.keyword = t_' . $locale . '.keyword and b.domain = t_' . $locale . '.domain and t_' . $locale . '.locale=:locale_' . $locale);
191
                $this->queryBuilder->setParameter('locale_' . $locale, $locale);
192
            }
193
194
            // Apply text filter
195
            if (!is_null($textValue) && !is_null($textComparator)) {
196
                $orX = $this->queryBuilder->expr()->orX();
197
198
                foreach ($this->locales as $key => $locale) {
199
                    $uniqueId = 'txt_' . $key;
200
                    switch ($textComparator) {
201
                        case 'equals':
202
                            $expr = $this->queryBuilder->expr()->eq('t_' . $locale . '.`text`', ':var_' . $uniqueId);
203
                            $this->queryBuilder->setParameter('var_' . $uniqueId, $textValue);
204
205
                            break;
206
                        case 'notequals':
207
                            $expr = $this->queryBuilder->expr()->neq('t_' . $locale . '.`text`', ':var_' . $uniqueId);
208
                            $this->queryBuilder->setParameter('var_' . $uniqueId, $textValue);
209
210
                            break;
211 View Code Duplication
                        case 'contains':
0 ignored issues
show
Duplication introduced by Kristof Jochmans
This code seems to be duplicated across 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...
212
                            $expr = $this->queryBuilder->expr()->like('t_' . $locale . '.`text`', ':var_' . $uniqueId);
213
                            $this->queryBuilder->setParameter('var_' . $uniqueId, '%' . $textValue . '%');
214
215
                            break;
216 View Code Duplication
                        case 'doesnotcontain':
0 ignored issues
show
Duplication introduced by Kristof Jochmans
This code seems to be duplicated across 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...
217
                            $expr = 't_' . $locale . '.`text`' . ' NOT LIKE :var_' . $uniqueId;
218
                            $this->queryBuilder->setParameter('var_' . $uniqueId, '%' . $textValue . '%');
219
220
                            break;
221
                        case 'startswith':
222
                            $expr = $this->queryBuilder->expr()->like('t_' . $locale . '.`text`', ':var_' . $uniqueId);
223
                            $this->queryBuilder->setParameter('var_' . $uniqueId, $textValue . '%');
224
225
                            break;
226
                        case 'endswith':
227
                            $expr = $this->queryBuilder->expr()->like('t_' . $locale . '.`text`', ':var_' . $uniqueId);
228
                            $this->queryBuilder->setParameter('var_' . $uniqueId, '%' . $textValue);
229
230
                            break;
231
                    }
232
                    $orX->add($expr);
0 ignored issues
show
Bug introduced by Kristof Jochmans
The variable $expr 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...
233
                }
234
235
                $this->queryBuilder->andWhere($orX);
236
            }
237
238
            // Apply sorting
239 View Code Duplication
            if (!empty($this->orderBy)) {
0 ignored issues
show
Duplication introduced by Wim Vandersmissen
This code seems to be duplicated across 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...
240
                $orderBy = $this->orderBy;
241
                $this->queryBuilder->orderBy($orderBy, ($this->orderDirection == 'DESC' ? 'DESC' : 'ASC'));
242
            }
243
        }
244
245
        return $this->queryBuilder;
246
    }
247
}
248