| Conditions | 21 |
| Paths | 2 |
| Total Lines | 96 |
| Code Lines | 60 |
| 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 |
||
| 55 | private static function generateTableFilter($filter = []) |
||
| 56 | { |
||
| 57 | $check_start = microtime(true); |
||
| 58 | $cached = Cache::get('query_builder_table_filter_migrations', []); |
||
| 59 | $db = DB::table('migrations')->pluck('migration'); |
||
| 60 | |||
| 61 | if ($db != $cached) { |
||
| 62 | Cache::forget('query_builder_table_filter'); |
||
| 63 | Cache::add('query_builder_table_filter_migrations', $db, 30); |
||
| 64 | } |
||
| 65 | $check_end = microtime(true); |
||
| 66 | Log::info('Query Builder filter check time: '.($check_end - $check_start).'s'); |
||
| 67 | |||
| 68 | |||
| 69 | return Cache::remember('query_builder_table_filter', 300, function() use ($filter) { |
||
| 70 | $schema = DB::getDoctrineSchemaManager(); |
||
| 71 | |||
| 72 | // Doctrine DBAL has issues with enums, pretend they are strings |
||
| 73 | $schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); |
||
| 74 | |||
| 75 | // $validTypes = ['string', 'integer', 'double', 'date', 'time', 'datetime', 'boolean']; |
||
| 76 | $ignoreTypes = ['blob', 'binary']; |
||
| 77 | |||
| 78 | $tables = $schema->listTables(); |
||
| 79 | foreach ($tables as $table) { |
||
| 80 | $columns = $schema->listTableColumns($table->getName()); |
||
| 81 | $tableName = $table->getName(); |
||
| 82 | |||
| 83 | // only allow tables with direct association to device_id for now |
||
| 84 | if (!$table->hasColumn('device_id')) { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | |||
| 88 | foreach ($columns as $column) { |
||
| 89 | $item = []; |
||
| 90 | $type = $column->getType()->getName(); |
||
| 91 | $name = $column->getName(); |
||
| 92 | |||
| 93 | switch ($type) { |
||
| 94 | case 'text': |
||
| 95 | // $item['input'] = 'textarea'; |
||
| 96 | case 'string': |
||
| 97 | $item['type'] = 'string'; |
||
| 98 | break; |
||
| 99 | |||
| 100 | case 'integer': |
||
| 101 | case 'smallint': |
||
| 102 | case 'bigint': |
||
| 103 | $item['type'] = 'integer'; |
||
| 104 | break; |
||
| 105 | |||
| 106 | case 'double': |
||
| 107 | case 'float': |
||
| 108 | case 'decimal': |
||
| 109 | $item['type'] = 'double'; |
||
| 110 | break; |
||
| 111 | |||
| 112 | case 'date': |
||
| 113 | $item['type'] = 'date'; |
||
| 114 | break; |
||
| 115 | |||
| 116 | case 'time': |
||
| 117 | $item['type'] = 'time'; |
||
| 118 | break; |
||
| 119 | |||
| 120 | case 'datetime': |
||
| 121 | $item['type'] = 'datetime'; |
||
| 122 | break; |
||
| 123 | |||
| 124 | case 'boolean': |
||
| 125 | $item['type'] = 'boolean'; |
||
| 126 | break; |
||
| 127 | |||
| 128 | } |
||
| 129 | |||
| 130 | if (!isset($item['type'])) { |
||
| 131 | if (!in_array($type, $ignoreTypes)) { |
||
| 132 | dd($type); |
||
| 133 | } |
||
| 134 | continue; |
||
| 135 | } |
||
| 136 | |||
| 137 | // ignore device id columns, except in the devices table |
||
| 138 | if ($name == 'device_id') { |
||
| 139 | if ($tableName != 'devices') { |
||
| 140 | continue; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | $item['id'] = $tableName.'.'.$name; |
||
| 145 | $filter[] = $item; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | return $filter; |
||
| 149 | }); |
||
| 150 | } |
||
| 151 | |||
| 156 | } |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: