diff --git a/README.md b/README.md index 0b29a19..b5a46f5 100644 --- a/README.md +++ b/README.md @@ -32,16 +32,16 @@ This package includes helpful global helpers for your project make almost anythi | | | | |---|---|---| | [app_call](#app_call) | [in_console](#in_console) | [route_is](#route_is) -| [call_existing](#call_existing) | [in_development](#in_development) | [shadow](#shadow) -| [created](#created) | [logged_in](#logged_in) | [sleep_between](#sleep_between) -| [data_update](#data_update) | [methods_of](#methods_of) | [taptap](#taptap) -| [delist](#delist) | [missing_trait](#missing_trait) | [undot_path](#undot_path) -| [diff](#diff) | [none_of](#none_of) | [until](#until) -| [dot_path](#dot_path) | [ok](#ok) | [user](#user) -| [enclose](#enclose) | [period](#period) | [weekend](#weekend) -| [files](#files) | [period_from](#period_from) | [weekstart](#weekstart) -| [has_trait](#has_trait) | [pipe](#pipe) | [which_of](#which_of) -| [hashy](#hashy) | [remember](#remember) | [yesterday](#yesterday) +| [call_existing](#call_existing) | [in_development](#in_development) | [sleep_between](#sleep_between) +| [created](#created) | [logged_in](#logged_in) | [taptap](#taptap) +| [data_update](#data_update) | [methods_of](#methods_of) | [undot_path](#undot_path) +| [delist](#delist) | [missing_trait](#missing_trait) | [until](#until) +| [diff](#diff) | [none_of](#none_of) | [user](#user) +| [dot_path](#dot_path) | [ok](#ok) | [weekend](#weekend) +| [enclose](#enclose) | [period](#period) | [weekstart](#weekstart) +| [files](#files) | [period_from](#period_from) | [which_of](#which_of) +| [has_trait](#has_trait) | [pipe](#pipe) | [yesterday](#yesterday) +| [hashy](#hashy) | [remember](#remember) | ### `app_call()` @@ -439,36 +439,6 @@ if (route_is('dahsboard.*')) { } ``` -### `shadow()` - -Calls the next method only if the given condition is true. It's like `tap()` but conditionally. - -```php -use App\Models\User; - -$inDevelopment = app()->environment('production'); - -$user = Use::find(1) - -$user->name = 'John'; - -return shadow($user, $inDevelopment)->save(); -``` - -It also accepts a Closure, that will be evaluated before running the target method. The Closure receives the object. - -```php -use App\Models\User; - -$user = Use::find(1) - -$user->name = 'John'; - -return shadow($user, function (string $env) { - return app()->environment($env) ; -})->save('development'); -``` - ### `sleep_between()` Runs a callback while sleeping between multiple executions, returning a Collection of all results. diff --git a/helpers/common.php b/helpers/common.php index f38b32f..3eda884 100644 --- a/helpers/common.php +++ b/helpers/common.php @@ -195,37 +195,6 @@ function sleep_between(int $times, int $sleep, callable $callback): Collection } } -if (!function_exists('shadow')) { - /** - * Calls the next method only if the given condition is true. - * - * @template T - * - * @param mixed $condition - * @param T $object - * - * @return T - */ - function shadow(object $object, mixed $condition): object - { - return new class($object, $condition) { - public function __construct(protected object $object, protected mixed $condition) - { - // - } - - public function __call(string $name, array $arguments): object - { - if (value($this->condition, $this->object)) { - $this->object->{$name}(...$arguments); - } - - return $this->object; - } - }; - } -} - if (!function_exists('taptap')) { /** * Call the given Closure with the given value then return the value, twice. diff --git a/tests/CommonTest.php b/tests/CommonTest.php index 450ff06..afe6cc7 100644 --- a/tests/CommonTest.php +++ b/tests/CommonTest.php @@ -4,7 +4,6 @@ use Closure; use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Fluent; use Illuminate\Support\HigherOrderTapProxy; use Illuminate\Support\Str; use Orchestra\Testbench\TestCase; @@ -236,37 +235,6 @@ public function test_while_sleep(): void static::assertLessThan(5, $end - $start); } - public function test_shadow(): void - { - $object = new Fluent([ - 'foo' => 'bar' - ]); - - shadow($object, true)->offsetSet('foo', 'quz'); - - static::assertSame('quz', $object->foo); - - shadow($object, false)->offsetSet('foo', 'bar'); - - static::assertSame('quz', $object->foo); - - $object = new Fluent([ - 'foo' => 'bar' - ]); - - shadow($object, static function ($object): bool { - return $object->foo === 'bar'; - })->offsetSet('foo', 'baz'); - - static::assertSame('baz', $object->foo); - - shadow($object, static function ($object): bool { - return $object->foo === 'bar'; - })->offsetSet('foo', 'bar'); - - static::assertSame('baz', $object->foo); - } - public function test_taptap(): void { $object = new class {