Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b35f40e

Browse files
authored
Refactoring of Schema::normalizeDefaultValue() method (#262)
1 parent 032e1b3 commit b35f40e

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 1.0.1 under development
44

55
- Enh #260: Typecast refactoring (@Tigrov)
6+
- Enh #262: Refactoring of `Schema::normalizeDefaultValue()` method (@Tigrov)
67

78
## 1.0.0 April 12, 2023
89

src/Schema.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
use function explode;
2626
use function md5;
2727
use function preg_match;
28+
use function preg_replace;
2829
use function serialize;
2930
use function strncasecmp;
3031
use function strtolower;
31-
use function trim;
3232

3333
/**
3434
* Implements the SQLite Server specific schema, supporting SQLite 3.3.0 or higher.
@@ -505,20 +505,20 @@ protected function loadColumnSchema(array $info): ColumnSchemaInterface
505505
* @param ColumnSchemaInterface $column The column schema object.
506506
*
507507
* @return mixed The normalized default value.
508-
*
509-
* @psalm-suppress PossiblyNullArgument
510508
*/
511-
private function normalizeDefaultValue(?string $defaultValue, ColumnSchemaInterface $column): mixed
509+
private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaInterface $column): mixed
512510
{
513-
if ($column->isPrimaryKey()) {
511+
if ($column->isPrimaryKey() || in_array($defaultValue, [null, '', 'null', 'NULL'], true)) {
514512
return null;
515513
}
516514

517-
return match ($defaultValue) {
518-
null, 'null', '' => null,
519-
'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME' => new Expression($defaultValue),
520-
default => $column->phpTypecast(trim($defaultValue, "'\"")),
521-
};
515+
if (in_array($defaultValue, ['CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME'], true)) {
516+
return new Expression($defaultValue);
517+
}
518+
519+
$value = preg_replace('/^([\'"])(.*)\1$/s', '$2', $defaultValue);
520+
521+
return $column->phpTypecast($value);
522522
}
523523

524524
/**

tests/Provider/SchemaProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static function columns(): array
9090
'size' => 100,
9191
'precision' => 100,
9292
'scale' => null,
93-
'defaultValue' => 'something',
93+
'defaultValue' => 'something"',
9494
],
9595
'char_col3' => [
9696
'type' => 'text',

tests/Support/Fixture/sqlite.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ CREATE TABLE "type" (
122122
tinyint_col TINYINT(3) DEFAULT '1',
123123
smallint_col SMALLINT(1) DEFAULT '1',
124124
char_col char(100) NOT NULL,
125-
char_col2 varchar(100) DEFAULT 'something',
125+
char_col2 varchar(100) DEFAULT 'something"',
126126
char_col3 text,
127127
float_col double(4,3) NOT NULL,
128128
float_col2 double DEFAULT '1.23',

0 commit comments

Comments
 (0)