Skip to content

Commit 749e97d

Browse files
xHeaveninnocenzi
andauthored
refactor(support): minor code cleanup (#961)
Co-authored-by: Enzo Innocenzi <[email protected]>
1 parent 92321b3 commit 749e97d

File tree

3 files changed

+14
-25
lines changed

3 files changed

+14
-25
lines changed

src/Tempest/Support/src/Arr/functions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ function intersect_keys(iterable $array, array ...$arrays): array
445445
*/
446446
function merge(iterable $array, iterable ...$arrays): array
447447
{
448-
return array_merge(to_array($array), ...array_map(fn (iterable $value) => to_array($value), $arrays));
448+
return array_merge(to_array($array), ...array_map(to_array(...), $arrays));
449449
}
450450

451451
/**
@@ -456,7 +456,7 @@ function merge(iterable $array, iterable ...$arrays): array
456456
* @template TValue
457457
*
458458
* @param iterable<TKey,TValue> $array
459-
* @param array<array-key, TCombineValue>|array<array-key, TCombineValue> $values
459+
* @param iterable<array-key, TCombineValue> $values
460460
*
461461
* @return array<array-key, TCombineValue>
462462
*/
@@ -580,7 +580,7 @@ function is_empty(iterable $array): bool
580580
*/
581581
function implode(iterable $array, string $glue): ImmutableString
582582
{
583-
return new ImmutableString(\implode($glue, $array));
583+
return new ImmutableString(\implode($glue, to_array($array)));
584584
}
585585

586586
/**

src/Tempest/Support/src/IsEnumHelper.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use Iterator;
1111
use IteratorAggregate;
1212
use Tempest\Support\Arr\ImmutableArray;
13-
use Traversable;
1413
use UnitEnum;
1514
use ValueError;
15+
use function Tempest\Support\Arr\to_array;
1616

1717
/**
1818
* This trait provides a bunch of helper methods to work with enums
@@ -89,11 +89,11 @@ public function isNot(UnitEnum $enum): bool
8989
/**
9090
* Check if the current enum case is in the given list of enums
9191
*
92-
* @param Traversable<UnitEnum>|array<UnitEnum> $enums The list of enums to check
92+
* @param iterable<UnitEnum> $enums The list of enums to check
9393
*
9494
* @return bool True if the current enum is in the list, false otherwise
9595
*/
96-
public function in(Traversable|array $enums): bool
96+
public function in(iterable $enums): bool
9797
{
9898
$iterator = match (true) {
9999
is_array($enums) => new ArrayIterator($enums),
@@ -102,23 +102,17 @@ public function in(Traversable|array $enums): bool
102102
default => throw new InvalidArgumentException(sprintf('The given value must be an iterable value, "%s" given', get_debug_type($enums))),
103103
};
104104

105-
foreach ($iterator as $enum) {
106-
if ($this->is($enum)) {
107-
return true;
108-
}
109-
}
110-
111-
return false;
112-
}
105+
return array_any(to_array($iterator), fn($enum) => $this->is($enum));
106+
}
113107

114108
/**
115109
* Check if the current enum case is not in the given list of enums
116110
*
117-
* @param Traversable<UnitEnum>|array<UnitEnum> $enums The list of enums to check
111+
* @param iterable<UnitEnum> $enums The list of enums to check
118112
*
119113
* @return bool True if the current enum is not in the list, false otherwise
120114
*/
121-
public function notIn(Traversable|array $enums): bool
115+
public function notIn(iterable $enums): bool
122116
{
123117
return ! $this->in($enums);
124118
}

src/Tempest/Support/src/JavaScript/PackageManager.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,9 @@ public function getInstallCommand(): string
4747

4848
public static function detect(string $cwd): ?self
4949
{
50-
foreach (PackageManager::cases() as $packageManager) {
51-
foreach ($packageManager->getLockFiles() as $lockFile) {
52-
if (file_exists($cwd . '/' . $lockFile)) {
53-
return $packageManager;
54-
}
55-
}
56-
}
57-
58-
return null;
50+
return array_find(
51+
array: PackageManager::cases(),
52+
callback: fn($packageManager) => array_any($packageManager->getLockFiles(), fn($lockFile) => file_exists($cwd . '/' . $lockFile))
53+
);
5954
}
6055
}

0 commit comments

Comments
 (0)