| Conditions | 20 | 
| Paths | 1 | 
| Total Lines | 89 | 
| Code Lines | 54 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
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:
If many parameters/temporary variables are present:
| 1 | <?php | ||
| 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']; | ||
|  | |||
| 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'; | ||
| 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 | } | 
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.