| 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 | ||
| 56 | private static function generateTableFilter($filter = []) | ||
| 57 |     { | ||
| 58 | $check_start = microtime(true); | ||
| 59 |         $cached = Cache::get('query_builder_table_filter_migrations', []); | ||
| 60 |         $db = DB::table('migrations')->pluck('migration'); | ||
| 61 | |||
| 62 |         if ($db != $cached) { | ||
| 63 |             Cache::forget('query_builder_table_filter'); | ||
| 64 |             Cache::add('query_builder_table_filter_migrations', $db, 30); | ||
| 65 | } | ||
| 66 | $check_end = microtime(true); | ||
| 67 |         Log::info('Query Builder filter check time: '.($check_end - $check_start).'s'); | ||
| 68 | |||
| 69 | |||
| 70 |         return Cache::remember('query_builder_table_filter', 300, function() use ($filter) { | ||
| 71 | $schema = DB::getDoctrineSchemaManager(); | ||
| 72 | |||
| 73 | // Doctrine DBAL has issues with enums, pretend they are strings | ||
| 74 |             $schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); | ||
| 75 | |||
| 76 | // $validTypes = ['string', 'integer', 'double', 'date', 'time', 'datetime', 'boolean']; | ||
| 77 | $ignoreTypes = ['blob', 'binary']; | ||
| 78 | |||
| 79 | $tables = $schema->listTables(); | ||
| 80 |             foreach ($tables as $table) { | ||
| 81 | $columns = $schema->listTableColumns($table->getName()); | ||
| 82 | $tableName = $table->getName(); | ||
| 83 | |||
| 84 | // only allow tables with direct association to device_id for now | ||
| 85 |                 if (!$table->hasColumn('device_id')) { | ||
| 86 | continue; | ||
| 87 | } | ||
| 88 | |||
| 89 |                 foreach ($columns as $column) { | ||
| 90 | $item = []; | ||
| 91 | $type = $column->getType()->getName(); | ||
| 92 | $name = $column->getName(); | ||
| 93 | |||
| 94 |                     switch ($type) { | ||
| 95 | case 'text': | ||
| 96 | // $item['input'] = 'textarea'; | ||
| 97 | case 'string': | ||
| 98 | $item['type'] = 'string'; | ||
| 99 | break; | ||
| 100 | |||
| 101 | case 'integer': | ||
| 102 | case 'smallint': | ||
| 103 | case 'bigint': | ||
| 104 | $item['type'] = 'integer'; | ||
| 105 | break; | ||
| 106 | |||
| 107 | case 'double': | ||
| 108 | case 'float': | ||
| 109 | case 'decimal': | ||
| 110 | $item['type'] = 'double'; | ||
| 111 | break; | ||
| 112 | |||
| 113 | case 'date': | ||
| 114 | $item['type'] = 'date'; | ||
| 115 | break; | ||
| 116 | |||
| 117 | case 'time': | ||
| 118 | $item['type'] = 'time'; | ||
| 119 | break; | ||
| 120 | |||
| 121 | case 'datetime': | ||
| 122 | $item['type'] = 'datetime'; | ||
| 123 | break; | ||
| 124 | |||
| 125 | case 'boolean': | ||
| 126 | $item['type'] = 'boolean'; | ||
| 127 | break; | ||
| 128 | |||
| 129 | } | ||
| 130 | |||
| 131 |                     if (!isset($item['type'])) { | ||
| 132 |                         if (!in_array($type, $ignoreTypes)) { | ||
| 133 | dd($type); | ||
| 134 | } | ||
| 135 | continue; | ||
| 136 | } | ||
| 137 | |||
| 138 | // ignore device id columns, except in the devices table | ||
| 139 |                     if ($name == 'device_id') { | ||
| 140 |                         if ($tableName != 'devices') { | ||
| 141 | continue; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | $item['id'] = $tableName.'.'.$name; | ||
| 146 | $filter[] = $item; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | return $filter; | ||
| 150 | }); | ||
| 151 | } | ||
| 152 | |||
| 157 | } | 
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: