Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
Fix PSR class/trait/method naming
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 7, 2020
1 parent c2d57a4 commit 97a61d7
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 43 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,11 +85,11 @@ table contents:
``` php
<?php
$s = new \atk4\data\schema\Snapshot($connection);
$tables = $s->getDB($tables);
$tables = $s->getDb($tables);

// do anything with tables

$s->setDB($tables);
$s->setDb($tables);
```

## Integration with PHPUnit
Expand All @@ -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
Expand Down
38 changes: 19 additions & 19 deletions src/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use atk4\core\Exception;
use atk4\data\Field;
use atk4\data\Field_SQL_Expression;
use atk4\data\FieldSqlExpression;
use atk4\Data\Model;
use atk4\data\Persistence;
use atk4\data\Reference\HasOne;
Expand Down Expand Up @@ -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,
];

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -180,12 +180,12 @@ public static function getConnection($source): Connection
{
if ($source instanceof Connection) {
return $source;
} elseif ($source instanceof Persistence\SQL) {
} elseif ($source instanceof Persistence\Sql) {
return $source->connection;
} elseif (
$source instanceof Model
&& $source->persistence
&& ($source->persistence instanceof Persistence\SQL)
&& ($source->persistence instanceof Persistence\Sql)
) {
return $source->persistence->connection;
}
Expand Down Expand Up @@ -219,7 +219,7 @@ public function setSource($source)
if (
$source instanceof Model
&& $source->persistence
&& ($source->persistence instanceof Persistence\SQL)
&& ($source->persistence instanceof Persistence\Sql)
) {
$this->setModel($source);
}
Expand All @@ -238,7 +238,7 @@ public function setModel(Model $m): Model
continue;
}

if ($field instanceof Field_SQL_Expression) {
if ($field instanceof FieldSqlExpression) {
continue;
}

Expand Down Expand Up @@ -292,7 +292,7 @@ protected function getReferenceField(Field $field): ?Field
// @TODO fix, but without the dummy persistence, the following is shown:
// Uncaught atk4\core\Exception: Element is not found in collection
// for ID column
$dummyPersistence = new Persistence\SQL($this->connection);
$dummyPersistence = new Persistence\Sql($this->connection);

return (new $reference_model_class($dummyPersistence))->getField($reference_field);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Migration/MySQL.php → src/Migration/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/Migration/PgSQL.php → src/Migration/Postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Migration/SQLite.php → src/Migration/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/PhpunitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down
10 changes: 5 additions & 5 deletions tests/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use atk4\schema\Migration;
use atk4\schema\PhpunitTestCase;

class CustomMySQLMigrator extends Migration
class CustomMysqlMigrator extends Migration
{
}

Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions tests/PhpunitTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 97a61d7

Please sign in to comment.