Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/macros/output/Hyperf/Collection/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ public static function integer(ArrayAccess|array $array, string|int|null $key, ?
{
}

/**
* Push an item into an array using "dot" notation.
* @return array
*/
public static function push(array $array, string|int|null $key, mixed ...$values)
{
}

/**
* Get a string item from an array using "dot" notation.
* @return string
Expand Down
27 changes: 27 additions & 0 deletions src/macros/src/ArrMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,31 @@ public function sortByMany()
return $array;
};
}

public function push()
{
return function (array $array, string|int|null $key, mixed ...$values) {
if ($key === null) {
foreach ($values as $value) {
$array[] = $value;
}
return $array;
}

$current = Arr::get($array, $key, []);

if (!is_array($current)) {
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

修复 not 运算符空格风格(CI 报错),与文件内现有风格保持一致

本文件其他方法均使用 if (! is_...) 的风格;此处少了空格,且 CI 已由 PHP-CS-Fixer 报出可修复问题。建议统一:

-            if (!is_array($current)) {
+            if (! is_array($current)) {

此外,同一代码块附近(大约行 232、238、244)的空白行包含多余空格,请一并移除以通过 no_whitespace_in_blank_line 规则。修复后再执行一次 vendor/bin/php-cs-fixer fix src/macros/src/ArrMixin.php 确认 CI 通过。

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!is_array($current)) {
if (! is_array($current)) {
🤖 Prompt for AI Agents
In src/macros/src/ArrMixin.php around line 233, change the negation spacing from
`if (!is_array($current))` to match the file's style `if (!
is_array($current))`, and remove trailing spaces on the blank lines near lines
~232, 238, and 244 to satisfy the no_whitespace_in_blank_line rule; after making
these whitespace fixes run vendor/bin/php-cs-fixer fix
src/macros/src/ArrMixin.php to confirm the CI issue is resolved.

throw new InvalidArgumentException(
sprintf('Array value for key [%s] must be an array, %s found.', $key, gettype($current))
);
}

$merged = array_merge($current, $values);

$result = $array;
Arr::set($result, $key, $merged);

return $result;
};
}
}
28 changes: 28 additions & 0 deletions tests/Macros/ArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/
use FriendsOfHyperf\Macros\ArrMixin;
use Hyperf\Collection\Arr;

// Register the mixin for tests
Arr::mixin(new ArrMixin());

require_once __DIR__ . '/Stubs/Common.php';

test('test arrayable', function () {
Expand Down Expand Up @@ -242,3 +246,27 @@ function ($a, $b) {
$this->assertTrue(Arr::some(['foo', 2], fn ($value, $key) => is_string($value)));
$this->assertTrue(Arr::some(['foo', 'bar'], fn ($value, $key) => is_string($value)));
});

test('test push', function () {
$array = [];

$array = Arr::push($array, 'office.furniture', 'Desk');
$this->assertEquals(['Desk'], $array['office']['furniture']);

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

$array = [];

$array = Arr::push($array, null, 'Chris', 'Nuno');
$this->assertEquals(['Chris', 'Nuno'], $array);

$array = Arr::push($array, null, 'Taylor');
$this->assertEquals(['Chris', 'Nuno', 'Taylor'], $array);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Array value for key [foo.bar] must be an array, boolean found.');

$array = ['foo' => ['bar' => false]];
Arr::push($array, 'foo.bar', 'baz');
});
Loading