Skip to content

Commit b0f34a1

Browse files
committed
Replace tap by transform
1 parent 321dfff commit b0f34a1

File tree

4 files changed

+16
-29
lines changed

4 files changed

+16
-29
lines changed

components/Components/URLSearchParams.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Iterator;
2222
use IteratorAggregate;
2323
use League\Uri\Contracts\QueryInterface;
24+
use League\Uri\Contracts\Transformable;
2425
use League\Uri\Contracts\UriComponentInterface;
2526
use League\Uri\Contracts\UriException;
2627
use League\Uri\Contracts\UriInterface;
@@ -57,7 +58,7 @@
5758
*
5859
* @implements IteratorAggregate<array{0:string, 1:string}>
5960
*/
60-
final class URLSearchParams implements Countable, IteratorAggregate, UriComponentInterface
61+
final class URLSearchParams implements Countable, IteratorAggregate, UriComponentInterface, Transformable
6162
{
6263
private QueryInterface $pairs;
6364

@@ -583,13 +584,11 @@ public function when(callable|bool $condition, callable $onSuccess, ?callable $o
583584
* Executes the given callback with the current instance
584585
* and returns the current instance.
585586
*
586-
* @param callable(self): void $callback
587+
* @param callable(self): self $callback
587588
*/
588-
public function tap(callable $callback): self
589+
public function transform(callable $callback): static
589590
{
590-
$callback($this);
591-
592-
return $this;
591+
return $callback($this);
593592
}
594593

595594
/**

interfaces/StringCoercionMode.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use BackedEnum;
1717
use DateTimeInterface;
18-
use IntlDateFormatter;
1918
use League\Uri\Contracts\FragmentDirective;
2019
use League\Uri\Contracts\UriComponentInterface;
2120
use League\Uri\Contracts\UriInterface;
@@ -26,7 +25,6 @@
2625

2726
use function array_is_list;
2827
use function array_map;
29-
use function extension_loaded;
3028
use function get_debug_type;
3129
use function implode;
3230
use function is_array;

uri/Builder.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use BackedEnum;
1717
use League\Uri\Contracts\Conditionable;
1818
use League\Uri\Contracts\FragmentDirective;
19+
use League\Uri\Contracts\Transformable;
1920
use League\Uri\Contracts\UriComponentInterface;
2021
use League\Uri\Exceptions\SyntaxError;
2122
use SensitiveParameter;
@@ -29,7 +30,7 @@
2930
use function str_replace;
3031
use function strpos;
3132

32-
final class Builder implements Conditionable
33+
final class Builder implements Conditionable, Transformable
3334
{
3435
private ?string $scheme = null;
3536
private ?string $username = null;
@@ -206,13 +207,11 @@ public function reset(): self
206207
* Executes the given callback with the current instance
207208
* and returns the current instance.
208209
*
209-
* @param callable(self): void $callback
210+
* @param callable(self): self $callback
210211
*/
211-
public function tap(callable $callback): self
212+
public function transform(callable $callback): static
212213
{
213-
$callback($this);
214-
215-
return $this;
214+
return $callback($this);
216215
}
217216

218217
public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): static

uri/BuilderTest.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,19 @@ public function test_tap_calls_callback_with_self_and_returns_same_instance(): v
141141
$builder = new Builder();
142142

143143
$called = false;
144-
$callback = function (Builder $b) use (&$called, $builder) {
144+
$callback = function (Builder $b) use (&$called, $builder): Builder {
145145
$called = true;
146146
self::assertSame($builder, $b);
147+
148+
return $b->authority('example.com');
147149
};
148150

149-
$result = $builder->tap($callback);
151+
$result = $builder->transform($callback);
150152

151153
self::assertTrue($called);
152154
self::assertSame($builder, $result);
153-
}
154-
155-
public function test_tap_allows_builder_mutation(): void
156-
{
157-
$builder = (new Builder())->scheme('http');
158-
$builder->tap(function (Builder $b) {
159-
$b->host('example.com');
160-
});
161-
162-
$uri = $builder->build();
163-
164-
self::assertSame('example.com', $uri->getHost());
165-
self::assertSame('http', $uri->getScheme());
155+
self::assertSame('example.com', $builder->build()->getHost());
156+
self::assertNull($builder->build()->getScheme());
166157
}
167158

168159
public function test_authority_with_host_only(): void

0 commit comments

Comments
 (0)