Skip to content

Commit 8e53675

Browse files
authored
Make Connection constructor protected (#1138)
1 parent 947664c commit 8e53675

14 files changed

+183
-76
lines changed

docs/.readthedocs.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: 2
2+
3+
build:
4+
# https://github.com/readthedocs/readthedocs.org/issues/8861
5+
os: ubuntu-22.04
6+
tools:
7+
# https://github.com/readthedocs/readthedocs.org/issues/9719
8+
python: '3'
9+
10+
python:
11+
install:
12+
# https://github.com/readthedocs/readthedocs.org/issues/10806
13+
- requirements: docs/requirements.txt
14+
15+
formats:
16+
- pdf

phpstan.neon.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ parameters:
4040
-
4141
message: '~^Class Doctrine\\DBAL\\Platforms\\SqlitePlatform referenced with incorrect case: Doctrine\\DBAL\\Platforms\\SQLitePlatform\.$~'
4242
path: '*'
43-
count: 31
43+
count: 28
4444

4545
# TODO these rules are generated, this ignores should be fixed in the code
4646
# for src/Schema/TestCase.php

src/Field.php

+4
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public function normalize($value)
153153

154154
break;
155155
case 'float':
156+
case 'decimal':
156157
case 'atk4_money':
157158
$value = preg_replace('~\s+|[`\']|,(?=.*\.)~', '', $value);
158159

@@ -163,6 +164,7 @@ public function normalize($value)
163164
case 'boolean':
164165
case 'integer':
165166
case 'float':
167+
case 'decimal':
166168
case 'atk4_money':
167169
if ($value === '') {
168170
$value = null;
@@ -178,6 +180,7 @@ public function normalize($value)
178180
case 'text':
179181
case 'integer':
180182
case 'float':
183+
case 'decimal':
181184
case 'atk4_money':
182185
if (is_bool($value)) {
183186
throw new Exception('Must not be boolean type');
@@ -223,6 +226,7 @@ public function normalize($value)
223226
break;
224227
case 'integer':
225228
case 'float':
229+
case 'decimal':
226230
case 'atk4_money':
227231
if ($this->required && !$value) {
228232
throw new Exception('Must not be a zero');

src/Persistence.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public function typecastSaveField(Field $field, $value)
359359
try {
360360
$v = $this->_typecastSaveField($field, $value);
361361
if ($v !== null && !is_scalar($v)) { // @phpstan-ignore-line
362-
throw new Exception('Unexpected non-scalar value');
362+
throw new \TypeError('Unexpected non-scalar value');
363363
}
364364

365365
return $v;
@@ -382,7 +382,7 @@ public function typecastLoadField(Field $field, $value)
382382
if ($value === null) {
383383
return null;
384384
} elseif (!is_scalar($value)) { // @phpstan-ignore-line
385-
throw new Exception('Unexpected non-scalar value');
385+
throw new \TypeError('Unexpected non-scalar value');
386386
}
387387

388388
try {
@@ -446,7 +446,7 @@ protected function _typecastSaveField(Field $field, $value)
446446
protected function _typecastLoadField(Field $field, $value)
447447
{
448448
// TODO casting optionally to null should be handled by type itself solely
449-
if ($value === '' && in_array($field->type, ['boolean', 'integer', 'float', 'datetime', 'date', 'time', 'json', 'object'], true)) {
449+
if ($value === '' && in_array($field->type, ['boolean', 'integer', 'float', 'decimal', 'datetime', 'date', 'time', 'json', 'object'], true)) {
450450
return null;
451451
}
452452

src/Persistence/Sql/Connection.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ abstract class Connection
4545
/**
4646
* @param array<string, mixed> $defaults
4747
*/
48-
public function __construct(array $defaults = [])
48+
protected function __construct(array $defaults = [])
4949
{
5050
$this->setDefaults($defaults);
5151
}
@@ -177,7 +177,7 @@ public static function resolveConnectionClass(string $driverName): string
177177
}
178178

179179
/**
180-
* Connect to database and return connection class.
180+
* Connect to database and return connection instance.
181181
*
182182
* @param string|array<string, string>|DbalConnection|DbalDriverConnection $dsn
183183
* @param string|null $user

src/Persistence/Sql/Expression.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,15 @@ public function getDebugQuery(): string
445445
{
446446
[$sql, $params] = $this->render();
447447

448-
if (class_exists('SqlFormatter')) { // requires optional "jdorn/sql-formatter" package
448+
if (class_exists(\SqlFormatter::class)) { // requires optional "jdorn/sql-formatter" package
449+
\Closure::bind(static function () {
450+
// fix latest/1.2.16 release from 2013-11-28
451+
if (end(\SqlFormatter::$reserved_toplevel) === 'INTERSECT') {
452+
\SqlFormatter::$reserved_toplevel[] = 'OFFSET';
453+
\SqlFormatter::$reserved_toplevel[] = 'FETCH';
454+
}
455+
}, null, \SqlFormatter::class)();
456+
449457
$sql = preg_replace('~ +(?=\n|$)|(?<=:) (?=\w)~', '', \SqlFormatter::format($sql, false));
450458
}
451459

src/Persistence/Sql/Oracle/PlatformTrait.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ public function getCreateAutoincrementSql($name, $table, $start = 1)
7171
$aiSequenceName = $this->getIdentitySequenceName($tableIdentifier->getQuotedName($this), $nameIdentifier->getQuotedName($this));
7272
assert(str_starts_with($sqls[count($sqls) - 1], 'CREATE TRIGGER ' . $aiTriggerName . "\n"));
7373

74-
$conn = new Connection();
7574
$pkSeq = \Closure::bind(fn () => $this->normalizeIdentifier($aiSequenceName), $this, OraclePlatform::class)()->getName();
76-
$sqls[count($sqls) - 1] = $conn->expr(
75+
$sqls[count($sqls) - 1] = (new Expression(
7776
// else branch should be maybe (because of concurrency) put into after update trigger
7877
str_replace('[pk_seq]', '\'' . str_replace('\'', '\'\'', $pkSeq) . '\'', <<<'EOF'
7978
CREATE TRIGGER {{trigger}}
@@ -100,7 +99,7 @@ public function getCreateAutoincrementSql($name, $table, $start = 1)
10099
'pk' => $nameIdentifier->getName(),
101100
'pk_seq' => $pkSeq,
102101
]
103-
)->render()[0];
102+
))->render()[0];
104103

105104
return $sqls;
106105
}

src/Persistence/Sql/Postgresql/PlatformTrait.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ protected function getCreateAutoincrementSql(Table $table, Column $pkColumn): ar
8282

8383
$pkSeqName = $this->getIdentitySequenceName($table->getName(), $pkColumn->getName());
8484

85-
$conn = new Connection();
86-
87-
$sqls[] = $conn->expr(
85+
$sqls[] = (new Expression(
8886
// else branch should be maybe (because of concurrency) put into after update trigger
8987
// with pure nextval instead of setval with a loop like in Oracle trigger
9088
str_replace('[pk_seq]', '\'' . $pkSeqName . '\'', <<<'EOF'
@@ -111,9 +109,9 @@ protected function getCreateAutoincrementSql(Table $table, Column $pkColumn): ar
111109
'pk_seq' => $pkSeqName,
112110
'trigger_func' => $table->getName() . '_AI_FUNC',
113111
]
114-
)->render()[0];
112+
))->render()[0];
115113

116-
$sqls[] = $conn->expr(
114+
$sqls[] = (new Expression(
117115
<<<'EOF'
118116
CREATE TRIGGER {trigger}
119117
BEFORE INSERT OR UPDATE
@@ -126,7 +124,7 @@ protected function getCreateAutoincrementSql(Table $table, Column $pkColumn): ar
126124
'trigger' => $table->getShortestName($table->getNamespaceName()) . '_AI_PK',
127125
'trigger_func' => $table->getName() . '_AI_FUNC',
128126
]
129-
)->render()[0];
127+
))->render()[0];
130128

131129
return $sqls;
132130
}

0 commit comments

Comments
 (0)