| Conditions | 8 |
| Paths | 2 |
| Total Lines | 53 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 54 | private static function generateTableFilter($filter = []) |
||
| 55 | { |
||
| 56 | // check if the database schema has changed |
||
| 57 | $db = DB::table('migrations')->pluck('migration'); |
||
| 58 | $cached = Cache::get('migrations_list', []); |
||
| 59 | |||
| 60 | if ($db != $cached) { |
||
| 61 | Cache::forget('query_builder_table_filter'); |
||
| 62 | Cache::forever('migrations_list', $db); |
||
| 63 | } |
||
| 64 | |||
| 65 | // return the table filter merged with $filter, fetch from cache if available |
||
| 66 | return array_merge($filter, Cache::rememberForever('query_builder_table_filter', function() { |
||
| 67 | $tableFilter = []; |
||
| 68 | $schema = DB::getDoctrineSchemaManager(); |
||
| 69 | |||
| 70 | // Doctrine DBAL has issues with enums, pretend they are strings |
||
| 71 | $schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); |
||
| 72 | |||
| 73 | $tables = $schema->listTables(); |
||
| 74 | foreach ($tables as $table) { |
||
| 75 | $columns = $schema->listTableColumns($table->getName()); |
||
| 76 | $tableName = $table->getName(); |
||
| 77 | |||
| 78 | // only allow tables with a direct association to device_id |
||
| 79 | if (!$table->hasColumn('device_id')) { |
||
| 80 | continue; |
||
| 81 | } |
||
| 82 | |||
| 83 | foreach ($columns as $column) { |
||
| 84 | $columnName = $column->getName(); |
||
| 85 | |||
| 86 | // ignore device id columns, except in the devices table |
||
| 87 | if ($columnName == 'device_id' && $tableName != 'devices') { |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | |||
| 91 | $columnType = self::getColumnType($column->getType()->getName()); |
||
| 92 | |||
| 93 | // ignore unsupported types (such as binary and blob) |
||
| 94 | if (is_null($columnType)) { |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | |||
| 98 | $tableFilter[] = [ |
||
| 99 | 'id' => $tableName.'.'.$columnName, |
||
| 100 | 'type' => $columnType, |
||
| 101 | ]; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | return $tableFilter; |
||
| 105 | })); |
||
| 106 | } |
||
| 107 | |||
| 146 | } |
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: