Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring of Schema::normalizeDefaultValue() method #262

Merged
merged 4 commits into from
Jul 20, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
use function explode;
use function md5;
use function preg_match;
use function preg_replace;
use function serialize;
use function strncasecmp;
use function strtolower;
use function trim;

/**
* Implements the SQLite Server specific schema, supporting SQLite 3.3.0 or higher.
Expand Down Expand Up @@ -505,20 +505,20 @@ protected function loadColumnSchema(array $info): ColumnSchemaInterface
* @param ColumnSchemaInterface $column The column schema object.
*
* @return mixed The normalized default value.
*
* @psalm-suppress PossiblyNullArgument
*/
private function normalizeDefaultValue(?string $defaultValue, ColumnSchemaInterface $column): mixed
private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaInterface $column): mixed
{
if ($column->isPrimaryKey()) {
if ($column->isPrimaryKey() || in_array($defaultValue, [null, '', 'null', 'NULL'], true)) {
return null;
}

return match ($defaultValue) {
null, 'null', '' => null,
'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME' => new Expression($defaultValue),
default => $column->phpTypecast(trim($defaultValue, "'\"")),
};
if (in_array($defaultValue, ['CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME'], true)) {
return new Expression($defaultValue);
}

$value = preg_replace('/^([\'"])(.*)\1$/s', '$2', $defaultValue);
vjik marked this conversation as resolved.
Show resolved Hide resolved

return $column->phpTypecast($value);
}

/**
Expand Down
Loading