Skip to content

Commit

Permalink
Improve flattened fields with types
Browse files Browse the repository at this point in the history
- Coerce values that can sensibly be coerced
- Convert the functions into a trait so they can
  be shared between the three select classes
- Adjust tests accordingly
  • Loading branch information
iquito committed May 10, 2020
1 parent 3ee52ad commit cec6583
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 105 deletions.
40 changes: 40 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
parameters:
ignoreErrors:
-
message: "#^Method Squirrel\\\\Entities\\\\Action\\\\MultiSelectEntries\\:\\:getFlattenedIntegerFields\\(\\) should return array\\<int\\> but returns array\\<bool\\|float\\|int\\|string\\|null\\>\\.$#"
count: 1
path: src/Action/MultiSelectEntries.php

-
message: "#^Method Squirrel\\\\Entities\\\\Action\\\\MultiSelectEntries\\:\\:getFlattenedFloatFields\\(\\) should return array\\<float\\> but returns array\\<bool\\|float\\|int\\|string\\|null\\>\\.$#"
count: 1
path: src/Action/MultiSelectEntries.php

-
message: "#^Method Squirrel\\\\Entities\\\\Action\\\\MultiSelectEntries\\:\\:getFlattenedStringFields\\(\\) should return array\\<string\\> but returns array\\<bool\\|float\\|int\\|string\\|null\\>\\.$#"
count: 1
path: src/Action/MultiSelectEntries.php

-
message: "#^Method Squirrel\\\\Entities\\\\Action\\\\MultiSelectEntries\\:\\:getFlattenedBooleanFields\\(\\) should return array\\<bool\\> but returns array\\<bool\\|float\\|int\\|string\\|null\\>\\.$#"
count: 1
path: src/Action/MultiSelectEntries.php

-
message: "#^Method Squirrel\\\\Entities\\\\Action\\\\MultiSelectEntriesFreeform\\:\\:getFlattenedIntegerFields\\(\\) should return array\\<int\\> but returns array\\<bool\\|float\\|int\\|string\\|null\\>\\.$#"
count: 1
path: src/Action/MultiSelectEntriesFreeform.php

-
message: "#^Method Squirrel\\\\Entities\\\\Action\\\\MultiSelectEntriesFreeform\\:\\:getFlattenedFloatFields\\(\\) should return array\\<float\\> but returns array\\<bool\\|float\\|int\\|string\\|null\\>\\.$#"
count: 1
path: src/Action/MultiSelectEntriesFreeform.php

-
message: "#^Method Squirrel\\\\Entities\\\\Action\\\\MultiSelectEntriesFreeform\\:\\:getFlattenedStringFields\\(\\) should return array\\<string\\> but returns array\\<bool\\|float\\|int\\|string\\|null\\>\\.$#"
count: 1
path: src/Action/MultiSelectEntriesFreeform.php

-
message: "#^Method Squirrel\\\\Entities\\\\Action\\\\MultiSelectEntriesFreeform\\:\\:getFlattenedBooleanFields\\(\\) should return array\\<bool\\> but returns array\\<bool\\|float\\|int\\|string\\|null\\>\\.$#"
count: 1
path: src/Action/MultiSelectEntriesFreeform.php

-
message: "#^Method Squirrel\\\\Entities\\\\Action\\\\SelectEntries\\:\\:getFlattenedIntegerFields\\(\\) should return array\\<int\\> but returns array\\<bool\\|float\\|int\\|string\\|null\\>\\.$#"
count: 1
Expand Down
8 changes: 1 addition & 7 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="3.11.2@d470903722cfcbc1cd04744c5491d3e6d13ec3d9">
<file src="src/Action/SelectEntries.php">
<InvalidReturnStatement occurrences="4">
<code>$values</code>
<code>$values</code>
<code>$values</code>
<code>$values</code>
</InvalidReturnStatement>
<file src="src/Action/FlattenedFieldsWithTypeTrait.php">
<InvalidReturnType occurrences="4">
<code>int[]</code>
<code>float[]</code>
Expand Down
136 changes: 136 additions & 0 deletions src/Action/FlattenedFieldsWithTypeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace Squirrel\Entities\Action;

use Squirrel\Debug\Debug;
use Squirrel\Queries\Exception\DBInvalidOptionException;

