Skip to content

Commit

Permalink
Deprecate with functions (#305)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
g105b authored Feb 13, 2025
1 parent 4254bf3 commit f5096b5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
20 changes: 17 additions & 3 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @implements ArrayAccess<string, ?string>
* @implements Iterator<string, ?string>
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class Input implements ArrayAccess, Countable, Iterator {
use InputValueGetter;
Expand Down Expand Up @@ -235,7 +236,7 @@ public function when(array|string...$matches):Trigger {
/**
* Return a Trigger that will only pass the provided keys to its callback.
*/
public function with(string...$keys):Trigger {
public function select(string...$keys):Trigger {
foreach($keys as $key) {
if(!$this->parameters->contains($key)) {
throw new MissingInputParameterException($key);
Expand All @@ -245,20 +246,33 @@ public function with(string...$keys):Trigger {
return $this->newTrigger("with", ...$keys);
}

/** @deprecated Use select() instead to avoid ambiguity with immutable `with` functions */
public function with(string...$keys):Trigger {
return $this->select(...$keys);
}

/**
* Return a Trigger that will pass all keys apart from the provided
* keys to its callback.
*/
public function without(string...$keys):Trigger {
public function selectAllExcept(string...$keys):Trigger {
return $this->newTrigger("without", ...$keys);
}
/** @deprecated Use selectAllExcept() instead to avoid ambiguity with immutable `with` functions */
public function without(string...$keys):Trigger {
return $this->selectAllExcept(...$keys);
}

/**
* Return a Trigger that will pass all keys to its callback.
*/
public function withAll():Trigger {
public function selectAll():Trigger {
return $this->newTrigger("withAll");
}
/** @deprecated Use selectAll() instead to avoid ambiguity with immutable `with` functions */
public function withAll():Trigger {
return $this->selectAll();
}

protected function newTrigger(string $functionName, string...$args):Trigger {
$trigger = new Trigger($this);
Expand Down
38 changes: 25 additions & 13 deletions src/Trigger/Trigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class Trigger {
/** @var array<string> */
protected array $keyMatches;
/** @var array<string> */
protected array $with;
protected array $select;
/** @var array<string> */
protected array $without;
protected array $selectAllExcept;
/** @var array<Callback> */
protected array $callbacks;
protected ?bool $hasFired;
Expand All @@ -27,8 +27,8 @@ public function __construct(Input $input) {

$this->matches = [];
$this->keyMatches = [];
$this->with = [];
$this->without = [];
$this->select = [];
$this->selectAllExcept = [];
$this->callbacks = [];
$this->hasFired = null;
}
Expand All @@ -49,28 +49,40 @@ public function when(string|array...$matches):self {
return $this;
}

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

return $this;
}
/** @deprecated Use select() instead to avoid ambiguity with immutable `with` functions */
public function with(string...$keys):self {
return $this->select(...$keys);
}

public function without(string...$keys):self {
public function selectAllExcept(string...$keys):self {
foreach($keys as $key) {
$this->without []= $key;
$this->selectAllExcept []= $key;
}

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

public function withAll():self {
$this->with = [];
$this->without = [];
public function selectAll():self {
$this->select = [];
$this->selectAllExcept = [];

return $this;
}
/** @deprecated Use selectAll() instead to avoid ambiguity with immutable `with` functions */
public function withAll():self {
return $this->selectAll();
}

public function setTrigger(string $key, string $value):self {
if(!isset($this->matches[$key])) {
Expand Down Expand Up @@ -142,8 +154,8 @@ public function fire():bool {
protected function callCallbacks():void {
$fields = $this->inputDataFactory->create(
$this->input,
$this->with,
$this->without
$this->select,
$this->selectAllExcept
);

foreach($this->callbacks as $callback) {
Expand Down

0 comments on commit f5096b5

Please sign in to comment.