Skip to content

Conversation

@huangdijia
Copy link
Member

Summary

This PR adds the push method to the Hyperf\Collection\Arr class, following the implementation from Laravel PR #56632.

Changes

  • Added push method to Arr class that allows pushing items into arrays using dot notation
  • Added comprehensive test coverage for the new method

Features

  • Push single or multiple values to nested arrays using dot notation
  • Push to root level arrays (null key)
  • Proper exception handling when target is not an array
  • API compatibility with Laravel's implementation

Usage Examples

$array = [];
Arr::push($array, 'office.furniture', 'Desk');
// Result: ['office' => ['furniture' => ['Desk']]]

Arr::push($array, 'office.furniture', 'Chair', 'Lamp');
// Result: ['office' => ['furniture' => ['Desk', 'Chair', 'Lamp']]]

Arr::push($array, null, 'Chris', 'Nuno');
// Result: ['Chris', 'Nuno']

Tests

All tests are passing, including edge cases and exception scenarios.

- Add push method to Hyperf\Collection\Arr based on Laravel PR #56632
- Allows pushing items into arrays using dot notation
- Includes comprehensive test coverage for nested arrays and edge cases
- Matches Laravel's implementation and API
@huangdijia huangdijia force-pushed the feature/add-push-method-to-arr branch from 4dd62e2 to f3079b1 Compare August 18, 2025 08:17
Copy link
Member Author

@huangdijia huangdijia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 代码审查结果

✅ 总体评价

这是一个高质量的 PR!功能实现正确,测试覆盖全面,代码风格符合项目规范。

💪 优点

  1. 类型安全: 使用了现代 PHP 的联合类型和严格类型检查
  2. 测试完整: 覆盖了正常流程、边界情况和异常情况
  3. 架构合理: 通过复用现有的 get()set() 方法实现功能
  4. 兼容性好: 与 Laravel 的实现保持一致
  5. CI/CD: 所有 65 项检查全部通过 ✅

📝 小建议

array() 方法的注释可以更准确一些:

/**
 * Get an array value from an array using "dot" notation, ensuring the result is an array.
 */

当前的注释 "Get an array item from an array" 有些模糊,建议强调这个方法的核心作用是确保返回值是数组类型。

🧪 测试评价

测试用例设计得很好:

  • ✅ 嵌套数组的点记法推送
  • ✅ 多值推送
  • ✅ 根级数组推送 (null key)
  • ✅ 异常情况处理

🎯 评分: 9.5/10

除了注释需要小幅优化外,其他方面表现优秀。推荐合并 🚀

这个功能将大大提升数组操作的便利性,为 Hyperf 框架增加了实用价值。

@limingxinleo limingxinleo changed the title feat: add push method to Arr class for dot notation array manipulation Added methods Hyperf\Collection\Arr::push() and Hyperf\Collection\Arr::array(). Sep 3, 2025
@limingxinleo limingxinleo merged commit 13091d5 into hyperf:master Sep 3, 2025
71 checks passed
@huangdijia huangdijia deleted the feature/add-push-method-to-arr branch September 3, 2025 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants