Skip to content

Commit

Permalink
Further improve type guessing
Browse files Browse the repository at this point in the history
  • Loading branch information
iquito committed May 4, 2022
1 parent 4348cb5 commit d2748de
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
6 changes: 5 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="v4.15.0@a1b5e489e6fcebe40cb804793d964e99fc347820">
<files psalm-version="4.23.0@f1fe6ff483bf325c803df9f510d09a03fd796f88">
<file src="src/MultiRepositoryReadOnly.php">
<MissingConstructor occurrences="2">
<code>$db</code>
Expand All @@ -8,6 +8,10 @@
<PossiblyInvalidArgument occurrences="1">
<code>$sanitizedOptions['query']</code>
</PossiblyInvalidArgument>
<RedundantCondition occurrences="2">
<code>\strval(\floatval($value)) !== $value</code>
<code>\strval(\intval($value)) !== $value</code>
</RedundantCondition>
</file>
<file src="src/RepositoryReadOnly.php">
<ArgumentTypeCoercion occurrences="3">
Expand Down
16 changes: 14 additions & 2 deletions src/MultiRepositoryReadOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,16 +781,28 @@ private function processSelectResult(
continue;
}

// Test boolean and numeric values to make sure we do not lose information
if ($selectTypes[$key] === 'ubool') {
// Only accept 0 and 1 - all other values are not necessarily boolean
if ($value === '0' || $value === '1') {
$selectTypes[$key] = 'bool';
} else {
$selectTypes[$key] = 'string';
}
} elseif ($selectTypes[$key] === 'uint' || $selectTypes[$key] === 'ufloat') {
if (!\is_numeric($value)) {
// Non-numeric values are kept as string
if (
!\is_numeric($value)
|| \strval(\floatval($value)) !== $value
) {
$selectTypes[$key] = 'string';
} else {
} elseif (
// Numeric values where we lose some information when converting to integer are kept as a string
$selectTypes[$key] === 'uint'
&& \strval(\intval($value)) !== $value
) {
$selectTypes[$key] = 'string';
} else { // No information loss detected, use int or float type
$selectTypes[$key] = \substr($selectTypes[$key], 1);
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/MultiRepositoryReadOnlyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ function ($p) {
'updateMinusCreated' => ':ticket.lastUpdate:-:ticket.createDate:',
'floaty' => ':ticket.floaty:',
'booleany' => ':ticket.open:',
'integery' => ':ticket.lastUpdate:/2',
'updateCreatedConcat' => 'CONCAT(:ticket.lastUpdate:,:ticket.createDate:)',
],
'tables' => [
Expand Down Expand Up @@ -825,6 +826,7 @@ public function testComplicatedQuery(): void
'updateMinusCreated' => '5',
'floaty' => '9.5',
'booleany' => '1',
'integery' => '1',
'updateCreatedConcat' => '5',
],
[
Expand All @@ -837,6 +839,7 @@ public function testComplicatedQuery(): void
'updateMinusCreated' => '8',
'floaty' => '2022-05-04',
'booleany' => '0',
'integery' => '5',
'updateCreatedConcat' => '5',
],
[
Expand All @@ -849,6 +852,7 @@ public function testComplicatedQuery(): void
'updateMinusCreated' => '53',
'floaty' => '3.3',
'booleany' => '1',
'integery' => '1.5',
'updateCreatedConcat' => '5',
],
];
Expand All @@ -865,6 +869,7 @@ public function testComplicatedQuery(): void
'updateMinusCreated' => 5,
'floaty' => 9.5,
'booleany' => true,
'integery' => 1,
'updateCreatedConcat' => '5',
],
[
Expand All @@ -877,6 +882,7 @@ public function testComplicatedQuery(): void
'updateMinusCreated' => 8,
'floaty' => '2022-05-04',
'booleany' => false,
'integery' => 5,
'updateCreatedConcat' => '5',
],
[
Expand All @@ -889,6 +895,7 @@ public function testComplicatedQuery(): void
'updateMinusCreated' => 53,
'floaty' => 3.3,
'booleany' => true,
'integery' => '1.5',
'updateCreatedConcat' => '5',
],
];
Expand All @@ -910,6 +917,8 @@ public function testComplicatedQuery(): void
$this->db->quoteIdentifier('floaty'),
'(' . $this->db->quoteIdentifier('ticket.ticket_open') . ') AS ' .
$this->db->quoteIdentifier('booleany'),
'(' . $this->db->quoteIdentifier('ticket.last_update') . '/2) AS ' .
$this->db->quoteIdentifier('integery'),
'(CONCAT(' . $this->db->quoteIdentifier('ticket.last_update') . ',' .
$this->db->quoteIdentifier('ticket.create_date') . ')) AS ' .
$this->db->quoteIdentifier('updateCreatedConcat'),
Expand Down

0 comments on commit d2748de

Please sign in to comment.