From 80e7af7066184e0a0ff7b98ab0960e3acaba78ee Mon Sep 17 00:00:00 2001 From: faizansf Date: Sun, 14 Jan 2024 23:53:22 +0000 Subject: [PATCH] Fix styling --- config/metafields.php | 2 +- src/Exceptions/InvalidKeyException.php | 4 +- src/Exceptions/MetafieldNotFoundException.php | 3 +- src/Facades/LaravelMetafields.php | 1 - src/LaravelMetafields.php | 37 ++++++------------- src/Models/Metafield.php | 2 - src/Utils/CacheContext.php | 9 ++--- tests/Pest.php | 4 +- tests/TestSupport/Enums/CarMetafieldsEnum.php | 2 +- tests/TestSupport/Models/Car.php | 1 - tests/Unit/LaravelMetafieldsUnitTest.php | 25 ++++--------- tests/Unit/StandardCastUnitTest.php | 2 - 12 files changed, 29 insertions(+), 63 deletions(-) diff --git a/config/metafields.php b/config/metafields.php index a8adfe0..c8d218f 100644 --- a/config/metafields.php +++ b/config/metafields.php @@ -21,5 +21,5 @@ 'cache_ttl' => null, // The prefix used for cache keys when storing individual meta field values. - 'cache_key_prefix' => "LaravelMetafields", + 'cache_key_prefix' => 'LaravelMetafields', ]; diff --git a/src/Exceptions/InvalidKeyException.php b/src/Exceptions/InvalidKeyException.php index 9a9c134..9bcac9a 100644 --- a/src/Exceptions/InvalidKeyException.php +++ b/src/Exceptions/InvalidKeyException.php @@ -3,15 +3,13 @@ namespace FaizanSf\LaravelMetafields\Exceptions; use Exception; -use InvalidArgumentException; class InvalidKeyException extends Exception { public static function withMessage(mixed $key): self { return new self( - 'Expected key to be string or string backed Enum Got' . gettype($key) + 'Expected key to be string or string backed Enum Got'.gettype($key) ); } - } diff --git a/src/Exceptions/MetafieldNotFoundException.php b/src/Exceptions/MetafieldNotFoundException.php index b99641f..b413dee 100644 --- a/src/Exceptions/MetafieldNotFoundException.php +++ b/src/Exceptions/MetafieldNotFoundException.php @@ -9,8 +9,7 @@ class MetafieldNotFoundException extends Exception public static function withMessage(mixed $key): self { return new self( - 'Metafield Not found having key ' . $key + 'Metafield Not found having key '.$key ); } - } diff --git a/src/Facades/LaravelMetafields.php b/src/Facades/LaravelMetafields.php index ebf0be5..d8b6231 100644 --- a/src/Facades/LaravelMetafields.php +++ b/src/Facades/LaravelMetafields.php @@ -11,7 +11,6 @@ */ class LaravelMetafields extends Facade { - protected static function getFacadeAccessor(): string { return \FaizanSf\LaravelMetafields\LaravelMetafields::class; diff --git a/src/LaravelMetafields.php b/src/LaravelMetafields.php index 2f0efe9..30f5b42 100644 --- a/src/LaravelMetafields.php +++ b/src/LaravelMetafields.php @@ -2,7 +2,6 @@ declare(strict_types=1); - namespace FaizanSf\LaravelMetafields; use BackedEnum; @@ -12,7 +11,6 @@ use FaizanSf\LaravelMetafields\Utils\CacheContext; use Illuminate\Support\Facades\Cache; - class LaravelMetafields { protected bool $withCache = true; @@ -32,8 +30,9 @@ public function withOutCache(): self /** * Normalizes the given key into a string. * - * @param string|BackedEnum $key The key to normalize. Can be either a string or a BackedEnum instance. + * @param string|BackedEnum $key The key to normalize. Can be either a string or a BackedEnum instance. * @return string The normalized key as a string. + * * @throws InvalidKeyException If the key is a BackedEnum instance and its value is not a string. */ public function normalizeKey(string|BackedEnum $key): string @@ -41,7 +40,7 @@ public function normalizeKey(string|BackedEnum $key): string if ($key instanceof BackedEnum) { $value = $key->value; - if (!is_string($value)) { + if (! is_string($value)) { throw InvalidKeyException::withMessage(key: $value); } @@ -54,24 +53,21 @@ public function normalizeKey(string|BackedEnum $key): string /** * Clears the cache for the given model and the given key. * - * @param Metafieldable $model The model for which to clear the cache. - * @param string $key The key for which to clear the cache. + * @param Metafieldable $model The model for which to clear the cache. + * @param string $key The key for which to clear the cache. */ - public function clearCache(Metafieldable $model, string|null $key = null): void + public function clearCache(Metafieldable $model, ?string $key = null): void { Cache::forget($this->getCacheKey($model, $key)); } - /** * Executes the given closure and caches its result for the given time if cache is enabled. * - * @param CacheContext $cacheContext - * @param $cacheKey - * @param Closure $callback The closure to execute. + * @param Closure $callback The closure to execute. * @return mixed The result of the executed closure. */ - public function runCachedOrDirect(CacheContext $cacheContext, $cacheKey, Closure $callback,): mixed + public function runCachedOrDirect(CacheContext $cacheContext, $cacheKey, Closure $callback): mixed { if ($this->canUseCache($cacheContext)) { return Cache::remember( @@ -89,28 +85,22 @@ public function runCachedOrDirect(CacheContext $cacheContext, $cacheKey, Closure * and primary key. The optional key parameter, when provided, specifies a particular metafield; otherwise, * it represents all metafields. Null values in model details are substituted with 'null'. * - * @param Metafieldable $model The model for which the cache key is being generated. - * @param string|null $key An optional key for a specific metafield. If null, the key represents all metafields. + * @param Metafieldable $model The model for which the cache key is being generated. + * @param string|null $key An optional key for a specific metafield. If null, the key represents all metafields. * @return string The constructed cache key. */ - public function getCacheKey(Metafieldable $model, string|null $key = null): string + public function getCacheKey(Metafieldable $model, ?string $key = null): string { return collect([ config('metafields.cache_key_prefix'), class_basename($model), $model->getKey() ?? 'null', - $key + $key, ])->filter(function ($value) { return $value !== null; })->join(':'); } - - /** - * - * @param Metafieldable $model - * @return string - */ public function getAllMetaFieldsCacheKey(Metafieldable $model): string { return $this->getCacheKey($model); @@ -118,12 +108,9 @@ public function getAllMetaFieldsCacheKey(Metafieldable $model): string /** * Checks if cache is enabled and the current instance is configured to use it. - * @param CacheContext $cacheContext - * @return bool */ protected function canUseCache(CacheContext $cacheContext): bool { return $cacheContext->isCacheEnabled() && $this->withCache; } - } diff --git a/src/Models/Metafield.php b/src/Models/Metafield.php index 2751ff1..0e443d4 100644 --- a/src/Models/Metafield.php +++ b/src/Models/Metafield.php @@ -23,7 +23,6 @@ class Metafield extends Model */ protected $hidden = ['model_type', 'model_id']; - /** * The model relationship. */ @@ -42,5 +41,4 @@ public function __construct(array $attributes = []) $this->setTable(config('metafields.table')); } - } diff --git a/src/Utils/CacheContext.php b/src/Utils/CacheContext.php index 065a502..b3cd4da 100644 --- a/src/Utils/CacheContext.php +++ b/src/Utils/CacheContext.php @@ -7,13 +7,13 @@ class CacheContext public function __construct( protected bool $cacheEnabled, protected ?int $ttl - ) {} + ) { + } /** * Create a new CacheContext instance. * - * @param string $class The class name to create the context for. - * @return CacheContext + * @param string $class The class name to create the context for. */ public static function make(string $class): CacheContext { @@ -25,8 +25,6 @@ public static function make(string $class): CacheContext /** * Check if caching is enabled. - * - * @return bool */ public function isCacheEnabled(): bool { @@ -42,5 +40,4 @@ public function getTtl(): ?int { return $this->ttl; } - } diff --git a/tests/Pest.php b/tests/Pest.php index e0fe742..b9fc1e3 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -7,8 +7,8 @@ uses(TestCase::class)->in(__DIR__); - -function makeCarInstance(){ +function makeCarInstance() +{ $car = Car::newFactory()->make(); $car->id = 1; diff --git a/tests/TestSupport/Enums/CarMetafieldsEnum.php b/tests/TestSupport/Enums/CarMetafieldsEnum.php index 80834d8..a464ce6 100644 --- a/tests/TestSupport/Enums/CarMetafieldsEnum.php +++ b/tests/TestSupport/Enums/CarMetafieldsEnum.php @@ -2,7 +2,7 @@ namespace FaizanSf\LaravelMetafields\Tests\TestSupport\Enums; -enum CarMetafieldsEnum:string +enum CarMetafieldsEnum: string { case MODEL = 'MODEL'; case COLOR = 'COLOR'; diff --git a/tests/TestSupport/Models/Car.php b/tests/TestSupport/Models/Car.php index bb9056f..13345c9 100644 --- a/tests/TestSupport/Models/Car.php +++ b/tests/TestSupport/Models/Car.php @@ -5,7 +5,6 @@ use FaizanSf\LaravelMetafields\Concerns\HasMetafields; use FaizanSf\LaravelMetafields\Contracts\Metafieldable; use FaizanSf\LaravelMetafields\Tests\TestSupport\Factories\CarFactory; -use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Car extends Model implements Metafieldable diff --git a/tests/Unit/LaravelMetafieldsUnitTest.php b/tests/Unit/LaravelMetafieldsUnitTest.php index 7cebb1a..0206371 100644 --- a/tests/Unit/LaravelMetafieldsUnitTest.php +++ b/tests/Unit/LaravelMetafieldsUnitTest.php @@ -1,39 +1,35 @@ toBeString()->toEqual($key->value); })->with('testKeys'); - -it('throws exception where enum key is not a string', function($invalidTestKey){ +it('throws exception where enum key is not a string', function ($invalidTestKey) { LaravelMetafields::normalizeKey($invalidTestKey); })->throws(InvalidKeyException::class)->with('invalidTestKeys'); -it('does not normalize string key', function($stringKey){ +it('does not normalize string key', function ($stringKey) { $key = $stringKey; $normalizedKey = LaravelMetafields::normalizeKey($key); expect($normalizedKey)->toBeString()->toEqual($key); })->with('stringKeys'); -it('returns single field cache key in correct format', function($key){ +it('returns single field cache key in correct format', function ($key) { $key = LaravelMetafields::normalizeKey($key); $car = makeCarInstance(); $prefix = 'LaravelMetafields'; - config()->set('metafields.cache_key_prefix', $prefix); $cacheKey = LaravelMetafields::getCacheKey($car, $key); - $expected = $prefix . ':Car:' . $car->getKey() . ':' . $key; + $expected = $prefix.':Car:'.$car->getKey().':'.$key; expect($cacheKey)->toBeString()->toEqual($expected); })->with('stringKeys', 'testKeys'); @@ -44,7 +40,7 @@ $car = makeCarInstance(); $cacheKey = LaravelMetafields::getAllMetaFieldsCacheKey($car); - $expected = $prefix . ':Car:' . $car->getKey(); + $expected = $prefix.':Car:'.$car->getKey(); expect($cacheKey)->toBeString()->toEqual($expected); @@ -55,7 +51,7 @@ $key = 'model'; $cacheKey = LaravelMetafields::getCacheKey($car, $key); - LaravelMetafields::runCachedOrDirect($car->getCacheContext(), $cacheKey, function(){ + LaravelMetafields::runCachedOrDirect($car->getCacheContext(), $cacheKey, function () { return 1999; }); @@ -67,7 +63,6 @@ }); - it('doesnt caches the result when cache is disabled', function () { config()->set('metafields.cache_enabled', false); @@ -75,13 +70,9 @@ $key = 'model'; $cacheKey = LaravelMetafields::getCacheKey($car, $key); - - LaravelMetafields::runCachedOrDirect($car->getCacheContext(), $cacheKey, function(){ + LaravelMetafields::runCachedOrDirect($car->getCacheContext(), $cacheKey, function () { return 1999; }); expect(cache()->has($cacheKey))->toBeFalse(); }); - - - diff --git a/tests/Unit/StandardCastUnitTest.php b/tests/Unit/StandardCastUnitTest.php index 5a55259..48fbdf5 100644 --- a/tests/Unit/StandardCastUnitTest.php +++ b/tests/Unit/StandardCastUnitTest.php @@ -3,7 +3,6 @@ use FaizanSf\LaravelMetafields\Casts\StandardCast; use FaizanSf\LaravelMetafields\Models\Metafield; - beforeEach(function () { $this->cast = new StandardCast(); $this->model = new Metafield(); @@ -44,4 +43,3 @@ expect($this->cast->get($this->model, 'value', $serialized, [])) ->toBeInstanceOf(__PHP_Incomplete_Class::class); }); -