Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Commit

Permalink
Removes shadow helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Aug 25, 2021
1 parent cfbe31f commit b3c3d5c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 103 deletions.
50 changes: 10 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`

Expand Down Expand Up @@ -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.
Expand Down
31 changes: 0 additions & 31 deletions helpers/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 0 additions & 32 deletions tests/CommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b3c3d5c

Please sign in to comment.