| Conditions | 3 |
| Paths | 3 |
| Total Lines | 58 |
| Code Lines | 39 |
| 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 |
||
| 33 | public function introspectSchema(string $schema_name = null): Schema |
||
| 34 | { |
||
| 35 | $schema_name ?? $schema_name = $this->query('SELECT DATABASE() AS schema_name')[0]->schema_name; |
||
| 36 | |||
| 37 | $pattern = $this->escapeLike($this->prefix) . '%'; |
||
| 38 | |||
| 39 | $sql = |
||
| 40 | 'SELECT TABLE_NAME, ENGINE, AUTO_INCREMENT, TABLE_COLLATION' . |
||
| 41 | ' FROM INFORMATION_SCHEMA.TABLES' . |
||
| 42 | ' WHERE TABLE_TYPE = :table_type' . |
||
| 43 | ' AND TABLE_SCHEMA = :table_schema' . |
||
| 44 | ' AND TABLE_NAME LIKE :pattern' . |
||
| 45 | ' ORDER BY TABLE_NAME'; |
||
| 46 | |||
| 47 | $table_rows = $this->query($sql, [ |
||
| 48 | 'table_type' => 'BASE TABLE', |
||
| 49 | 'pattern' => $pattern, |
||
| 50 | 'table_schema' => $schema_name, |
||
| 51 | ]); |
||
| 52 | |||
| 53 | $sql = |
||
| 54 | 'SELECT TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, COLUMN_DEFAULT, IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, CHARACTER_OCTET_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, DATETIME_PRECISION, CHARACTER_SET_NAME, COLLATION_NAME, COLUMN_TYPE, COLUMN_KEY, EXTRA, COLUMN_COMMENT, GENERATION_EXPRESSION, SRS_ID' . |
||
| 55 | ' FROM INFORMATION_SCHEMA.COLUMNS' . |
||
| 56 | ' WHERE TABLE_SCHEMA = :table_schema' . |
||
| 57 | ' AND TABLE_NAME LIKE :pattern' . |
||
| 58 | ' ORDER BY ORDINAL_POSITION'; |
||
| 59 | |||
| 60 | $column_rows = $this->query($sql, ['pattern' => $pattern, 'table_schema' => $schema_name]); |
||
| 61 | |||
| 62 | $sql = |
||
| 63 | 'SELECT TABLE_NAME, CONSTRAINT_NAME, CONSTRAINT_TYPE' . |
||
| 64 | ' FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS' . |
||
| 65 | ' WHERE TABLE_SCHEMA = :table_schema' . |
||
| 66 | ' AND TABLE_NAME LIKE :pattern'; |
||
| 67 | |||
| 68 | $table_constraints_rows = $this->query($sql, ['pattern' => $pattern, 'table_schema' => $schema_name]); |
||
| 69 | |||
| 70 | $sql = |
||
| 71 | 'SELECT CONSTRAINT_NAME, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, POSITION_IN_UNIQUE_CONSTRAINT,' . |
||
| 72 | ' REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME' . |
||
| 73 | ' FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE' . |
||
| 74 | ' WHERE TABLE_SCHEMA = :table_schema' . |
||
| 75 | ' AND TABLE_NAME LIKE :pattern'; |
||
| 76 | |||
| 77 | $key_column_usage_rows = $this->query($sql, ['pattern' => $pattern, 'table_schema' => $schema_name]); |
||
| 78 | |||
| 79 | foreach ($table_rows as $table_row) { |
||
| 80 | $table = new Table($table_row->TABLE_NAME); |
||
| 81 | |||
| 82 | $this_column_rows = array_filter($column_rows, static fn (object $row): bool => $row->TABLE_NAME === $table_row->TABLE_NAME); |
||
| 83 | |||
| 84 | foreach ($this_column_rows as $this_column_row) { |
||
| 85 | $column = new Column($this_column_row->COLUMN_NAME, $this_column_row->COLUMN_TYPE, $this_column_row->COLUMN_DEFAULT); |
||
| 86 | } |
||
| 87 | |||
| 88 | } |
||
| 89 | |||
| 90 | var_dump($pattern, $schema_name, $table_rows, $column_rows, $table_constraints_rows, $key_column_usage_rows);exit; |
||
| 91 | } |
||
| 93 |
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: