Skip to content

Commit 1d5caa5

Browse files
committed
Core: Review HasMutator methods
- Rename `withPropertyValue()` -> `with()` - Rename `withoutProperty()` -> `without()` - Make methods private for better safety by default - Remove `clone()`
1 parent d5ad5f9 commit 1d5caa5

29 files changed

+78
-88
lines changed

src/Toolkit/Cli/CliHelpStyle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function getVisibility(): int
213213
*/
214214
public function withCollapseSynopsis(bool $value = true)
215215
{
216-
return $this->withPropertyValue('CollapseSynopsis', $value);
216+
return $this->with('CollapseSynopsis', $value);
217217
}
218218

219219
public function prepareHelp(string $text, string $indent = ''): string

src/Toolkit/Collection/ImmutableCollectionTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ trait ImmutableCollectionTrait
3838
*/
3939
protected function maybeClone()
4040
{
41-
return $this->clone();
41+
return clone $this;
4242
}
4343
}

src/Toolkit/Collection/ImmutableListTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ trait ImmutableListTrait
3737
*/
3838
protected function maybeClone()
3939
{
40-
return $this->clone();
40+
return clone $this;
4141
}
4242
}

src/Toolkit/Console/ConsoleFormatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ public function withSpinnerState(?array &$state)
189189
*/
190190
public function withUnescape(bool $value = true)
191191
{
192-
return $this->withPropertyValue('TagFormats', $this->TagFormats->withUnescape($value));
192+
return $this->with('TagFormats', $this->TagFormats->withUnescape($value));
193193
}
194194

195195
/**
196196
* @inheritDoc
197197
*/
198198
public function withWrapAfterApply(bool $value = true)
199199
{
200-
return $this->withPropertyValue('TagFormats', $this->TagFormats->withWrapAfterApply($value));
200+
return $this->with('TagFormats', $this->TagFormats->withWrapAfterApply($value));
201201
}
202202

203203
/**

src/Toolkit/Console/Support/ConsoleMessageAttributes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,22 @@ public function __construct(
7474
*/
7575
public function withIsMsg1(bool $value = true)
7676
{
77-
return $this->withPropertyValue('IsMsg1', $value);
77+
return $this->with('IsMsg1', $value);
7878
}
7979

8080
/**
8181
* @return static
8282
*/
8383
public function withIsMsg2(bool $value = true)
8484
{
85-
return $this->withPropertyValue('IsMsg2', $value);
85+
return $this->with('IsMsg2', $value);
8686
}
8787

8888
/**
8989
* @return static
9090
*/
9191
public function withIsPrefix(bool $value = true)
9292
{
93-
return $this->withPropertyValue('IsPrefix', $value);
93+
return $this->with('IsPrefix', $value);
9494
}
9595
}

