From 889686b3cd2547e104ebf1377a346b622a1419d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 2 Oct 2022 13:53:32 +0200 Subject: [PATCH 1/7] minor CS --- src/Model.php | 22 +---- src/Model/AggregateModel.php | 1 - src/Persistence.php | 3 - src/Persistence/Sql.php | 7 -- src/Persistence/Sql/Expression.php | 8 +- src/Persistence/Sql/Oracle/PlatformTrait.php | 4 +- src/Util/DeepCopy.php | 1 - tests/Schema/TestCaseTest.php | 89 ++++++++++---------- tests/UserActionTest.php | 4 +- 9 files changed, 55 insertions(+), 84 deletions(-) diff --git a/src/Model.php b/src/Model.php index 7672dd309..11c20134e 100644 --- a/src/Model.php +++ b/src/Model.php @@ -56,46 +56,32 @@ class Model implements \IteratorAggregate use Model\UserActionsTrait; use ReadableCaptionTrait; - /** @const string */ public const HOOK_BEFORE_LOAD = self::class . '@beforeLoad'; - /** @const string */ public const HOOK_AFTER_LOAD = self::class . '@afterLoad'; - /** @const string */ public const HOOK_BEFORE_UNLOAD = self::class . '@beforeUnload'; - /** @const string */ public const HOOK_AFTER_UNLOAD = self::class . '@afterUnload'; - /** @const string */ public const HOOK_BEFORE_INSERT = self::class . '@beforeInsert'; - /** @const string */ public const HOOK_AFTER_INSERT = self::class . '@afterInsert'; - /** @const string */ public const HOOK_BEFORE_UPDATE = self::class . '@beforeUpdate'; - /** @const string */ public const HOOK_AFTER_UPDATE = self::class . '@afterUpdate'; - /** @const string */ public const HOOK_BEFORE_DELETE = self::class . '@beforeDelete'; - /** @const string */ public const HOOK_AFTER_DELETE = self::class . '@afterDelete'; - /** @const string */ public const HOOK_BEFORE_SAVE = self::class . '@beforeSave'; - /** @const string */ public const HOOK_AFTER_SAVE = self::class . '@afterSave'; - /** @const string Executed when execution of self::atomic() failed. */ + /** Executed when execution of self::atomic() failed. */ public const HOOK_ROLLBACK = self::class . '@rollback'; - /** @const string Executed for every field set using self::set() method. */ + /** Executed for every field set using self::set() method. */ public const HOOK_NORMALIZE = self::class . '@normalize'; - /** @const string Executed when self::validate() method is called. */ + /** Executed when self::validate() method is called. */ public const HOOK_VALIDATE = self::class . '@validate'; - /** @const string Executed when self::setOnlyFields() method is called. */ + /** Executed when self::setOnlyFields() method is called. */ public const HOOK_ONLY_FIELDS = self::class . '@onlyFields'; - /** @const string */ protected const ID_LOAD_ONE = self::class . '@idLoadOne-h7axmDNBB3qVXjVv'; - /** @const string */ protected const ID_LOAD_ANY = self::class . '@idLoadAny-h7axmDNBB3qVXjVv'; // {{{ Properties of the class diff --git a/src/Model/AggregateModel.php b/src/Model/AggregateModel.php index d5abf227a..06c4584a5 100644 --- a/src/Model/AggregateModel.php +++ b/src/Model/AggregateModel.php @@ -39,7 +39,6 @@ */ class AggregateModel extends Model { - /** @const string */ public const HOOK_INIT_AGGREGATE_SELECT_QUERY = self::class . '@initAggregateSelectQuery'; /** @var array */ diff --git a/src/Persistence.php b/src/Persistence.php index 2ce131f4f..9ebd219e5 100644 --- a/src/Persistence.php +++ b/src/Persistence.php @@ -22,12 +22,9 @@ abstract class Persistence use HookTrait; use NameTrait; - /** @const string */ public const HOOK_AFTER_ADD = self::class . '@afterAdd'; - /** @const string */ public const ID_LOAD_ONE = self::class . '@idLoadOne-qZ5TJwMVJ4LzVhuN'; - /** @const string */ public const ID_LOAD_ANY = self::class . '@idLoadAny-qZ5TJwMVJ4LzVhuN'; /** @internal prevent recursion */ diff --git a/src/Persistence/Sql.php b/src/Persistence/Sql.php index 7c570ba66..84cb97ff7 100644 --- a/src/Persistence/Sql.php +++ b/src/Persistence/Sql.php @@ -26,19 +26,12 @@ class Sql extends Persistence { use Sql\BinaryTypeCompatibilityTypecastTrait; - /** @const string */ public const HOOK_INIT_SELECT_QUERY = self::class . '@initSelectQuery'; - /** @const string */ public const HOOK_BEFORE_INSERT_QUERY = self::class . '@beforeInsertQuery'; - /** @const string */ public const HOOK_AFTER_INSERT_QUERY = self::class . '@afterInsertQuery'; - /** @const string */ public const HOOK_BEFORE_UPDATE_QUERY = self::class . '@beforeUpdateQuery'; - /** @const string */ public const HOOK_AFTER_UPDATE_QUERY = self::class . '@afterUpdateQuery'; - /** @const string */ public const HOOK_BEFORE_DELETE_QUERY = self::class . '@beforeDeleteQuery'; - /** @const string */ public const HOOK_AFTER_DELETE_QUERY = self::class . '@afterDeleteQuery'; /** @var Connection */ diff --git a/src/Persistence/Sql/Expression.php b/src/Persistence/Sql/Expression.php index 092ec4963..f16569004 100644 --- a/src/Persistence/Sql/Expression.php +++ b/src/Persistence/Sql/Expression.php @@ -22,13 +22,13 @@ abstract class Expression implements Expressionable, \ArrayAccess { use DiContainerTrait; - /** @const string "[]" in template, escape as parameter */ + /** "[]" in template, escape as parameter */ protected const ESCAPE_PARAM = 'param'; - /** @const string "{}" in template, escape as identifier */ + /** "{}" in template, escape as identifier */ protected const ESCAPE_IDENTIFIER = 'identifier'; - /** @const string "{{}}" in template, escape as identifier, but keep input with special characters like "." or "(" unescaped */ + /** "{{}}" in template, escape as identifier, but keep input with special characters like "." or "(" unescaped */ protected const ESCAPE_IDENTIFIER_SOFT = 'identifier-soft'; - /** @const string keep input as is */ + /** Keep input as is */ protected const ESCAPE_NONE = 'none'; protected ?string $template = null; diff --git a/src/Persistence/Sql/Oracle/PlatformTrait.php b/src/Persistence/Sql/Oracle/PlatformTrait.php index e91436cc9..b87b13d86 100644 --- a/src/Persistence/Sql/Oracle/PlatformTrait.php +++ b/src/Persistence/Sql/Oracle/PlatformTrait.php @@ -71,7 +71,7 @@ public function getCreateAutoincrementSql($name, $table, $start = 1) $pkSeq = \Closure::bind(fn () => $this->normalizeIdentifier($aiSequenceName), $this, OraclePlatform::class)()->getName(); $sqls[count($sqls) - 1] = $conn->expr( // else branch should be maybe (because of concurrency) put into after update trigger - str_replace('[pk_seq]', '\'' . str_replace('\'', '\'\'', $pkSeq) . '\'', <<<'EOT' + str_replace('[pk_seq]', '\'' . str_replace('\'', '\'\'', $pkSeq) . '\'', <<<'EOF' CREATE TRIGGER {{trigger}} BEFORE INSERT OR UPDATE ON {{table}} @@ -89,7 +89,7 @@ public function getCreateAutoincrementSql($name, $table, $start = 1) END LOOP; END IF; END; - EOT), + EOF), [ 'trigger' => \Closure::bind(fn () => $this->normalizeIdentifier($aiTriggerName), $this, OraclePlatform::class)()->getName(), 'table' => $tableIdentifier->getName(), diff --git a/src/Util/DeepCopy.php b/src/Util/DeepCopy.php index de3196d91..aee206627 100644 --- a/src/Util/DeepCopy.php +++ b/src/Util/DeepCopy.php @@ -22,7 +22,6 @@ class DeepCopy { use DebugTrait; - /** @const string */ public const HOOK_AFTER_COPY = self::class . '@afterCopy'; /** Model from which we want to copy records */ diff --git a/tests/Schema/TestCaseTest.php b/tests/Schema/TestCaseTest.php index e68c724d3..897900c25 100644 --- a/tests/Schema/TestCaseTest.php +++ b/tests/Schema/TestCaseTest.php @@ -41,52 +41,49 @@ public function testLogQuery(): void return; } - $this->assertSameSql( - <<<'EOF' - - "START TRANSACTION"; - - - insert into `t` (`name`, `int`, `float`, `null`) - values - ('Ewa', 1, '1.0', NULL); - - - select - `id`, - `name`, - `int`, - `float`, - `null` - from - `t` - where - `int` > -1 - and `id` = 1 - limit - 0, - 2; - - - "COMMIT"; - - - select - `id`, - `name`, - `int`, - `float`, - `null` - from - `t` - where - `int` > -1 - limit - 0, - 1; - EOF . "\n\n", - $output - ); + $this->assertSameSql(<<<'EOF' + + "START TRANSACTION"; + + + insert into `t` (`name`, `int`, `float`, `null`) + values + ('Ewa', 1, '1.0', NULL); + + + select + `id`, + `name`, + `int`, + `float`, + `null` + from + `t` + where + `int` > -1 + and `id` = 1 + limit + 0, + 2; + + + "COMMIT"; + + + select + `id`, + `name`, + `int`, + `float`, + `null` + from + `t` + where + `int` > -1 + limit + 0, + 1; + EOF . "\n\n", $output); } public function testGetSetDropDb(): void diff --git a/tests/UserActionTest.php b/tests/UserActionTest.php index 53f3b60db..b902f2960 100644 --- a/tests/UserActionTest.php +++ b/tests/UserActionTest.php @@ -9,7 +9,7 @@ use Atk4\Data\Persistence; use Atk4\Data\Schema\TestCase; -trait UaReminder +trait UaReminderTrait { public function sendReminder(): string { @@ -26,7 +26,7 @@ public function backupClients(): string class UaClient extends Model { - use UaReminder; + use UaReminderTrait; public $caption = 'UaClient'; From 7aa34178e7963fe801a5e0fe9a8b5a7f65bccae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Wed, 12 Oct 2022 10:16:59 +0200 Subject: [PATCH 2/7] unify regex delim --- src/Field/EmailField.php | 2 +- src/Persistence/Array_/Action.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Field/EmailField.php b/src/Field/EmailField.php index f39895266..6289685df 100644 --- a/src/Field/EmailField.php +++ b/src/Field/EmailField.php @@ -32,7 +32,7 @@ public function normalize($value) $email = trim($value); if ($this->allowName) { - $email = preg_replace('/^[^<]*<([^>]*)>/', '\1', $email); + $email = preg_replace('~^[^<]*<([^>]*)>~', '\1', $email); } if (!str_contains($email, '@')) { diff --git a/src/Persistence/Array_/Action.php b/src/Persistence/Array_/Action.php index 982a566f1..cdb3718d2 100644 --- a/src/Persistence/Array_/Action.php +++ b/src/Persistence/Array_/Action.php @@ -188,9 +188,9 @@ protected function evaluateIf($v1, string $operator, $v2): bool break; case 'LIKE': - $pattern = str_ireplace('%', '(.*?)', preg_quote($v2)); + $pattern = str_ireplace('%', '(.*?)', preg_quote($v2, '~')); - $result = (bool) preg_match('/^' . $pattern . '$/', (string) $v1); + $result = (bool) preg_match('~^' . $pattern . '$~', (string) $v1); break; case 'NOT LIKE': From cce74f6499088badcb71f66d70c96380d23af155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 15 Oct 2022 12:51:48 +0200 Subject: [PATCH 3/7] minor cs --- src/Field/EmailField.php | 6 +++--- src/Model.php | 1 + src/Model/AggregateModel.php | 3 +-- src/Persistence/Sql/Connection.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Field/EmailField.php b/src/Field/EmailField.php index 6289685df..2b001d6a6 100644 --- a/src/Field/EmailField.php +++ b/src/Field/EmailField.php @@ -11,9 +11,9 @@ * Stores valid email as per configuration. * * Usage: - * $user->addField('email', [EmailField::class]); - * $user->addField('email_mx_check', [EmailField::class, 'dnsCheck' => true]); - * $user->addField('email_with_name', [EmailField::class, 'allowName' => true]); + * $user->addField('email', [EmailField::class]); + * $user->addField('email_mx_check', [EmailField::class, 'dnsCheck' => true]); + * $user->addField('email_with_name', [EmailField::class, 'allowName' => true]); */ class EmailField extends Field { diff --git a/src/Model.php b/src/Model.php index 11c20134e..88a86bf5f 100644 --- a/src/Model.php +++ b/src/Model.php @@ -1672,6 +1672,7 @@ public function export(array $fields = null, string $keyField = null, bool $type // no key field - then just do export if ($keyField === null) { + /* TODO this optimization should be removed in favor of one Persistence::export call and php calcualted fields should be exported as well */ return $this->getPersistence()->export($this, $fields, $typecast); } diff --git a/src/Model/AggregateModel.php b/src/Model/AggregateModel.php index 06c4584a5..f8e9117e1 100644 --- a/src/Model/AggregateModel.php +++ b/src/Model/AggregateModel.php @@ -21,8 +21,7 @@ * 'salary' => ['expr' => 'sum([])', 'type' => 'atk4_money'], * ]; * - * your resulting model will have 3 fields: - * first, last, salary + * your resulting model will have 3 fields: first, last, salary * * but when querying it will use the original model to calculate the query, then add grouping and aggregates. * diff --git a/src/Persistence/Sql/Connection.php b/src/Persistence/Sql/Connection.php index 3090d79ee..b997505c6 100644 --- a/src/Persistence/Sql/Connection.php +++ b/src/Persistence/Sql/Connection.php @@ -152,7 +152,7 @@ public static function normalizeDsn($dsn, $user = null, $password = null) * Adds connection class to the registry for resolving in Connection::resolve method. * * Can be used as: - * Connection::registerConnection(MySQL\Connection::class, 'pdo_mysql') + * Connection::registerConnection(MySQL\Connection::class, 'pdo_mysql') * * @param class-string $connectionClass */ From ed606d0b3790395f5a5d87ccb6e6b26f9436e2d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 15 Oct 2022 12:53:17 +0200 Subject: [PATCH 4/7] fix comment --- src/Persistence/Sql.php | 16 ++++++++-------- src/Persistence/Sql/Connection.php | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Persistence/Sql.php b/src/Persistence/Sql.php index 84cb97ff7..9ba9124ba 100644 --- a/src/Persistence/Sql.php +++ b/src/Persistence/Sql.php @@ -90,14 +90,6 @@ public function disconnect(): void $this->_connection = null; // @phpstan-ignore-line } - /** - * Returns Query instance. - */ - public function dsql(): Query - { - return $this->getConnection()->dsql(); - } - /** * Atomic executes operations within one begin/end transaction, so if * the code inside callback will fail, then all of the transaction @@ -189,6 +181,14 @@ public function exprNow(int $precision = null): Expression return $this->getConnection()->dsql()->exprNow($precision); } + /** + * Creates new Query object. + */ + public function dsql(): Query + { + return $this->getConnection()->dsql(); + } + /** * Initializes base query for model $m. */ diff --git a/src/Persistence/Sql/Connection.php b/src/Persistence/Sql/Connection.php index b997505c6..aceb6b57e 100644 --- a/src/Persistence/Sql/Connection.php +++ b/src/Persistence/Sql/Connection.php @@ -281,7 +281,7 @@ protected static function connectFromDbalDriverConnection(DbalDriverConnection $ } /** - * Returns new Expression with connection already set. + * Create new Expression with connection already set. * * @param string|array $template * @param array $arguments @@ -296,7 +296,7 @@ public function expr($template = [], array $arguments = []): Expression } /** - * Returns new Query with connection already set. + * Create new Query with connection already set. * * @param string|array $defaults */ From a37bef132c03522ed442b436005e5e050cc9db2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 15 Oct 2022 12:54:19 +0200 Subject: [PATCH 5/7] fix phpstan ignore not needed anymore --- tests/Model/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Model/Client.php b/tests/Model/Client.php index 663d9e5bf..284b49ac4 100644 --- a/tests/Model/Client.php +++ b/tests/Model/Client.php @@ -6,7 +6,7 @@ class Client extends User { - public $table = 'client'; // @phpstan-ignore-line https://github.com/phpstan/phpstan/issues/7839 + public $table = 'client'; protected function init(): void { From 7e971358e2fdb67890d59051c5068a962a54cba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 15 Oct 2022 12:55:43 +0200 Subject: [PATCH 6/7] fix cs --- src/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model.php b/src/Model.php index 88a86bf5f..7ffa1d535 100644 --- a/src/Model.php +++ b/src/Model.php @@ -1672,7 +1672,7 @@ public function export(array $fields = null, string $keyField = null, bool $type // no key field - then just do export if ($keyField === null) { - /* TODO this optimization should be removed in favor of one Persistence::export call and php calcualted fields should be exported as well */ + // TODO this optimization should be removed in favor of one Persistence::export call and php calcualted fields should be exported as well return $this->getPersistence()->export($this, $fields, $typecast); } From 94d16d8125f714088a2893f5805942e62bab7764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 15 Oct 2022 13:04:05 +0200 Subject: [PATCH 7/7] fix typo --- src/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model.php b/src/Model.php index 7ffa1d535..1a8076967 100644 --- a/src/Model.php +++ b/src/Model.php @@ -1672,7 +1672,7 @@ public function export(array $fields = null, string $keyField = null, bool $type // no key field - then just do export if ($keyField === null) { - // TODO this optimization should be removed in favor of one Persistence::export call and php calcualted fields should be exported as well + // TODO this optimization should be removed in favor of one Persistence::export call and php calculated fields should be exported as well return $this->getPersistence()->export($this, $fields, $typecast); }