-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Change return type hint of JsonCast::get
method to mixed
#9323
Comments
Tests for mixed values? |
Response to "Tests for Mixed Values?"The suggestion to use
In summary, by adopting |
|
While By virtue of YAGNI, if the use case for widening the return is because the underlying implementation theoretically covers such wide return but realistically no one ever enter into such scenario, I think it is not worth it. |
@neznaika0:You raised a valid point regarding the specific types By leveraging @paulbalandan:I appreciate your thoughtful consideration of the YAGNI principle, and I agree that practicality should guide our decisions. However, it’s worth noting that JSON data, especially when dealing with external APIs or user-generated content, can often be unpredictable. The use of While it might seem that scalar returns are not common within the current context of CI4, one could envision scenarios such as settings storage or configuration management where JSON-encoded scalar values are not only plausible but also practical. For example: Scenarios in a CodeIgniter 4 Model Cast for Key-Value Options:
The current framework usage might not heavily rely on scalar JSON types, adopting a |
You are not giving a valid use case for scenarios where inputs are purely scalar. In your example 1, the JSON values are actually returned as json objects (or json arrays if required). $json = '{"featured_enabled": true}';
// this is an object
$value = JsonCast::get($json);
var_dump($value->feature_enabled); // bool(true) |
Please stop using ChatGPT for giving us examples. Use a redacted version of your real world projects (or needs) where such scenarios to return |
Example:
@paulbalandan, these are not ChatGPT examples. Does that mean one cannot delve into details with examples without chatgpt? It seems like you're not understanding me. |
Hi @ChibuezeAgwuDennis I do not mean to offend you in any way. But to me or maybe other readers that your previous responses are made entirely by chatgpt. I understand if you're not fluent in English and want the AI to help you construct your messages but the way I see it you type in the question and let the AI answer for you and that's it. No changes or anything. |
Now, my question on your example. Why is the cast you set for |
Example in Model:<?php
namespace App\Models;
use CodeIgniter\Model;
class Setting extends Model
{
protected $table = 'settings';
protected $allowedFields = [
'key',
'value',
'type',
];
protected array $casts = [
'value' => 'json',
];
} From the example above, our goal is to allow the My Modification on the Core Code:public static function get(
mixed $value,
array $params = [],
?object $helper = null
): mixed {
if (!is_string($value)) {
self::invalidTypeValueError($value);
}
$associative = in_array('array', $params, true);
$output = ($associative ? [] : new stdClass());
try {
$output = json_decode($value, $associative, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw CastException::forInvalidJsonFormat($e->getCode());
}
return $output;
} I changed the return hint type to I understand if this detail might be overwhelming for some readers, but my aim is to gain a simple yet clear understanding. The current implementation uses While the public static function set(
mixed $value, // Any data type can be passed
array $params = [],
?object $helper = null
): string The I prefer not to modify the core code directly, which is why I'm sharing my suggestions here. If there is a more effective way to store values of any data type, please feel free to suggest it. I am willing to adapt to a better solution. Although I refrain from comparing frameworks, I believe Laravel handles this scenario effectively. However, as a devoted user of CodeIgniter, I am eager to find an optimal solution within this framework as well. |
Thank you for your patience. Now I understand your use case. As |
Description
The
JsonCast::get
method currently has a return type hint ofarray|stdClass
. However, considering the nature of the method, which decodes JSON strings into PHP types, the return value can theoretically be any PHP type, including scalar values, arrays, orstdClass
objects.Suggested Change
Change the return type hint of the
get
method fromarray|stdClass
tomixed
, to accurately reflect the potential return types.Code Example
Rationale
This change will prevent any type mismatches and make the method's expected behavior clearer to developers using the framework.
Additional Context
If the method is used with a JSON string representing a scalar value (like a string or number), the current type hints may cause confusion or type errors.
References
Relevant section in the codebase:
Thank you for considering this improvement.
The text was updated successfully, but these errors were encountered: