-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add InteractsWithData trait and enhance Fluent class #7476
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
Conversation
Introduces the InteractsWithData trait to provide advanced data interaction methods. Updates the Fluent class to use this trait and adds new methods for attribute access, mutation, and data retrieval. Also adds the enum_value helper to Functions.php for handling enum values.
This commit adds new test cases for the Fluent class, covering methods such as getIterator, set, fill, value, scope, all, data interaction methods, conditional methods (whenHas, whenFilled, whenMissing), date parsing, and enum handling. These tests improve coverage and ensure correct behavior for a wide range of Fluent features.
Replaced fully qualified class names with imported classes for ArrayIterator, Carbon, Collection, and Stringable in assertions. Also standardized array formatting by adding trailing commas for consistency and readability.
Documented the addition of the `InteractsWithData` trait and enhancements to the `Fluent` class in the changelog for version 3.1.61.
PR #7476 代码审查报告📋 变更概述此PR引入了 InteractsWithData trait 来增强 Fluent 类的数据交互能力,添加了新的属性访问、转换和数据检索方法,同时新增 enum_value 辅助函数处理枚举值。 ✅ 主要改进点架构设计
新增功能
测试覆盖
|
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.
Pull Request Overview
This PR introduces the InteractsWithData trait to provide a comprehensive set of data interaction methods and enhances the Fluent class to use this trait. It also adds a new enum_value helper function for handling enum values in data operations.
- Adds
InteractsWithDatatrait with methods for checking data existence, validation, type conversion, and data manipulation - Enhances
Fluentclass with new methods likeset,fill,value,scope, andallwhile integrating the trait - Adds
enum_valuehelper function to handle enum value extraction
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/support/src/Traits/InteractsWithData.php | New trait providing comprehensive data interaction methods including existence checks, type conversions, and data manipulation |
| src/support/src/Fluent.php | Enhanced with new methods and integration of InteractsWithData trait |
| src/support/src/Functions.php | Added enum_value helper function for extracting scalar values from enums |
| src/support/tests/FluentTest.php | Comprehensive test coverage for all new methods and functionality |
| CHANGELOG-3.1.md | Documents the addition of InteractsWithData trait and Fluent enhancements |
Comments suppressed due to low confidence (1)
src/support/src/Traits/InteractsWithData.php:17
- [nitpick] Aliasing
CarbonasDatecan be confusing since there's already a native PHPDateclass and the importedCarbon\Carbonclass. Consider using a more specific alias likeHyperfCarbonor removing the alias entirely.
use Hyperf\Carbon\Carbon as Date;
| return value($default); | ||
| } | ||
|
|
||
| return $enumClass::tryFrom($this->data($key)) ?: value($default); |
Copilot
AI
Aug 2, 2025
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.
The ?: value($default) operator will call value($default) even when tryFrom returns a valid enum with a falsy value (like 0 or empty string). Use null coalescing operator ?? value($default) instead to only use the default when tryFrom returns null.
| return $enumClass::tryFrom($this->data($key)) ?: value($default); | |
| return $enumClass::tryFrom($this->data($key)) ?? value($default); |
| */ | ||
| public function get($key, $default = null) | ||
| { | ||
| return data_get($this->attributes, $key, $default); |
Copilot
AI
Aug 2, 2025
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.
The get method implementation has been completely changed from using array_key_exists to using data_get, but the original implementation logic has been moved to the value method. This creates confusion about which method should be used for what purpose. Consider keeping the original get logic or clearly documenting the difference between get and value.
| return data_get($this->attributes, $key, $default); | |
| if (array_key_exists($key, $this->attributes)) { | |
| return $this->attributes[$key]; | |
| } | |
| return $default; |
Introduces the InteractsWithData trait to provide advanced data interaction methods. Updates the Fluent class to use this trait and adds new methods for attribute access, mutation, and data retrieval. Also adds the enum_value helper to Functions.php for handling enum values.