trait FlattenedFieldsWithTypeTrait
{
/**
* @return int[]
*/
public function getFlattenedIntegerFields(): array
{
$values = $this->getFlattenedFields();

foreach ($values as $key => $value) {
// Convert non-int values which do not change when type casted
if (
!\is_integer($value)
&& \strval(\intval($value)) === \strval($value)
) {
$values[$key] = \intval($value);
continue;
}

if (!\is_int($value)) {
throw Debug::createException(
DBInvalidOptionException::class,
[ActionInterface::class],
'Flattened integers requested, but not all values were integers'
);
}
}

return $values;
}

/**
* @return float[]
*/
public function getFlattenedFloatFields(): array
{
$values = $this->getFlattenedFields();

foreach ($values as $key => $value) {
if (\is_int($value)) {
$values[$key] = \floatval($value);
continue;
}

// Convert non-float values which do not change when type casted
if (
!\is_float($value)
&& \strval(\floatval($value)) === \strval($value)
) {
$values[$key] = \floatval($value);
continue;
}

if (!\is_float($value)) {
throw Debug::createException(
DBInvalidOptionException::class,
[ActionInterface::class],
'Flattened floats requested, but not all values were floats'
);
}
}

return $values;
}

/**
* @return string[]
*/
public function getFlattenedStringFields(): array
{
$values = $this->getFlattenedFields();

foreach ($values as $key => $value) {
// Integers and floats can be converted to strings without problems
if (
\is_int($value)
|| \is_float($value)
) {
$values[$key] = \strval($value);
continue;
}

if (!\is_string($value)) {
throw Debug::createException(
DBInvalidOptionException::class,
[ActionInterface::class],
'Flattened strings requested, but not all values were strings'
);
}
}

return $values;
}

/**
* @return bool[]
*/
public function getFlattenedBooleanFields(): array
{
$values = $this->getFlattenedFields();

foreach ($values as $key => $value) {
// Convert non-boolean values which can reasonably be converted to boolean
if (
$value === 0
|| $value === '0'
) {
$values[$key] = false;
continue;
} elseif (
$value === 1
|| $value === '1'
) {
$values[$key] = true;
continue;
}

if (!\is_bool($value)) {
throw Debug::createException(
DBInvalidOptionException::class,
[ActionInterface::class],
'Flattened booleans requested, but not all values were booleans'
);
}
}

return $values;
}
}
4 changes: 3 additions & 1 deletion src/Action/MultiSelectEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
class MultiSelectEntries implements ActionInterface, \IteratorAggregate
{
use FlattenedFieldsWithTypeTrait;

private MultiRepositoryReadOnlyInterface $queryHandler;

/**
Expand Down Expand Up @@ -191,7 +193,7 @@ public function getOneEntry(): ?array
/**
* Execute SELECT query and return the fields as a list of values
*
* @return array<int,mixed>
* @return array<bool|int|float|string|null>
*/
public function getFlattenedFields(): array
{
Expand Down
2 changes: 2 additions & 0 deletions src/Action/MultiSelectEntriesFreeform.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
class MultiSelectEntriesFreeform implements ActionInterface, \IteratorAggregate
{
use FlattenedFieldsWithTypeTrait;

private MultiRepositoryReadOnlyInterface $queryHandler;

/**
Expand Down
89 changes: 2 additions & 87 deletions src/Action/SelectEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Squirrel\Entities\Action;

use Squirrel\Debug\Debug;
use Squirrel\Entities\RepositoryReadOnlyInterface;
use Squirrel\Queries\Exception\DBInvalidOptionException;

/**
* Select query builder as a fluent object - build query and return object(s) or flattened fields
Expand All @@ -13,6 +11,8 @@
*/
class SelectEntries implements ActionInterface, \IteratorAggregate
{
use FlattenedFieldsWithTypeTrait;

private RepositoryReadOnlyInterface $repository;

/**
Expand Down Expand Up @@ -157,91 +157,6 @@ public function getFlattenedFields(): array
]);
}

/**
* @return int[]
*/
public function getFlattenedIntegerFields(): array
{
$values = $this->getFlattenedFields();

foreach ($values as $value) {
if (!\is_int($value)) {
throw Debug::createException(
DBInvalidOptionException::class,
[ActionInterface::class],
'Flattened integers requested, but not all values were integers'
);
}
}

return $values;
}

/**
* @return float[]
*/
public function getFlattenedFloatFields(): array
{
$values = $this->getFlattenedFields();

foreach ($values as $key => $value) {
if (\is_int($value)) {
$values[$key] = \floatval($value);
continue;
}

if (!\is_float($value)) {
throw Debug::createException(
DBInvalidOptionException::class,
[ActionInterface::class],
'Flattened floats requested, but not all values were floats'
);
}
}

return $values;
}

/**
* @return string[]
*/
public function getFlattenedStringFields(): array
{
$values = $this->getFlattenedFields();

foreach ($values as $value) {
if (!\is_string($value)) {
throw Debug::createException(
DBInvalidOptionException::class,
[ActionInterface::class],
'Flattened strings requested, but not all values were strings'
);
}
}

return $values;
}

/**
* @return bool[]
*/
public function getFlattenedBooleanFields(): array
{
$values = $this->getFlattenedFields();

foreach ($values as $value) {
if (!\is_bool($value)) {
throw Debug::createException(
DBInvalidOptionException::class,
[ActionInterface::class],
'Flattened booleans requested, but not all values were booleans'
);
}
}

return $values;
}

public function getIterator(): SelectIterator
{
return new SelectIterator($this->repository, [
Expand Down
Loading

0 comments on commit cec6583

Please sign in to comment.