Skip to content

Commit 7c7d4c6

Browse files
Merge pull request #96 from TheDragonCode/4.x
Optimized the appeal to the functions
2 parents ffc2089 + 997f7bf commit 7c7d4c6

File tree

8 files changed

+47
-27
lines changed

8 files changed

+47
-27
lines changed

src/Concerns/Arrayable.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
use Illuminate\Contracts\Support\Arrayable as IlluminateArrayable;
1717
use Illuminate\Foundation\Http\FormRequest;
1818

19+
use function array_merge;
20+
use function get_class;
21+
use function is_array;
22+
use function is_bool;
23+
use function is_numeric;
24+
use function is_object;
25+
use function is_string;
26+
use function method_exists;
27+
1928
trait Arrayable
2029
{
2130
protected function arrayMap(array $values, callable $callback): array
@@ -44,7 +53,7 @@ protected function arrayFlattenKeysMap(array $values, callable $callback): array
4453
return Arr::of($values)
4554
->flattenKeys()
4655
->filter(static fn ($value) => ! empty($value) || is_numeric($value) || is_bool($value))
47-
->map(fn (mixed $value, mixed $key) => $callback($key . '=' . $value))
56+
->map(static fn (mixed $value, mixed $key) => $callback($key . '=' . $value))
4857
->toArray();
4958
}
5059

@@ -72,10 +81,12 @@ protected function flattenKeys(mixed $array, string $delimiter = '.', ?string $p
7281
protected function toArray($value): array
7382
{
7483
return Arr::of(Arr::wrap($value))
75-
->map(fn ($value) => Instance::of($value, Carbon::class) ? $value->toIso8601String() : $value)
76-
->map(fn ($value) => Instance::of($value, FormRequest::class) ? $value->validated() : $value)
77-
->map(fn ($value) => Instance::of($value, BackedEnum::class) ? ($value->value ?? $value->name) : $value)
78-
->map(fn ($value) => is_object($value) ? (Arr::resolve($value) ?: get_class($value)) : $value)
84+
->map(static fn ($value) => Instance::of($value, Carbon::class) ? $value->toIso8601String() : $value)
85+
->map(static fn ($value) => Instance::of($value, FormRequest::class) ? $value->validated() : $value)
86+
->map(
87+
static fn ($value) => Instance::of($value, BackedEnum::class) ? ($value->value ?? $value->name) : $value
88+
)
89+
->map(static fn ($value) => is_object($value) ? (Arr::resolve($value) ?: get_class($value)) : $value)
7990
->resolve()
8091
->toArray();
8192
}

src/Concerns/Call.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,26 @@
44

55
namespace DragonCode\Cache\Concerns;
66

7+
use function function_exists;
8+
use function is_callable;
9+
use function is_string;
10+
711
trait Call
812
{
913
use Arrayable;
1014

1115
protected function call(mixed $callback = null): mixed
1216
{
13-
return $this->isCallable($callback) && ! $this->isFunction($callback) ? $callback() : $callback;
17+
return is_callable($callback) && ! $this->isFunction($callback) ? $callback() : $callback;
1418
}
1519

1620
protected function makeCallable($value): callable
1721
{
18-
if ($this->isCallable($value) && ! $this->isFunction($value)) {
22+
if (is_callable($value) && ! $this->isFunction($value)) {
1923
return $value;
2024
}
2125

22-
return function () use ($value) {
23-
return $value;
24-
};
25-
}
26-
27-
protected function isCallable($value): bool
28-
{
29-
return is_callable($value);
26+
return static fn () => $value;
3027
}
3128

3229
protected function isFunction($value): bool

src/ServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function boot(): void
1616
protected function bootConfig(): void
1717
{
1818
$this->publishes([
19-
__DIR__ . '/../config/cache.php' => config_path('cache.php'),
19+
__DIR__ . '/../config/cache.php' => $this->app->configPath('cache.php'),
2020
], 'config');
2121
}
2222
}

src/Services/Cache.php

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
use DragonCode\Support\Facades\Instances\Call;
1414
use Illuminate\Support\Facades\Auth;
1515

16+
use function array_unshift;
17+
use function config;
18+
use function get_class;
19+
use function is_object;
20+
use function is_string;
21+
1622
/**
1723
* @method static Cache make()
1824
*/

src/Support/CacheManager.php

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use DragonCode\Support\Concerns\Makeable;
1212
use Illuminate\Support\Facades\Cache;
1313

14+
use function method_exists;
15+
1416
/**
1517
* @method static CacheManager make(bool $when = true)
1618
*/

src/Support/Key.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use DragonCode\Cache\Concerns\Arrayable;
99
use Illuminate\Contracts\Support\Arrayable as IlluminateArrayable;
1010

11+
use function hash;
12+
use function implode;
13+
1114
class Key
1215
{
1316
use Arrayable;
@@ -21,16 +24,11 @@ public function get(
2124

2225
$hashed = $this->hash($values, $hash);
2326

24-
return $this->compile($hashed, $separator);
27+
return implode($separator, $hashed);
2528
}
2629

2730
protected function hash(array $values, bool $hash = true): array
2831
{
29-
return $this->arrayFlattenKeysMap($values, fn (mixed $value) => $hash ? hash('xxh128', $value) : $value);
30-
}
31-
32-
protected function compile(array $values, string $separator): string
33-
{
34-
return implode($separator, $values);
32+
return $this->arrayFlattenKeysMap($values, static fn (mixed $value) => $hash ? hash('xxh128', $value) : $value);
3533
}
3634
}

src/Support/Tag.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class Tag
1717
*/
1818
public function get(mixed $tags): array
1919
{
20-
$tags = $this->toArray($tags);
21-
22-
return $this->map($tags);
20+
return $this->map(
21+
$this->toArray($tags)
22+
);
2323
}
2424

2525
protected function map(array $tags): array

src/Support/Ttl.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99
use DragonCode\Cache\Concerns\Call;
1010
use DragonCode\Cache\Concerns\Has;
1111

12+
use function abs;
13+
use function config;
14+
use function get_class;
15+
1216
class Ttl
1317
{
1418
use Call;
1519
use Has;
1620

17-
public const DAY = 60 * 24;
21+
/** @deprecated */
22+
public const DAY = 60 * 24;
23+
1824
public const MONTH = 60 * 24 * 30;
1925
public const WEEK = 60 * 24 * 7;
2026

0 commit comments

Comments
 (0)