-
-
Notifications
You must be signed in to change notification settings - Fork 49
Simplify AutoCaster
#1920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Simplify AutoCaster
#1920
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,7 @@ | |
| type_date, | ||
| type_datetime, | ||
| type_float, | ||
| type_integer, | ||
| type_json, | ||
| type_uuid}; | ||
| type_integer}; | ||
| use Flow\Types\Type\Native\String\StringTypeChecker; | ||
|
|
||
| final readonly class AutoCaster | ||
|
|
@@ -36,24 +34,15 @@ public function cast(mixed $value) : mixed | |
| */ | ||
| private function castArray(array $value) : array | ||
| { | ||
| $keyTypes = []; | ||
| $valueTypes = []; | ||
|
|
||
| foreach ($value as $key => $item) { | ||
| $keyType = get_type($key); | ||
| foreach ($value as $item) { | ||
| $valueType = get_type($item); | ||
| $keyTypes[$keyType->toString()] = $keyType; | ||
| $valueTypes[$valueType->toString()] = $valueType; | ||
| } | ||
|
|
||
| if (isset($valueTypes['integer'], $valueTypes['float']) && \count($valueTypes) === 2) { | ||
| $castedArray = []; | ||
|
|
||
| foreach ($value as $key => $item) { | ||
| $castedArray[$key] = type_float()->cast($item); | ||
| } | ||
|
|
||
| return $castedArray; | ||
| return \array_map(fn ($item) => type_float()->cast($item), $value); | ||
| } | ||
|
|
||
| return $value; | ||
|
|
@@ -79,14 +68,6 @@ private function castToString(string $value) : mixed | |
| return type_boolean()->cast($value); | ||
| } | ||
|
|
||
| if ($typeChecker->isJson()) { | ||
| return type_json()->cast($value); | ||
| } | ||
|
|
||
| if ($typeChecker->isUuid()) { | ||
| return type_uuid()->cast($value); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think those two can be removed, it seems that we are missing tests for them but by removing those lines following code will fail, breaking backward compatibility of caster: <?php
declare(strict_types=1);
use Flow\Types\Type\AutoCaster;
use Symfony\Component\Uid\Uuid;
require_once __DIR__ . '/vendor/autoload.php';
$output = (new AutoCaster())->cast(Uuid::v7()->toString());
dd($output); // this should be Uuid instance, not string |
||
|
|
||
| if ($typeChecker->isDate()) { | ||
| return type_date()->cast($value); | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uhm I think I messed up a bit this
castArray.The only thing it does now is that it narrows array types.
For example when we have array
[1, 2, 3.0]in which we have int's and float, it will narrow the type tofloatas allintcan be represented byfloathowever I don't think this should be responsibility of a class calledAutoCaster😅Probably I was trying to fix some bug and I did it here without properly thinking this through 🤦♂️
I think this is much bigger issue that will need to be solved in separated PR (probably will require some more explicit array types narrowing mechanism) but for now removing key types makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1921