Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2k
fix: TypeError in DataCaster initialization#9738
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
Merged
paulbalandan
merged 4 commits into
codeigniter4:develop
from
neznaika0:fix/datacaster-errorDec 11, 2025
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -28,6 +28,12 @@ | ||
| use CodeIgniter\Entity\Exceptions\CastException; | ||
| use CodeIgniter\Exceptions\InvalidArgumentException; | ||
| /** | ||
| * @phpstan-type cast_handlers array<string, class-string<EntityCastInterface|CastInterface>> | ||
| * | ||
| * @see CodeIgniter\DataCaster\DataCasterTest | ||
| * @see CodeIgniter\Entity\EntityTest | ||
| */ | ||
| final class DataCaster | ||
| { | ||
| /** | ||
| @@ -38,9 +44,9 @@ final class DataCaster | ||
| private array $types = []; | ||
| /** | ||
| * Convert handlers | ||
| * Convert handlers. | ||
| * | ||
| * @var array<string, class-string> [type => classname] | ||
| * @var cast_handlers [type => classname] | ||
| */ | ||
| private array $castHandlers = [ | ||
| 'array' => ArrayCast::class, | ||
| @@ -59,18 +65,18 @@ final class DataCaster | ||
| ]; | ||
| /** | ||
| * @param array<string, class-string>|null $castHandlers Custom convert handlers | ||
| * @param array<string, string>|null $types [field => type] | ||
| * @param object|null $helper Helper object. | ||
| * @param bool $strict Strict mode? Set to false for casts for Entity. | ||
| * @param cast_handlers|null $castHandlers Custom convert handlers | ||
| * @param array<string, string>|null $types [field => type] | ||
| * @param object|null $helper Helper object. | ||
| * @param bool $strict Strict mode? Set to `false` for casts for Entity. | ||
| */ | ||
| public function __construct( | ||
| ?array $castHandlers = null, | ||
| ?array $types = null, | ||
| private readonly ?object $helper = null, | ||
| private readonly bool $strict = true, | ||
| ) { | ||
| $this->castHandlers = array_merge($this->castHandlers, $castHandlers); | ||
| $this->castHandlers = array_merge($this->castHandlers, $castHandlers ?? []); | ||
| if ($types !== null) { | ||
| $this->setTypes($types); | ||
| @@ -113,12 +119,16 @@ public function setTypes(array $types): static | ||
| * Add ? at the beginning of the type (i.e. ?string) to get `null` | ||
| * instead of casting $value when $value is null. | ||
| * | ||
| * @param mixed $value The value to convert | ||
| * @param string $field The field name | ||
| * @param 'get'|'set' $method Allowed to "get" and "set" | ||
| * @param mixed $value The value to convert | ||
| * @param string $field The field name | ||
| * @param string $method Allowed to "get" and "set" | ||
| */ | ||
| public function castAs(mixed $value, string $field, string $method = 'get'): mixed | ||
| { | ||
| if ($method !== 'get' && $method !== 'set') { | ||
samsonasik marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| throw CastException::forInvalidMethod($method); | ||
| } | ||
| // If the type is not defined, return as it is. | ||
| if (! isset($this->types[$field])) { | ||
| return $value; | ||
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
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
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 |
|---|---|---|
| @@ -17,9 +17,6 @@ | ||
| use DateTime; | ||
| use Exception; | ||
| /** | ||
| * Class DatetimeCast | ||
| */ | ||
| class DatetimeCast extends BaseCast | ||
| { | ||
| /** | ||
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
| namespace CodeIgniter\DataCaster; | ||
| use CodeIgniter\Entity\Exceptions\CastException; | ||
| use CodeIgniter\Test\CIUnitTestCase; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| /** | ||
| * @internal | ||
| */ | ||
| #[Group('Others')] | ||
| final class DataCasterTest extends CIUnitTestCase | ||
| { | ||
| public function testCastInvalidMethodException(): void | ||
| { | ||
| $this->expectException(CastException::class); | ||
| $this->expectExceptionMessage('The "add" is invalid cast method, valid methods are: ["get", "set"].'); | ||
| $dataCaster = new DataCaster(); | ||
| $dataCaster->castAs([], 'name', 'add'); | ||
| } | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.