Skip to content

Commit 2f3c148

Browse files
authored
Merge pull request #22 from laudis-technologies/debug
Updated dependencies and code maintainability
2 parents 5134771 + 7b3d7fd commit 2f3c148

File tree

4 files changed

+121
-79
lines changed

4 files changed

+121
-79
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
],
2222
"require": {
2323
"php": "^7.4 || ^8.0",
24-
"youngsource/typed-enum": "^1.1",
24+
"laudis/typed-enum": "^1.1",
2525
"php-http/discovery": "^1.13",
2626
"psr/http-message": "^1.0",
2727
"psr/http-factory": "^1.0",

composer.lock

Lines changed: 74 additions & 75 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ParameterHelper.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,68 @@ public static function asMap(iterable $iterable): Map
3232
}
3333

3434
/**
35-
* @param iterable|scalar|null $value
35+
* @param mixed $value
3636
*
3737
* @return iterable|scalar|stdClass|null
3838
*/
3939
public static function asParameter($value)
40+
{
41+
return self::emptySequenceToArray($value) ??
42+
self::emptyDictionaryToStdClass($value) ??
43+
self::filledIterableToArray($value) ??
44+
self::filterInvalidType($value);
45+
}
46+
47+
/**
48+
* @param mixed $value
49+
*
50+
* @return scalar|null
51+
*/
52+
private static function filterInvalidType($value)
53+
{
54+
if ($value !== null && !is_scalar($value)) {
55+
throw new InvalidArgumentException('Parameters must be iterable, scalar or null');
56+
}
57+
58+
return $value;
59+
}
60+
61+
/**
62+
* @param mixed $value
63+
*/
64+
private static function emptySequenceToArray($value): ?array
4065
{
4166
if ($value instanceof Sequence && $value->count() === 0) {
4267
return [];
4368
}
69+
70+
return null;
71+
}
72+
73+
/**
74+
* @param mixed $value
75+
*/
76+
private static function emptyDictionaryToStdClass($value): ?stdClass
77+
{
4478
if (($value instanceof Map && $value->count() === 0) ||
4579
(is_array($value) && count($value) === 0)
4680
) {
4781
return new stdClass();
4882
}
83+
84+
return null;
85+
}
86+
87+
/**
88+
* @param mixed $value
89+
*/
90+
private static function filledIterableToArray($value): ?array
91+
{
4992
if (is_iterable($value)) {
5093
return self::iterableToArray($value);
5194
}
5295

53-
return $value;
96+
return null;
5497
}
5598

5699
/**
@@ -82,7 +125,6 @@ private static function iterableToArray(iterable $value): array
82125
*/
83126
foreach ($value as $key => $val) {
84127
if (is_int($key) || is_string($key)) {
85-
/** @psalm-suppress MixedAssignment */
86128
$tbr[$key] = self::asParameter($val);
87129
} else {
88130
$msg = 'Iterable parameters must have an integer or string as key values, '.gettype($key).' received.';

tests/Unit/ParameterHelperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ final class ParameterHelperTest extends TestCase
2929
public static function setUpBeforeClass(): void
3030
{
3131
parent::setUpBeforeClass();
32+
/** @psalm-suppress MixedPropertyTypeCoercion */
3233
self::$invalidIterable = new class() implements Iterator {
3334
private bool $initial = true;
3435

0 commit comments

Comments
 (0)