Skip to content

Commit f5096b5

Browse files
author
Greg Bowler
authored
Deprecate with functions (#305)
* ci: remove old artifacts * ci: run all php versions * deprecate: all with functions on the Input class for #301 * deprecate: all with functions on the Trigger class for #301 * tweak: deprecate pre-8.4 attribute * tweak: until deprecations are removed, suppress TooManyPublicMethods * tweak: line length
1 parent 4254bf3 commit f5096b5

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

src/Input.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @implements ArrayAccess<string, ?string>
2424
* @implements Iterator<string, ?string>
2525
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
26+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2627
*/
2728
class Input implements ArrayAccess, Countable, Iterator {
2829
use InputValueGetter;
@@ -235,7 +236,7 @@ public function when(array|string...$matches):Trigger {
235236
/**
236237
* Return a Trigger that will only pass the provided keys to its callback.
237238
*/
238-
public function with(string...$keys):Trigger {
239+
public function select(string...$keys):Trigger {
239240
foreach($keys as $key) {
240241
if(!$this->parameters->contains($key)) {
241242
throw new MissingInputParameterException($key);
@@ -245,20 +246,33 @@ public function with(string...$keys):Trigger {
245246
return $this->newTrigger("with", ...$keys);
246247
}
247248

249+
/** @deprecated Use select() instead to avoid ambiguity with immutable `with` functions */
250+
public function with(string...$keys):Trigger {
251+
return $this->select(...$keys);
252+
}
253+
248254
/**
249255
* Return a Trigger that will pass all keys apart from the provided
250256
* keys to its callback.
251257
*/
252-
public function without(string...$keys):Trigger {
258+
public function selectAllExcept(string...$keys):Trigger {
253259
return $this->newTrigger("without", ...$keys);
254260
}
261+
/** @deprecated Use selectAllExcept() instead to avoid ambiguity with immutable `with` functions */
262+
public function without(string...$keys):Trigger {
263+
return $this->selectAllExcept(...$keys);
264+
}
255265

256266
/**
257267
* Return a Trigger that will pass all keys to its callback.
258268
*/
259-
public function withAll():Trigger {
269+
public function selectAll():Trigger {
260270
return $this->newTrigger("withAll");
261271
}
272+
/** @deprecated Use selectAll() instead to avoid ambiguity with immutable `with` functions */
273+
public function withAll():Trigger {
274+
return $this->selectAll();
275+
}
262276

263277
protected function newTrigger(string $functionName, string...$args):Trigger {
264278
$trigger = new Trigger($this);

src/Trigger/Trigger.php

+25-13
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class Trigger {
1313
/** @var array<string> */
1414
protected array $keyMatches;
1515
/** @var array<string> */
16-
protected array $with;
16+
protected array $select;
1717
/** @var array<string> */
18-
protected array $without;
18+
protected array $selectAllExcept;
1919
/** @var array<Callback> */
2020
protected array $callbacks;
2121
protected ?bool $hasFired;
@@ -27,8 +27,8 @@ public function __construct(Input $input) {
2727

2828
$this->matches = [];
2929
$this->keyMatches = [];
30-
$this->with = [];
31-
$this->without = [];
30+
$this->select = [];
31+
$this->selectAllExcept = [];
3232
$this->callbacks = [];
3333
$this->hasFired = null;
3434
}
@@ -49,28 +49,40 @@ public function when(string|array...$matches):self {
4949
return $this;
5050
}
5151

52-
public function with(string...$keys):self {
52+
public function select(string...$keys):self {
5353
foreach($keys as $key) {
54-
$this->with []= $key;
54+
$this->select []= $key;
5555
}
5656

5757
return $this;
5858
}
59+
/** @deprecated Use select() instead to avoid ambiguity with immutable `with` functions */
60+
public function with(string...$keys):self {
61+
return $this->select(...$keys);
62+
}
5963

60-
public function without(string...$keys):self {
64+
public function selectAllExcept(string...$keys):self {
6165
foreach($keys as $key) {
62-
$this->without []= $key;
66+
$this->selectAllExcept []= $key;
6367
}
6468

6569
return $this;
6670
}
71+
/** @deprecated Use selectAllExcept() instead to avoid ambiguity with immutable `with` functions */
72+
public function without(string...$keys):self {
73+
return $this->selectAllExcept(...$keys);
74+
}
6775

68-
public function withAll():self {
69-
$this->with = [];
70-
$this->without = [];
76+
public function selectAll():self {
77+
$this->select = [];
78+
$this->selectAllExcept = [];
7179

7280
return $this;
7381
}
82+
/** @deprecated Use selectAll() instead to avoid ambiguity with immutable `with` functions */
83+
public function withAll():self {
84+
return $this->selectAll();
85+
}
7486

7587
public function setTrigger(string $key, string $value):self {
7688
if(!isset($this->matches[$key])) {
@@ -142,8 +154,8 @@ public function fire():bool {
142154
protected function callCallbacks():void {
143155
$fields = $this->inputDataFactory->create(
144156
$this->input,
145-
$this->with,
146-
$this->without
157+
$this->select,
158+
$this->selectAllExcept
147159
);
148160

149161
foreach($this->callbacks as $callback) {

0 commit comments

Comments
 (0)