From a1cce3917e46544872c0b9a3cefba439779d0abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 7 Jul 2020 15:29:44 +0200 Subject: [PATCH] Fix PSR class/trait/method naming --- README.md | 10 ++++---- src/Migration.php | 26 ++++++++++----------- src/Migration/{MySQL.php => Mysql.php} | 2 +- src/Migration/{PgSQL.php => Postgresql.php} | 6 ++--- src/Migration/{SQLite.php => Sqlite.php} | 6 ++--- src/PhpunitTestCase.php | 4 ++-- tests/BasicTest.php | 10 ++++---- tests/PhpunitTestCaseTest.php | 10 ++++---- 8 files changed, 37 insertions(+), 37 deletions(-) rename src/Migration/{MySQL.php => Mysql.php} (97%) rename src/Migration/{PgSQL.php => Postgresql.php} (93%) rename src/Migration/{SQLite.php => Sqlite.php} (88%) diff --git a/README.md b/README.md index 37aeffe..01fe0f1 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ $migrator->table('user') ->alter(); ``` -Currently atk4/schema fully supports MySQL and SQLite connections, partly PgSQL and Oracle connections. +Currently atk4/schema fully supports MySQL and SQLite databases and partly PostgreSQL and Oracle. Other SQL databases are not yet natively supported but you can register your migrator class at runtime. ``` php @@ -85,11 +85,11 @@ table contents: ``` php getDB($tables); +$tables = $s->getDb($tables); // do anything with tables -$s->setDB($tables); +$s->setDb($tables); ``` ## Integration with PHPUnit @@ -105,13 +105,13 @@ $q = ['user' => [ ['name' => 'John', 'surname' => 'Smith'], ['name' => 'Steve', 'surname' => 'Jobs'], ]]; -$this->setDB($q); +$this->setDb($q); ``` Perform any changes, then execute: ``` -$this->assertEquals($q, $this->getDB('user')); +$this->assertEquals($q, $this->getDb('user')); ``` To ensure that database remained the same. Of course you can compare diff --git a/src/Migration.php b/src/Migration.php index d6474b8..6b6fc89 100644 --- a/src/Migration.php +++ b/src/Migration.php @@ -85,16 +85,16 @@ class Migration extends Expression * * Visibility is intentionally set to private. * If generic class Migration::of($source) is called, the migrator class will be resolved based on driverType of $source. - * When specific migrator class e.g Migration\MySQL::of($source) is called, driverType will not be resolved (the $registry property is NOT visible). - * MySQL migrator class will be used explicitly. + * When specific migrator class e.g Migration\Mysql::of($source) is called, driverType will not be resolved (the $registry property is NOT visible). + * Mysql migrator class will be used explicitly. * * @var array * * */ private static $registry = [ - 'sqlite' => Migration\SQLite::class, - 'mysql' => Migration\MySQL::class, - 'pgsql' => Migration\PgSQL::class, + 'sqlite' => Migration\Sqlite::class, + 'mysql' => Migration\Mysql::class, + 'pgsql' => Migration\Postgresql::class, 'oci' => Migration\Oracle::class, ]; @@ -136,16 +136,16 @@ public static function of($source, $params = []): self * * Can be used as: * - * Migration::register('mysql', CustomMigration\MySQL), or - * CustomMigration\MySQL::register('mysql') + * Migration::register('mysql', CustomMigration\Mysql), or + * CustomMigration\Mysql::register('mysql') * - * CustomMigration\MySQL must be descendant of Migration class. + * CustomMigration\Mysql must be descendant of Migration class. * * @param string $migrator */ public static function register(string $driverType, string $migrator = null) { - // forward to generic Migration::register if called with a descendant class e.g Migration\MySQL::register + // forward to generic Migration::register if called with a descendant class e.g Migration\Mysql::register if (static::class !== __CLASS__) { return call_user_func([__CLASS__, 'register'], $driverType, $migrator ?: static::class); } elseif (!$migrator) { @@ -411,8 +411,8 @@ public function run(): string // compare options and if needed alter field // @todo add more options here like 'len' if (array_key_exists('type', $old[$field]) && array_key_exists('type', $options)) { - $oldSQLFieldType = $this->getSQLFieldType($old[$field]['type']); - $newSQLFieldType = $this->getSQLFieldType($options['type']); + $oldSQLFieldType = $this->getSqlFieldType($old[$field]['type']); + $newSQLFieldType = $this->getSqlFieldType($options['type']); if ($oldSQLFieldType !== $newSQLFieldType) { $this->alterField($field, $options); ++$altered; @@ -591,7 +591,7 @@ public function getModelFieldType(string $type): ?string * @param string|null $type Agile Data field type * @param array $options More options */ - public function getSQLFieldType(?string $type, array $options = []): ?string + public function getSqlFieldType(?string $type, array $options = []): ?string { if ($type !== null) { $type = strtolower($type); @@ -742,7 +742,7 @@ public function _render_field() protected function _render_one_field(string $field, array $options): string { $name = $options['name'] ?? $field; - $type = $this->getSQLFieldType($options['type'] ?? null, $options); + $type = $this->getSqlFieldType($options['type'] ?? null, $options); return $this->_escape($name) . ' ' . $type; } diff --git a/src/Migration/MySQL.php b/src/Migration/Mysql.php similarity index 97% rename from src/Migration/MySQL.php rename to src/Migration/Mysql.php index 5191691..2c5b5a6 100644 --- a/src/Migration/MySQL.php +++ b/src/Migration/Mysql.php @@ -4,7 +4,7 @@ namespace atk4\schema\Migration; -class MySQL extends \atk4\schema\Migration +class Mysql extends \atk4\schema\Migration { /** * Field, table and alias name escaping symbol. diff --git a/src/Migration/PgSQL.php b/src/Migration/Postgresql.php similarity index 93% rename from src/Migration/PgSQL.php rename to src/Migration/Postgresql.php index 2cedd6e..b2a8c1f 100644 --- a/src/Migration/PgSQL.php +++ b/src/Migration/Postgresql.php @@ -5,7 +5,7 @@ namespace atk4\schema\Migration; // ONLY PARTIALLY IMPLEMENTED -class PgSQL extends \atk4\schema\Migration +class Postgresql extends \atk4\schema\Migration { /** @var string Expression to create primary key */ public $primary_key_expr = 'generated by default as identity(start with 1) primary key'; @@ -45,7 +45,7 @@ public function describeTable(string $table): array $row2 = []; $row2['name'] = $row['column_name']; $row2['pk'] = $row['is_identity'] === 'YES'; - $row2['type'] = preg_replace('/\(.*/', '', $row['udt_name']); // $row['data_type'], but it's PgSQL specific type + $row2['type'] = preg_replace('/\(.*/', '', $row['udt_name']); // $row['data_type'], but it's PostgreSQL specific type $result[] = $row2; } @@ -74,7 +74,7 @@ public function _render_statements(): string if (isset($this->args['alterField'])) { foreach ($this->args['alterField'] as $field => $option) { - $type = $this->getSQLFieldType($option['type'] ?? null, $option); + $type = $this->getSqlFieldType($option['type'] ?? null, $option); $result[] = 'alter column ' . $this->_escape($field) . ' type ' . $type . ' using (' . $this->_escape($field) . '::' . $type . ')'; // requires to cast value diff --git a/src/Migration/SQLite.php b/src/Migration/Sqlite.php similarity index 88% rename from src/Migration/SQLite.php rename to src/Migration/Sqlite.php index da02ccc..b4cb0bb 100644 --- a/src/Migration/SQLite.php +++ b/src/Migration/Sqlite.php @@ -4,7 +4,7 @@ namespace atk4\schema\Migration; -class SQLite extends \atk4\schema\Migration +class Sqlite extends \atk4\schema\Migration { /** @var string expression to create primary key */ public $primary_key_expr = 'primary key autoincrement'; @@ -29,9 +29,9 @@ public function describeTable(string $table): array * @param string|null $type Agile Data field type * @param array $options More options */ - public function getSQLFieldType(?string $type, array $options = []): ?string + public function getSqlFieldType(?string $type, array $options = []): ?string { - $res = parent::getSQLFieldType($type, $options); + $res = parent::getSqlFieldType($type, $options); // fix PK datatype to "integer primary key" // see https://www.sqlite.org/lang_createtable.html#rowid diff --git a/src/PhpunitTestCase.php b/src/PhpunitTestCase.php index 9410b7f..8c2cff4 100644 --- a/src/PhpunitTestCase.php +++ b/src/PhpunitTestCase.php @@ -79,7 +79,7 @@ public function dropTable($table) * @param array $db_data * @param bool $import_data Should we import data of just create table */ - public function setDB($db_data, $import_data = true) + public function setDb($db_data, $import_data = true) { $this->tables = array_keys($db_data); @@ -154,7 +154,7 @@ public function setDB($db_data, $import_data = true) * * @return array */ - public function getDB($tables = null, bool $no_id = false) + public function getDb($tables = null, bool $no_id = false) { $tables = $tables ?: $this->tables; diff --git a/tests/BasicTest.php b/tests/BasicTest.php index d31ab84..bccd3c5 100644 --- a/tests/BasicTest.php +++ b/tests/BasicTest.php @@ -8,7 +8,7 @@ use atk4\schema\Migration; use atk4\schema\PhpunitTestCase; -class CustomMySQLMigrator extends Migration +class CustomMysqlMigrator extends Migration { } @@ -100,13 +100,13 @@ public function testMigratorRegistering() // get original migrator registration $origMigratorClass = get_class($this->getMigrator()); - Migration::register($this->driverType, CustomMySQLMigrator::class); + Migration::register($this->driverType, CustomMysqlMigrator::class); - $this->assertSame(CustomMySQLMigrator::class, get_class($this->getMigrator())); + $this->assertSame(CustomMysqlMigrator::class, get_class($this->getMigrator())); - CustomMySQLMigrator::register($this->driverType); + CustomMysqlMigrator::register($this->driverType); - $this->assertSame(CustomMySQLMigrator::class, get_class($this->getMigrator())); + $this->assertSame(CustomMysqlMigrator::class, get_class($this->getMigrator())); // restore original migrator registration Migration::register($this->driverType, $origMigratorClass); diff --git a/tests/PhpunitTestCaseTest.php b/tests/PhpunitTestCaseTest.php index a7d2227..87734ec 100644 --- a/tests/PhpunitTestCaseTest.php +++ b/tests/PhpunitTestCaseTest.php @@ -10,20 +10,20 @@ class PhpunitTestCaseTest extends PhpunitTestCase { public function testInit() { - $this->setDB($q = [ + $this->setDb($q = [ 'user' => [ ['name' => 'John', 'surname' => 'Smith'], ['name' => 'Steve', 'surname' => 'Jobs'], ], ]); - $q2 = $this->getDB('user'); + $q2 = $this->getDb('user'); - $this->setDB($q2); - $q3 = $this->getDB('user'); + $this->setDb($q2); + $q3 = $this->getDb('user'); $this->assertSame($q2, $q3); - $this->assertSame($q, $this->getDB('user', true)); + $this->assertSame($q, $this->getDb('user', true)); } }