getQueryBuilder()   F
last analyzed

Complexity

Conditions 24
Paths 9

Size

Total Lines 128

Duplication

Lines 12
Ratio 9.38 %

Code Coverage

Tests 0
CRAP Score 600

Importance

Changes 0
Metric Value
dl 12
loc 128
ccs 0
cts 101
cp 0
rs 3.3333
c 0
b 0
f 0
cc 24
nc 9
nop 0
crap 600

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,
49
            $this->locales
50
        ));
51
    }
52
53
    /**
54
     * Configure the visible columns
55
     */
56
    public function buildFields()
57
    {
58
        $this->addField('domain', 'kuma_translator.adminlist.header.domain', true);
59
        $this->addField('keyword', 'kuma_translator.adminlist.header.keyword', true);
60
        $this->addField('status', 'kuma_translator.adminlist.header.status', true);
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    public function canAdd()
67
    {
68
        return true;
69
    }
70
71
    /**
72
     * @param object|array $item
73
     *
74
     * @return bool
75
     */
76
    public function canEdit($item)
77
    {
78
        return false;
79
    }
80
81
    /**
82
     * @param object|array $item
83
     *
84
     * @return bool
85
     */
86
    public function canEditInline($item)
87
    {
88
        return true;
89
    }
90
91
    /**
92
     * Override path convention (because settings is a virtual admin subtree)
93
     *
94
     * @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...
95
     *
96
     * @return string
97
     */
98 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...
99
    {
100
        if (empty($suffix)) {
101
            return sprintf('%s_settings_%ss', $this->getBundleName(), strtolower($this->getEntityName()));
102
        }
103
104
        return sprintf('%s_settings_%ss_%s', $this->getBundleName(), strtolower($this->getEntityName()), $suffix);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getAdminType($item)
111
    {
112
        return null;
113
    }
114
115
    public function getBundleName()
116
    {
117
        return 'KunstmaanTranslatorBundle';
118
    }
119
120
    public function getEntityName()
121
    {
122
        return 'Translation';
123
    }
124
125
    public function getControllerPath()
126
    {
127
        return 'KunstmaanTranslatorBundle:Index';
128
    }
129
130
    /**
131
     * @return QueryBuilder|null
132
     */
133
    public function getQueryBuilder()
134
    {
135
        if (is_null($this->queryBuilder)) {
136
            $this->queryBuilder = new QueryBuilder($this->connection);
137
            $this->queryBuilder
138
                ->select('DISTINCT b.translation_id AS id, b.keyword, b.domain, b.status')
139
                ->from('kuma_translation', 'b')
140
                ->andWhere('b.status != :statusstring')
141
                ->setParameter('statusstring', Translation::STATUS_DISABLED);
142
143
            // Apply filters
144
            $filters = $this->getFilterBuilder()->getCurrentFilters();
145
            $locales = [];
146
147
            $textValue = $textComparator = null;
148
            foreach ($filters as $filter) {
149
                if ($filter->getType() instanceof EnumerationFilterType && $filter->getColumnName() == 'locale') {
150
                    // Override default enumeration filter handling ... catch selected locales here
151
                    $data = $filter->getData();
152
                    $comparator = $filter->getType()->getComparator();
153
                    if ($comparator == 'in') {
154
                        $locales = $data['value'];
155
                    } elseif ($comparator == 'notin') {
156
                        $locales = array_diff($this->locales, $data['value']);
157
                    }
158
                } elseif ($filter->getType() instanceof StringFilterType && $filter->getColumnName() == 'text') {
159
                    // Override default text filter handling ...
160
                    $data = $filter->getData();
161
                    $textValue = $data['value'];
162
                    $textComparator = $data['comparator'];
163
                } else {
164
                    /* @var AbstractDBALFilterType $type */
165
                    $type = $filter->getType();
166
                    $type->setQueryBuilder($this->queryBuilder);
167
                    $filter->apply();
168
                }
169
            }
170
171
            if (!empty($locales)) {
172
                $this->locales = $locales;
173
            }
174
            $this->locales = array_unique($this->locales);
175
176
            // Field building hack...
177
            foreach ($this->locales as $locale) {
178
                $this->addField($locale, strtoupper($locale), false, 'KunstmaanTranslatorBundle:Translator:inline_edit.html.twig');
179
            }
180
181
            // Field filter hack...
182
            $this->addFilter('locale', new EnumerationFilterType('locale'), 'kuma_translator.adminlist.filter.locale', array_combine(
183
                $this->locales,
184
                $this->locales
185
            ));
186
187
            // Add join for every locale
188
            foreach ($this->locales as $locale) {
189
                $this->queryBuilder->addSelect('t_'.$locale.'.`text` AS '.$locale);
190
                $this->queryBuilder->addSelect('t_'.$locale.'.id AS '.$locale.'_id');
191
                $this->queryBuilder->leftJoin(
192
                    'b',
193
                    'kuma_translation',
194
                    't_'.$locale,
195
                    'b.keyword = t_'.$locale.'.keyword and b.domain = t_'.$locale.'.domain and t_'.$locale.'.locale=:locale_'.$locale
196
                );
197
                $this->queryBuilder->setParameter('locale_'.$locale, $locale);
198
            }
199
200
            // Apply text filter
201
            if (!is_null($textValue) && !is_null($textComparator)) {
202
                $orX = $this->queryBuilder->expr()->orX();
203
204
                foreach ($this->locales as $key => $locale) {
205
                    $uniqueId = 'txt_'.$key;
206
                    switch ($textComparator) {
207
                        case 'equals':
208
                            $expr = $this->queryBuilder->expr()->eq('t_'.$locale.'.`text`', ':var_'.$uniqueId);
209
                            $this->queryBuilder->setParameter('var_'.$uniqueId, $textValue);
210
211
                            break;
212
                        case 'notequals':
213
                            $expr = $this->queryBuilder->expr()->neq('t_'.$locale.'.`text`', ':var_'.$uniqueId);
214
                            $this->queryBuilder->setParameter('var_'.$uniqueId, $textValue);
215
216
                            break;
217 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...
218
                            $expr = $this->queryBuilder->expr()->like('t_'.$locale.'.`text`', ':var_'.$uniqueId);
219
                            $this->queryBuilder->setParameter('var_'.$uniqueId, '%'.$textValue.'%');
220
221
                            break;
222 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...
223
                            $expr = 't_'.$locale.'.`text`'.' NOT LIKE :var_'.$uniqueId;
224
                            $this->queryBuilder->setParameter('var_'.$uniqueId, '%'.$textValue.'%');
225
226
                            break;
227
                        case 'startswith':
228
                            $expr = $this->queryBuilder->expr()->like('t_'.$locale.'.`text`', ':var_'.$uniqueId);
229
                            $this->queryBuilder->setParameter('var_'.$uniqueId, $textValue.'%');
230
231
                            break;
232
                        case 'endswith':
233
                            $expr = $this->queryBuilder->expr()->like('t_'.$locale.'.`text`', ':var_'.$uniqueId);
234
                            $this->queryBuilder->setParameter('var_'.$uniqueId, '%'.$textValue);
235
236
                            break;
237
                        case 'empty':
238
                            $expr = $this->queryBuilder->expr()->orX(
239
                                $this->queryBuilder->expr()->isNull('t_'.$locale.'.`text`'),
240
                                $this->queryBuilder->expr()->eq('t_'.$locale.'.`text`', '\'-\''),
241
                                $this->queryBuilder->expr()->eq('t_'.$locale.'.`text`', '\'\'')
242
                            );
243
244
                            break;
245
                    }
246
                    $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...
247
                }
248
249
                $this->queryBuilder->andWhere($orX);
250
            }
251
252
            // Apply sorting
253 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...
254
                $orderBy = $this->orderBy;
255
                $this->queryBuilder->orderBy($orderBy, ($this->orderDirection == 'DESC' ? 'DESC' : 'ASC'));
256
            }
257
        }
258
259
        return $this->queryBuilder;
260
    }
261
}
262