Skip to content

Commit bdddacb

Browse files
committed
Use more PHP8.1 feature
1 parent 54b8a95 commit bdddacb

12 files changed

+202
-160
lines changed

src/AbstractExpression.php

+14-17
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,17 @@ abstract class AbstractExpression implements ExpressionInterface {
55
*
66
* @var string[]
77
*/
8-
protected array $allowedTypes = [
9-
self::TYPE_IDENTIFIER,
10-
self::TYPE_LITERAL,
11-
self::TYPE_SELECT,
12-
self::TYPE_VALUE
13-
];
148
/**
159
* Normalize Argument
1610
*
1711
* @param mixed $argument
18-
* @param string $defaultType
12+
* @param ExpressionType $defaultType
1913
*
2014
* @return array
2115
*
2216
* @throws Exception\InvalidArgumentException
2317
*/
24-
protected function normalizeArgument(mixed $argument, string $defaultType = self::TYPE_VALUE): array {
18+
protected function normalizeArgument(mixed $argument, ExpressionType $defaultType = self::TYPE_VALUE): array {
2519
if ($argument instanceof ExpressionInterface || $argument instanceof SqlInterface) {
2620
return $this->buildNormalizedArgument($argument, self::TYPE_VALUE);
2721
}
@@ -39,7 +33,7 @@ protected function normalizeArgument(mixed $argument, string $defaultType = self
3933

4034
$key = key($argument);
4135

42-
if (is_integer($key) && ! in_array($value, $this->allowedTypes)) {
36+
if (is_integer($key) && ! $value instanceof ExpressionType) {
4337
return $this->buildNormalizedArgument($value, $defaultType);
4438
}
4539

@@ -55,8 +49,16 @@ protected function normalizeArgument(mixed $argument, string $defaultType = self
5549
}
5650

5751
throw new Exception\InvalidArgumentException(sprintf('$argument should be %s or %s or %s or %s or %s or %s or %s, "%s" given',
58-
'null', 'scalar', 'array', 'Sql\ExpressionInterface', 'Sql\Sql\SqlInterface', 'BackedEnum', 'Swango\Model\IdIndexedModel',
59-
is_object($argument) ? get_class($argument) : gettype($argument)));
52+
'null',
53+
'scalar',
54+
'array',
55+
'Sql\ExpressionInterface',
56+
'Sql\Sql\SqlInterface',
57+
'BackedEnum',
58+
'Swango\Model\IdIndexedModel',
59+
is_object($argument) ? get_class($argument) : gettype($argument)
60+
)
61+
);
6062
}
6163
/**
6264
*
@@ -67,12 +69,7 @@ protected function normalizeArgument(mixed $argument, string $defaultType = self
6769
*
6870
* @throws Exception\InvalidArgumentException
6971
*/
70-
private function buildNormalizedArgument(mixed $argument, string $argumentType): array {
71-
if (! in_array($argumentType, $this->allowedTypes)) {
72-
throw new Exception\InvalidArgumentException(sprintf('Argument type should be in array(%s)',
73-
implode(',', $this->allowedTypes)));
74-
}
75-
72+
private function buildNormalizedArgument(mixed $argument, ExpressionType $argumentType): array {
7673
return [
7774
$argument,
7875
$argumentType

src/AbstractSql.php

+47-52
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,17 @@ protected function renderTable(string $table, string $alias = null): string {
6060
}
6161
/**
6262
*
63-
* @staticvar int $runtimeExpressionPrefix
6463
* @param ExpressionInterface $expression
6564
* @param PlatformInterface $platform
6665
* @param null|string $namedParameterPrefix
6766
* @return string
6867
* @throws Exception\RuntimeException
6968
*/
70-
protected function processExpression(ExpressionInterface $expression, PlatformInterface $platform, ?string $namedParameterPrefix = null): string {
69+
protected function processExpression(ExpressionInterface $expression,
70+
PlatformInterface $platform,
71+
?string $namedParameterPrefix = null): string {
7172
$namedParameterPrefix = ! $namedParameterPrefix ? $namedParameterPrefix : $this->processInfo['paramPrefix'] .
7273
$namedParameterPrefix;
73-
// static counter for the number of times this method was invoked across the PHP runtime
74-
static $runtimeExpressionPrefix = 0;
7574

7675
$namedParameterPrefix = preg_replace('/\s/', '__', $namedParameterPrefix);
7776

@@ -80,57 +79,46 @@ protected function processExpression(ExpressionInterface $expression, PlatformIn
8079
// initialize variables
8180
$parts = $expression->getExpressionData();
8281

83-
if (! isset($this->instanceParameterIndex[$namedParameterPrefix])) {
84-
$this->instanceParameterIndex[$namedParameterPrefix] = 1;
85-
}
82+
$this->instanceParameterIndex[$namedParameterPrefix] ??= 1;
8683

8784
$expressionParamIndex = &$this->instanceParameterIndex[$namedParameterPrefix];
8885

8986
foreach ($parts as $part) {
90-
// #7407: use $expression->getExpression() to get the unescaped
91-
// version of the expression
92-
if (is_string($part) && $expression instanceof Expression) {
93-
$sql .= $expression->getExpression();
94-
continue;
95-
}
96-
97-
// If it is a string, simply tack it onto the return sql
87+
//
88+
// If it is a string, use $expression->getExpression() to get the unescaped, or simply tack it onto the return sql
9889
// "specification" string
9990
if (is_string($part)) {
100-
$sql .= $part;
91+
$sql .= $expression instanceof Expression ? $expression->getExpression() : $part;
10192
continue;
10293
}
10394

10495
if (! is_array($part)) {
105-
throw new Exception\RuntimeException('Elements returned from getExpressionData() array must be a string or array.');
96+
throw new Exception\RuntimeException('Elements returned from getExpressionData() array must be a string or array.'
97+
);
10698
}
10799

108-
// Process values and types (the middle and last position of the
109-
// expression data)
100+
// Process values and types (the middle and last position of the expression data)
110101
$values = $part[1];
111-
$types = isset($part[2]) ? $part[2] : [];
112-
foreach ($values as $vIndex => $value) {
113-
if (! isset($types[$vIndex])) {
114-
continue;
115-
}
116-
$type = $types[$vIndex];
117-
if ($value instanceof Select) {
118-
// process sub-select
119-
$values[$vIndex] = '(' . $this->processSubSelect($value, $platform) . ')';
120-
} elseif ($value instanceof ExpressionInterface) {
121-
// recursive call to satisfy nested expressions
122-
$values[$vIndex] = $this->processExpression($value, $platform,
123-
$namedParameterPrefix . $vIndex . 'subpart');
124-
} elseif ($type == ExpressionInterface::TYPE_IDENTIFIER) {
125-
$values[$vIndex] = $platform->quoteIdentifierInFragment($value);
126-
} elseif ($type == ExpressionInterface::TYPE_VALUE) {
127-
// if not a preparable statement, simply quote the value and move on
128-
$values[$vIndex] = $platform->quoteValue($value);
129-
} elseif ($type == ExpressionInterface::TYPE_LITERAL) {
130-
$values[$vIndex] = $value;
131-
}
102+
if (! empty($part[2])) {
103+
$types = $part[2];
104+
foreach ($values as $vIndex => &$value)
105+
if (isset($types[$vIndex])) {
106+
$value = match (true) {
107+
$value instanceof Select => '(' . $this->processSubSelect($value, $platform) . ')',
108+
$value instanceof ExpressionInterface => $this->processExpression($value,
109+
$platform,
110+
$namedParameterPrefix . $vIndex . 'subpart'
111+
),
112+
default => match ($types[$vIndex]) {
113+
ExpressionInterface::TYPE_IDENTIFIER => $platform->quoteIdentifierInFragment($value),
114+
ExpressionInterface::TYPE_VALUE => $platform->quoteValue($value),
115+
ExpressionInterface::TYPE_LITERAL => $value,
116+
default => $value
117+
}
118+
};
119+
}
120+
unset($value);
132121
}
133-
134122
// After looping the values, interpolate them into the sql string
135123
// (they might be placeholder names, or values)
136124
$sql .= vsprintf($part[0], $values);
@@ -163,7 +151,8 @@ protected function createSqlFromSpecificationAndParameters(string|array $specifi
163151
}
164152

165153
if (! isset($specificationString)) {
166-
throw new Exception\RuntimeException('A number of parameters was found that is not supported by this specification');
154+
throw new Exception\RuntimeException('A number of parameters was found that is not supported by this specification'
155+
);
167156
}
168157

169158
$topParameters = [];
@@ -179,7 +168,9 @@ protected function createSqlFromSpecificationAndParameters(string|array $specifi
179168

180169
if (! isset($paramSpecs[$position][$ppCount])) {
181170
throw new Exception\RuntimeException(sprintf('A number of parameters (%d) was found that is not supported by this specification',
182-
$ppCount));
171+
$ppCount
172+
)
173+
);
183174
}
184175
if (is_string($multiParamsForPosition)) {
185176
$multiParamValues[] = sprintf($paramSpecs[$position][$ppCount], $multiParamsForPosition);
@@ -192,7 +183,9 @@ protected function createSqlFromSpecificationAndParameters(string|array $specifi
192183
$ppCount = count($paramsForPosition);
193184
if (! isset($paramSpecs[$position][$ppCount])) {
194185
throw new Exception\RuntimeException(sprintf('A number of parameters (%d) was found that is not supported by this specification',
195-
$ppCount));
186+
$ppCount
187+
)
188+
);
196189
}
197190
$topParameters[] = vsprintf($paramSpecs[$position][$ppCount], $paramsForPosition);
198191
} else {
@@ -245,16 +238,17 @@ protected function processJoin(Join $joins, PlatformInterface $platform): ?array
245238
$platform->getIdentifierSeparator() : '') . $platform->quoteIdentifier($joinName[0]);
246239
} elseif ($joinName instanceof Select) {
247240
$joinName = '(' . $this->processSubSelect($joinName, $platform) . ')';
248-
} elseif (is_string($joinName) || (is_object($joinName) && is_callable([
249-
$joinName,
250-
'__toString'
251-
]))) {
241+
} elseif (is_string($joinName) || (is_object($joinName) && method_exists($joinName, '__toString'))) {
252242
if ($platform->shoueldQuoteOtherTable()) {
253243
$joinName = $platform->quoteIdentifier($joinName);
244+
} else {
245+
$joinName = (string)$joinName;
254246
}
255247
} else {
256248
throw new Exception\InvalidArgumentException(sprintf('Join name expected to be Expression|TableIdentifier|Select|string, "%s" given',
257-
gettype($joinName)));
249+
gettype($joinName)
250+
)
251+
);
258252
}
259253

260254
$joinSpecArgArray[$j] = [
@@ -293,7 +287,9 @@ protected function processJoin(Join $joins, PlatformInterface $platform): ?array
293287
* @param null|string $namedParameterPrefix
294288
* @return string
295289
*/
296-
protected function resolveColumnValue(mixed $column, PlatformInterface $platform, ?string $namedParameterPrefix = null): string {
290+
protected function resolveColumnValue(mixed $column,
291+
PlatformInterface $platform,
292+
?string $namedParameterPrefix = null): string {
297293
$namedParameterPrefix = ! $namedParameterPrefix ? $namedParameterPrefix : $this->processInfo['paramPrefix'] .
298294
$namedParameterPrefix;
299295
$isIdentifier = false;
@@ -324,8 +320,7 @@ protected function resolveColumnValue(mixed $column, PlatformInterface $platform
324320
}
325321
$column = (string)$column;
326322

327-
return $isIdentifier ? $fromTable .
328-
$platform->quoteIdentifierInFragment($column) : $platform->quoteValue($column);
323+
return $isIdentifier ? $fromTable . $platform->quoteIdentifierInFragment($column) : $platform->quoteValue($column);
329324
}
330325
/**
331326
*

src/Expression.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function getExpressionData(): array {
111111
}
112112

113113
foreach ($parameters as $parameter) {
114-
list($values[], $types[]) = $this->normalizeArgument($parameter, self::TYPE_VALUE);
114+
[$values[], $types[]] = $this->normalizeArgument($parameter, self::TYPE_VALUE);
115115
}
116116
return [
117117
[

src/ExpressionInterface.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
namespace Sql;
33
interface ExpressionInterface {
4-
const TYPE_IDENTIFIER = 'identifier';
5-
const TYPE_VALUE = 'value';
6-
const TYPE_LITERAL = 'literal';
7-
const TYPE_SELECT = 'select';
4+
const TYPE_IDENTIFIER = ExpressionType::IDENTIFIER;
5+
const TYPE_VALUE = ExpressionType::VALUE;
6+
const TYPE_LITERAL = ExpressionType::LITERAL;
7+
const TYPE_SELECT = ExpressionType::SELECT;
88
public function getExpressionData(): array;
99
}

src/ExpressionType.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace Sql;
3+
enum ExpressionType {
4+
case IDENTIFIER;
5+
case VALUE;
6+
case LITERAL;
7+
case SELECT;
8+
public function isForOperator(): bool {
9+
return $this === self::IDENTIFIER || $this === self::VALUE;
10+
}
11+
}

src/Predicate/Between.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ public function getSpecification(): string {
103103
* @return array
104104
*/
105105
public function getExpressionData(): array {
106-
list($values[], $types[]) = $this->normalizeArgument($this->identifier, self::TYPE_IDENTIFIER);
107-
list($values[], $types[]) = $this->normalizeArgument($this->minValue, self::TYPE_VALUE);
108-
list($values[], $types[]) = $this->normalizeArgument($this->maxValue, self::TYPE_VALUE);
106+
[$values[], $types[]] = $this->normalizeArgument($this->identifier, self::TYPE_IDENTIFIER);
107+
[$values[], $types[]] = $this->normalizeArgument($this->minValue, self::TYPE_VALUE);
108+
[$values[], $types[]] = $this->normalizeArgument($this->maxValue, self::TYPE_VALUE);
109109
return [
110110
[
111111
$this->getSpecification(),

src/Predicate/In.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function getExpressionData(): array {
9090
$types[] = self::TYPE_VALUE;
9191
} else {
9292
foreach ($values as $argument) {
93-
list($replacements[], $types[]) = $this->normalizeArgument($argument, self::TYPE_VALUE);
93+
[$replacements[], $types[]] = $this->normalizeArgument($argument, self::TYPE_VALUE);
9494
}
9595
$countValues = count($values);
9696
$valuePlaceholders = $countValues > 0 ? array_fill(0, $countValues, '%s') : [];

src/Predicate/Like.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public function getSpecification(): string {
6868
* @return array
6969
*/
7070
public function getExpressionData(): array {
71-
list($values[], $types[]) = $this->normalizeArgument($this->identifier, self::TYPE_IDENTIFIER);
72-
list($values[], $types[]) = $this->normalizeArgument($this->like, self::TYPE_VALUE);
71+
[$values[], $types[]] = $this->normalizeArgument($this->identifier, self::TYPE_IDENTIFIER);
72+
[$values[], $types[]] = $this->normalizeArgument($this->like, self::TYPE_VALUE);
7373
return [
7474
[
7575
$this->specification,

0 commit comments

Comments
 (0)