Skip to content

Commit 7597029

Browse files
authored
refactor(support): clarify has and contains methods (#1296)
1 parent e3743ae commit 7597029

File tree

10 files changed

+72
-23
lines changed

10 files changed

+72
-23
lines changed

packages/console/src/Middleware/ResolveOrRescueMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private function getSimilarCommands(ImmutableString $search): ImmutableArray
8484
$currentName = str($consoleCommand->getName());
8585

8686
// Already added to suggestions
87-
if ($suggestions->contains($currentName->toString())) {
87+
if ($suggestions->hasValue($currentName->toString())) {
8888
continue;
8989
}
9090

packages/console/src/Middleware/ValidateNamedArgumentsMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __invoke(Invocation $invocation, ConsoleMiddlewareCallable $next
2929

3030
$invalidInput = arr($invocation->argumentBag->arguments)
3131
->filter(fn (ConsoleInputArgument $argument) => $argument->name !== null)
32-
->filter(fn (ConsoleInputArgument $argument) => ! $allowedParameterNames->contains(ltrim($argument->name, '-')))
32+
->filter(fn (ConsoleInputArgument $argument) => ! $allowedParameterNames->hasValue(ltrim($argument->name, '-')))
3333
->filter(fn (ConsoleInputArgument $argument) => ! in_array($argument->name, GlobalFlags::values(), strict: true));
3434

3535
if ($invalidInput->isNotEmpty()) {

packages/database/src/Mappers/SelectModelMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function normalizeRow(ModelInspector $model, array $row, MutableArray $da
103103

104104
$originalKey .= $relation->name . '.';
105105

106-
if (! $data->has(trim($originalKey, '.'))) {
106+
if (! $data->hasKey(trim($originalKey, '.'))) {
107107
$data->set(trim($originalKey, '.'), []);
108108
}
109109

packages/http/src/IsRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use function Tempest\get;
1313
use function Tempest\Support\Arr\get_by_key;
14-
use function Tempest\Support\Arr\has;
14+
use function Tempest\Support\Arr\has_key;
1515

1616
/** @phpstan-require-implements \Tempest\Http\Request */
1717
trait IsRequest
@@ -125,14 +125,14 @@ public function has(string $key): bool
125125
public function hasBody(?string $key = null): bool
126126
{
127127
if ($key) {
128-
return has($this->body, $key);
128+
return has_key($this->body, $key);
129129
}
130130

131131
return count($this->body) || ((bool) $this->raw);
132132
}
133133

134134
public function hasQuery(string $key): bool
135135
{
136-
return has($this->query, $key);
136+
return has_key($this->query, $key);
137137
}
138138
}

packages/http/src/Mappers/PsrRequestToGenericRequestMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function map(mixed $from, mixed $to): GenericRequest
2828
$data = (array) $from->getParsedBody();
2929
$raw = $from->getBody()->getContents();
3030

31-
if (arr($from->getHeader('content-type'))->contains('application/json') && json_validate($raw)) {
31+
if (arr($from->getHeader('content-type'))->hasValue('application/json') && json_validate($raw)) {
3232
$data = [...$data, ...json_decode($raw, associative: true)];
3333
}
3434

packages/support/src/Arr/ManipulatesArray.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ public function mapWithKeys(Closure $map): self
517517
*/
518518
public function get(int|string $key, mixed $default = null): mixed
519519
{
520-
return get_by_key($this->value, $key, $default);
520+
return namespace\get_by_key($this->value, $key, $default);
521521
}
522522

523523
/**
@@ -537,21 +537,45 @@ public function put(string $key, mixed $value): self
537537
}
538538

539539
/**
540-
* Asserts whether a value identified by the specified `$key` exists.
540+
* Asserts whether the instance has the given item. A callback may be used instead of a value.
541+
*
542+
* @see `hasValue`
543+
* @param TValue|Closure(TValue, TKey): bool $search
541544
*/
542-
public function has(int|string $key): bool
545+
public function contains(mixed $search): bool
543546
{
544-
return namespace\has($this->value, $key);
547+
return namespace\contains($this->value, $search);
545548
}
546549

547550
/**
548-
* Asserts whether the instance contains an item that can be identified by `$search`.
551+
* Asserts whether a value identified by the specified `$key` exists. Dot notation is supported.
549552
*/
550-
public function contains(mixed $search): bool
553+
public function hasKey(int|string $key): bool
554+
{
555+
return namespace\has_key($this->value, $key);
556+
}
557+
558+
/**
559+
* Asserts whether the instance contains the specified value.
560+
*
561+
* @param TValue: bool $search
562+
*/
563+
public function hasValue(mixed $search): bool
551564
{
552565
return namespace\contains($this->value, $search);
553566
}
554567

568+
/**
569+
* Asserts whether the instance contains the specified value.
570+
*
571+
* @see `hasValue`
572+
* @param TValue|Closure(TValue, TKey): bool $search
573+
*/
574+
public function includes(mixed $search): bool
575+
{
576+
return $this->hasValue($search);
577+
}
578+
555579
/**
556580
* Asserts whether all items in the instance pass the given `$callback`.
557581
*

packages/support/src/Arr/functions.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,9 +807,9 @@ function get_by_key(iterable $array, int|string $key, mixed $default = null): mi
807807
}
808808

809809
/**
810-
* Asserts whether a value identified by the specified `$key` exists.
810+
* Asserts whether a value identified by the specified `$key` exists. Dot notation is supported.
811811
*/
812-
function has(iterable $array, int|string $key): bool
812+
function has_key(iterable $array, int|string $key): bool
813813
{
814814
$array = to_array($array);
815815

@@ -833,11 +833,21 @@ function has(iterable $array, int|string $key): bool
833833
}
834834

835835
/**
836-
* Asserts whether the given array contains an item that can be identified by `$search`.
836+
* Asserts whether the given array contains a value that can be identified by `$search`.
837+
*
838+
* @template TKey of array-key
839+
* @template TValue
840+
*
841+
* @param iterable<TKey,TValue> $array
842+
* @param TValue|Closure(TValue, TKey): bool $search
837843
*/
838844
function contains(iterable $array, mixed $search): bool
839845
{
840-
return namespace\first(to_array($array), fn (mixed $value) => $value === $search) !== null;
846+
$search = ($search instanceof Closure)
847+
? $search
848+
: static fn (mixed $value) => $value === $search;
849+
850+
return namespace\first(to_array($array), $search) !== null;
841851
}
842852

843853
/**

packages/support/tests/Arr/ManipulatesArrayTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,17 @@ public function test_get(): void
9292
$this->assertSame('b', arr($array)->get('a'));
9393
}
9494

95-
public function test_arr_has(): void
95+
public function test_arr_has_key(): void
9696
{
9797
$array = [
9898
'a' => [
9999
'b' => 'c',
100100
],
101101
];
102102

103-
$this->assertTrue(arr($array)->has('a.b'));
104-
$this->assertTrue(arr($array)->has('a'));
105-
$this->assertFalse(arr($array)->has('a.x'));
103+
$this->assertTrue(arr($array)->hasKey('a.b'));
104+
$this->assertTrue(arr($array)->hasKey('a'));
105+
$this->assertFalse(arr($array)->hasKey('a.x'));
106106
}
107107

108108
public function test_arr_set(): void
@@ -330,6 +330,21 @@ public function test_contains(): void
330330
{
331331
$this->assertTrue(arr(['a', 'b', 'c'])->contains('b'));
332332
$this->assertFalse(arr(['a', 'b', 'c'])->contains('d'));
333+
334+
$this->assertTrue(arr(['a', 'b', 'c'])->includes('b'));
335+
$this->assertFalse(arr(['a', 'b', 'c'])->includes('d'));
336+
337+
$this->assertTrue(arr(['a', 'b', 'c'])->hasValue('b'));
338+
$this->assertFalse(arr(['a', 'b', 'c'])->hasValue('d'));
339+
340+
$this->assertTrue(arr(['a', 'b', 'c'])->contains(fn (string $v) => $v === 'b'));
341+
$this->assertFalse(arr(['a', 'b', 'c'])->contains(fn (string $v) => $v === 'd'));
342+
343+
$this->assertTrue(arr(['a', 'b', 'c'])->hasValue(fn (string $v) => $v === 'b'));
344+
$this->assertFalse(arr(['a', 'b', 'c'])->hasValue(fn (string $v) => $v === 'd'));
345+
346+
$this->assertTrue(arr(['a', 'b', 'c'])->includes(fn (string $v) => $v === 'b'));
347+
$this->assertFalse(arr(['a', 'b', 'c'])->includes(fn (string $v) => $v === 'd'));
333348
}
334349

335350
public function test_explode(): void

packages/validation/src/Validator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function validateValuesForClass(ClassReflector|string $class, ?array $val
6363

6464
$key = $prefix . $property->getName();
6565

66-
if (! $values->has($key) && $property->hasDefaultValue()) {
66+
if (! $values->hasKey($key) && $property->hasDefaultValue()) {
6767
continue;
6868
}
6969

src/Tempest/Framework/Testing/InstallerTester.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function assertFileNotContains(string $path, string $search): self
121121
public function assertCommandExecuted(string $command): self
122122
{
123123
Assert::assertTrue(
124-
condition: arr($this->executor->executedCommands)->contains($command),
124+
condition: arr($this->executor->executedCommands)->hasValue($command),
125125
message: sprintf('The command `%s` was not executed', $command),
126126
);
127127

0 commit comments

Comments
 (0)