src/Toolkit/Console/Support/ConsoleTagFormats.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public function __construct(
3838
*/
3939
public function withUnescape(bool $value = true)
4040
{
41-
return $this->withPropertyValue('Unescape', $value);
41+
return $this->with('Unescape', $value);
4242
}
4343

4444
/**
4545
* @return static
4646
*/
4747
public function withWrapAfterApply(bool $value = true)
4848
{
49-
return $this->withPropertyValue('WrapAfterApply', $value);
49+
return $this->with('WrapAfterApply', $value);
5050
}
5151

5252
/**
@@ -73,7 +73,7 @@ public function getWrapAfterApply(): bool
7373
*/
7474
public function withFormat($tag, Format $format)
7575
{
76-
return $this->withPropertyValue('Formats', Arr::set($this->Formats, $tag, $format));
76+
return $this->with('Formats', Arr::set($this->Formats, $tag, $format));
7777
}
7878

7979
/**

src/Toolkit/Core/Concern/HasMutator.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,20 @@
66
use ReflectionProperty;
77

88
/**
9+
* @api
10+
*
911
* @phpstan-require-implements Immutable
1012
*/
1113
trait HasMutator
1214
{
1315
/**
14-
* Clone the object
15-
*
16-
* @return static
17-
*/
18-
protected function clone()
19-
{
20-
return clone $this;
21-
}
22-
23-
/**
24-
* Apply a value to a clone of the object if the current value differs
16+
* Get a copy of the object with a value assigned to a property if its
17+
* current value differs, otherwise return the object
2518
*
2619
* @param mixed $value
2720
* @return static
2821
*/
29-
protected function withPropertyValue(string $property, $value)
22+
private function with(string $property, $value)
3023
{
3124
if ((
3225
isset($this->$property)
@@ -35,18 +28,18 @@ protected function withPropertyValue(string $property, $value)
3528
return $this;
3629
}
3730

38-
$clone = $this->clone();
31+
$clone = clone $this;
3932
$clone->$property = $value;
4033
return $clone;
4134
}
4235

4336
/**
44-
* Remove a property from a clone of the object if it is currently set or
45-
* initialized
37+
* Get a copy of the object where a property is unset if it is currently
38+
* set, otherwise return the object
4639
*
4740
* @return static
4841
*/
49-
protected function withoutProperty(string $property)
42+
private function without(string $property)
5043
{
5144
if (
5245
!isset($this->$property)
@@ -55,7 +48,7 @@ protected function withoutProperty(string $property)
5548
return $this;
5649
}
5750

58-
$clone = $this->clone();
51+
$clone = clone $this;
5952
unset($clone->$property);
6053
return $clone;
6154
}

src/Toolkit/Core/Pipeline.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ final class Pipeline implements
3434
StreamPipelineInterface
3535
{
3636
use HasChainableMethods;
37-
use HasMutator {
38-
withPropertyValue as with;
39-
}
37+
use HasMutator;
4038

4139
private bool $HasPayload = false;
4240
private bool $HasStream;

src/Toolkit/Core/ProviderContext.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ final public function getProvider(): ProviderInterface
7272
*/
7373
final public function withContainer(ContainerInterface $container)
7474
{
75-
return $this->withPropertyValue('Container', $container);
75+
return $this->with('Container', $container);
7676
}
7777

7878
/**
7979
* @inheritDoc
8080
*/
8181
final public function push($entity)
8282
{
83-
$clone = $this->clone();
83+
$clone = clone $this;
8484
$clone->Stack[] = $entity;
8585

8686
if ($entity instanceof HasId) {
@@ -110,23 +110,23 @@ final public function withValue(string $name, $value)
110110
}
111111
}
112112

113-
return $this->withPropertyValue('Values', $values);
113+
return $this->with('Values', $values);
114114
}
115115

116116
/**
117117
* @inheritDoc
118118
*/
119119
final public function withParent(?Treeable $parent)
120120
{
121-
return $this->withPropertyValue('Parent', $parent);
121+
return $this->with('Parent', $parent);
122122
}
123123

124124
/**
125125
* @inheritDoc
126126
*/
127127
final public function withConformity($conformity)
128128
{
129-
return $this->withPropertyValue('Conformity', $conformity);
129+
return $this->with('Conformity', $conformity);
130130
}
131131

132132
/**

src/Toolkit/Curler/Curler.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@
7474
*/
7575
class Curler implements CurlerInterface, Buildable
7676
{
77+
/** @use HasBuilder<CurlerBuilder> */
78+
use HasBuilder;
79+
use HasHttpHeaders;
80+
use HasMutator;
81+
7782
/**
7883
* Limit input strings to 2MiB
7984
*
@@ -88,14 +93,6 @@ class Curler implements CurlerInterface, Buildable
8893
Method::DELETE => true,
8994
];
9095

91-
/** @use HasBuilder<CurlerBuilder> */
92-
use HasBuilder;
93-
use HasHttpHeaders;
94-
use HasMutator {
95-
HasMutator::withPropertyValue as with;
96-
HasMutator::withoutProperty as without;
97-
}
98-
9996
protected Uri $Uri;
10097
protected HttpHeadersInterface $Headers;
10198
protected ?AccessTokenInterface $AccessToken;

0 commit comments

Comments
 (0)