Completed
Push — device-groups ( d263d8...cfdf4d )
by Tony
03:49
created

QueryBuilderFilter::generateFilter()   D

Complexity

Conditions 20
Paths 1

Size

Total Lines 89
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 89
rs 4.7294
cc 20
eloc 54
nc 1
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
 * QueryBuilderFilter.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace app;
27
28
29
use Cache;
30
use DB;
31
32
class QueryBuilderFilter
33
{
34
    /**
35
     *
36
     */
37
    public static function generateFilter()
38
    {
39
        //TODO use variable for cache time or check if it should be invalidated
40
//        Cache::forget('query_builder_filter');
41
42
        return Cache::remember('query_builder_filter', 30, function() {
43
            $filter = [];
44
            $schema = DB::getDoctrineSchemaManager();
45
46
            // Doctrine DBAL has issues with enums, pretend they are strings
47
            $schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
48
49
            $validTypes = ['string', 'integer', 'double', 'date', 'time', 'datetime', 'boolean'];
0 ignored issues
show
Unused Code introduced by
$validTypes 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...
50
            $ignoreTypes = ['blob', 'binary'];
51
52
            $tables = $schema->listTables();
53
            foreach ($tables as $table) {
54
                $columns = $schema->listTableColumns($table->getName());
55
                $tableName = $table->getName();
56
57
                // only allow tables with direct association to device_id for now
58
                if (!$table->hasColumn('device_id')) {
59
                    continue;
60
                }
61
62
                foreach ($columns as $column) {
63
                    $item = [];
64
                    $type = $column->getType()->getName();
65
                    $name = $column->getName();
66
67
                    switch ($type) {
68
                        case 'text':
69
//                            $item['input'] = 'textarea';
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
70
                        case 'string':
71
                            $item['type'] = 'string';
72
                            break;
73
74
                        case 'integer':
75
                        case 'smallint':
76
                        case 'bigint':
77
                            $item['type'] = 'integer';
78
                            break;
79
80
                        case 'double':
81
                        case 'float':
82
                        case 'decimal':
83
                            $item['type'] = 'double';
84
                            break;
85
86
                        case 'date':
87
                            $item['type'] = 'date';
88
                            break;
89
90
                        case 'time':
91
                            $item['type'] = 'time';
92
                            break;
93
94
                        case 'datetime':
95
                            $item['type'] = 'datetime';
96
                            break;
97
98
                        case 'boolean':
99
                            $item['type'] = 'boolean';
100
                            break;
101
102
                    }
103
104
                    if (!isset($item['type'])) {
105
                        if (!in_array($type, $ignoreTypes)) {
106
                            dd($type);
107
                        }
108
                        continue;
109
                    }
110
111
112
                    // ignore device id columns, except in the devices table
113
                    if ($name == 'device_id') {
114
                        if ($tableName != 'devices') {
115
                            continue;
116
                        }
117
                    }
118
119
                    $item['id'] = $tableName.'.'.$name;
120
                    $filter[] = $item;
121
                }
122
            }
123
            return json_encode($filter);
124
        });
125
    }
126
}