Skip to content

Commit b16f797

Browse files
feat(support): add methods to array helper (#590)
Co-authored-by: Karel Faille <[email protected]>
1 parent e719dfa commit b16f797

File tree

3 files changed

+205
-1
lines changed

3 files changed

+205
-1
lines changed

rector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Rector\Config\RectorConfig;
99
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPublicMethodParameterRector;
1010
use Rector\DeadCode\Rector\PropertyProperty\RemoveNullPropertyInitializationRector;
11-
use Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector;
1211
use Rector\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector;
1312
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
1413
use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector;

src/Tempest/Support/src/ArrayHelper.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,56 @@ public function __construct(
4848
}
4949
}
5050

51+
/**
52+
* Get a value from the array and remove it.
53+
*
54+
* @param array-key $key
55+
*/
56+
public function pull(string|int $key, mixed $default = null): mixed
57+
{
58+
$value = $this->get($key, $default);
59+
60+
$this->remove($key);
61+
62+
return $value;
63+
}
64+
65+
/**
66+
* Shuffle the array.
67+
*
68+
* @return self<TKey, TValue>
69+
*/
70+
public function shuffle(): self
71+
{
72+
return new self((new Randomizer())->shuffleArray($this->array));
73+
}
74+
75+
/**
76+
* @alias of remove.
77+
*/
78+
public function forget(string|int|array $keys): self
79+
{
80+
return $this->remove($keys);
81+
}
82+
83+
/**
84+
* Remove items from the array.
85+
*
86+
* @param array-key|array<array-key> $keys The keys of the items to remove.
87+
*
88+
* @return self<TKey, TValue>
89+
*/
90+
public function remove(string|int|array $keys): self
91+
{
92+
$keys = is_array($keys) ? $keys : [$keys];
93+
94+
foreach ($keys as $key) {
95+
$this->offsetUnset($key);
96+
}
97+
98+
return $this;
99+
}
100+
51101
/**
52102
* Determines if the array is a list.
53103
*

src/Tempest/Support/tests/ArrayHelperTest.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,4 +1109,159 @@ public function test_is_assoc(): void
11091109
$this->assertTrue(arr([0 => 'a', 2 => 'b'])->isAssoc());
11101110
$this->assertTrue(arr(['foo' => 'a', 'baz' => 'b'])->isAssoc());
11111111
}
1112+
1113+
public function test_remove_with_basic_keys(): void
1114+
{
1115+
$collection = arr([1, 2, 3]);
1116+
1117+
$this->assertEquals(
1118+
actual: $collection
1119+
->remove(1)
1120+
->toArray(),
1121+
expected: [
1122+
0 => 1,
1123+
2 => 3,
1124+
],
1125+
);
1126+
1127+
$this->assertEquals(
1128+
actual: $collection
1129+
->remove([0, 2])
1130+
->toArray(),
1131+
expected: [],
1132+
);
1133+
}
1134+
1135+
public function test_remove_with_associative_keys(): void
1136+
{
1137+
$collection = arr([
1138+
'first_name' => 'John',
1139+
'last_name' => 'Doe',
1140+
'age' => 42,
1141+
]);
1142+
1143+
$this->assertEquals(
1144+
actual: $collection
1145+
->remove('first_name')
1146+
->toArray(),
1147+
expected: [
1148+
'last_name' => 'Doe',
1149+
'age' => 42,
1150+
],
1151+
);
1152+
1153+
$this->assertEquals(
1154+
actual: $collection
1155+
->remove(['last_name', 'age'])
1156+
->toArray(),
1157+
expected: [],
1158+
);
1159+
}
1160+
1161+
public function test_remove_with_no_valid_key(): void
1162+
{
1163+
$collection = arr([1, 2, 3]);
1164+
1165+
$this->assertEquals(
1166+
actual: $collection
1167+
->remove(42)
1168+
->toArray(),
1169+
expected: [1, 2, 3],
1170+
);
1171+
1172+
$collection = arr([
1173+
'first_name' => 'John',
1174+
'last_name' => 'Doe',
1175+
'age' => 42,
1176+
]);
1177+
1178+
$this->assertEquals(
1179+
actual: $collection
1180+
->remove('foo')
1181+
->toArray(),
1182+
expected: [
1183+
'first_name' => 'John',
1184+
'last_name' => 'Doe',
1185+
'age' => 42,
1186+
],
1187+
);
1188+
1189+
$this->assertEquals(
1190+
actual: $collection
1191+
->remove(['bar', 'first_name'])
1192+
->toArray(),
1193+
expected: [
1194+
'last_name' => 'Doe',
1195+
'age' => 42,
1196+
],
1197+
);
1198+
}
1199+
1200+
public function test_forget_is_alias_of_remove(): void
1201+
{
1202+
$first_collection = arr([
1203+
'first_name' => 'John',
1204+
'last_name' => 'Doe',
1205+
'age' => 42,
1206+
])
1207+
->remove(42)
1208+
->remove('foo')
1209+
->remove(['bar', 'first_name']);
1210+
1211+
$second_collection = arr([
1212+
'first_name' => 'John',
1213+
'last_name' => 'Doe',
1214+
'age' => 42,
1215+
])
1216+
->forget(42)
1217+
->forget('foo')
1218+
->forget(['bar', 'first_name']);
1219+
1220+
1221+
$this->assertTrue($first_collection->equals($second_collection));
1222+
}
1223+
1224+
public function test_shuffle_actually_shuffles(): void
1225+
{
1226+
$array = range('a', 'z');
1227+
1228+
$this->assertFalse(
1229+
arr($array)->shuffle()->toArray() === $array
1230+
&& arr($array)->shuffle()->toArray() === $array,
1231+
);
1232+
}
1233+
1234+
public function test_shuffle_keeps_same_values(): void
1235+
{
1236+
$array = range('a', 'z');
1237+
$shuffled = arr($array)->shuffle()->toArray();
1238+
sort($shuffled);
1239+
1240+
$this->assertSame(
1241+
actual : $shuffled,
1242+
expected: $array,
1243+
);
1244+
}
1245+
1246+
public function test_pull(): void
1247+
{
1248+
$collection = arr([
1249+
'first_name' => 'John',
1250+
'last_name' => 'Doe',
1251+
'age' => 42,
1252+
]);
1253+
1254+
$this->assertSame(
1255+
actual: $collection->pull('first_name'),
1256+
expected: 'John',
1257+
);
1258+
1259+
$this->assertSame(
1260+
actual: $collection->toArray(),
1261+
expected: [
1262+
'last_name' => 'Doe',
1263+
'age' => 42,
1264+
],
1265+
);
1266+
}
11121267
}

0 commit comments

Comments
 (0)