Skip to content

Commit d8f0f04

Browse files
authored
[9.x] Universal HigherOrderWhenProxy (#37632)
* [9.x] Universal HigherOrderWhenProxy * StyleCI * Replace by-reference variables with exceptions
1 parent 86f18a8 commit d8f0f04

File tree

3 files changed

+16
-54
lines changed

3 files changed

+16
-54
lines changed

Enumerable.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,11 @@ public function filter(callable $callback = null);
271271
* Apply the callback if the value is truthy.
272272
*
273273
* @param bool $value
274-
* @param callable $callback
274+
* @param callable|null $callback
275275
* @param callable|null $default
276276
* @return static|mixed
277277
*/
278-
public function when($value, callable $callback, callable $default = null);
278+
public function when($value, callable $callback = null, callable $default = null);
279279

280280
/**
281281
* Apply the callback if the collection is empty.

HigherOrderWhenProxy.php

+12-15
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
namespace Illuminate\Support;
44

5-
/**
6-
* @mixin \Illuminate\Support\Enumerable
7-
*/
85
class HigherOrderWhenProxy
96
{
107
/**
11-
* The collection being operated on.
8+
* The target being conditionally operated on.
129
*
13-
* @var \Illuminate\Support\Enumerable
10+
* @var mixed
1411
*/
15-
protected $collection;
12+
protected $target;
1613

1714
/**
1815
* The condition for proxying.
@@ -24,31 +21,31 @@ class HigherOrderWhenProxy
2421
/**
2522
* Create a new proxy instance.
2623
*
27-
* @param \Illuminate\Support\Enumerable $collection
24+
* @param mixed $target
2825
* @param bool $condition
2926
* @return void
3027
*/
31-
public function __construct(Enumerable $collection, $condition)
28+
public function __construct($target, $condition)
3229
{
30+
$this->target = $target;
3331
$this->condition = $condition;
34-
$this->collection = $collection;
3532
}
3633

3734
/**
38-
* Proxy accessing an attribute onto the collection.
35+
* Proxy accessing an attribute onto the target.
3936
*
4037
* @param string $key
4138
* @return mixed
4239
*/
4340
public function __get($key)
4441
{
4542
return $this->condition
46-
? $this->collection->{$key}
47-
: $this->collection;
43+
? $this->target->{$key}
44+
: $this->target;
4845
}
4946

5047
/**
51-
* Proxy a method call onto the collection.
48+
* Proxy a method call on the target.
5249
*
5350
* @param string $method
5451
* @param array $parameters
@@ -57,7 +54,7 @@ public function __get($key)
5754
public function __call($method, $parameters)
5855
{
5956
return $this->condition
60-
? $this->collection->{$method}(...$parameters)
61-
: $this->collection;
57+
? $this->target->{$method}(...$parameters)
58+
: $this->target;
6259
}
6360
}

Traits/EnumeratesValues.php

+2-37
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Illuminate\Support\Collection;
1212
use Illuminate\Support\Enumerable;
1313
use Illuminate\Support\HigherOrderCollectionProxy;
14-
use Illuminate\Support\HigherOrderWhenProxy;
1514
use JsonSerializable;
1615
use Symfony\Component\VarDumper\VarDumper;
1716
use Traversable;
@@ -45,6 +44,8 @@
4544
*/
4645
trait EnumeratesValues
4746
{
47+
use Conditionable;
48+
4849
/**
4950
* The methods that can be proxied.
5051
*
@@ -453,29 +454,6 @@ public function sum($callback = null)
453454
}, 0);
454455
}
455456

456-
/**
457-
* Apply the callback if the value is truthy.
458-
*
459-
* @param bool|mixed $value
460-
* @param callable|null $callback
461-
* @param callable|null $default
462-
* @return static|mixed
463-
*/
464-
public function when($value, callable $callback = null, callable $default = null)
465-
{
466-
if (! $callback) {
467-
return new HigherOrderWhenProxy($this, $value);
468-
}
469-
470-
if ($value) {
471-
return $callback($this, $value);
472-
} elseif ($default) {
473-
return $default($this, $value);
474-
}
475-
476-
return $this;
477-
}
478-
479457
/**
480458
* Apply the callback if the collection is empty.
481459
*
@@ -500,19 +478,6 @@ public function whenNotEmpty(callable $callback, callable $default = null)
500478
return $this->when($this->isNotEmpty(), $callback, $default);
501479
}
502480

503-
/**
504-
* Apply the callback if the value is falsy.
505-
*
506-
* @param bool $value
507-
* @param callable $callback
508-
* @param callable|null $default
509-
* @return static|mixed
510-
*/
511-
public function unless($value, callable $callback, callable $default = null)
512-
{
513-
return $this->when(! $value, $callback, $default);
514-
}
515-
516481
/**
517482
* Apply the callback unless the collection is empty.
518483
*

0 commit comments

Comments
 (0)