From 483e717f8f9160a3fb07876f8dfcbd9b8f75a17a Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Tue, 4 Jun 2024 09:39:51 +0900 Subject: [PATCH] Scrutinizer 10.00 Refactor injector creation to separate method The process of creating a new injector has been moved to a new private method called getInjector in the PackageInjector class. This refactor improves code maintainability by encapsulating the injector creation and cache verification logic in a separate function. --- src/Injector/PackageInjector.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Injector/PackageInjector.php b/src/Injector/PackageInjector.php index 9798ef41..be90f346 100644 --- a/src/Injector/PackageInjector.php +++ b/src/Injector/PackageInjector.php @@ -52,12 +52,7 @@ public static function getInstance(AbstractAppMeta $meta, string $context, Cache [$injector, $fileUpdate] = $cache->getItem($injectorId)->get(); // @phpstan-ignore-line $isCacheableInjector = $injector instanceof ScriptInjector || ($injector instanceof InjectorInterface && $fileUpdate instanceof FileUpdate && $fileUpdate->isNotUpdated($meta)); if (! $isCacheableInjector) { - $injector = self::factory($meta, $context); - $cache->save($cache->getItem($injectorId)->set([$injector, new FileUpdate($meta)])); - // Check the cache - if ($cache->getItem($injectorId)->get() === null) { - trigger_error('Failed to verify the injector cache. See https://github.com/bearsunday/BEAR.Package/issues/418', E_USER_WARNING); - } + $injector = self::getInjector($meta, $context, $cache, $injectorId); } self::$instances[$injectorId] = $injector; @@ -86,4 +81,16 @@ public static function factory(AbstractAppMeta $meta, string $context, AbstractM return $injector; } + + private static function getInjector(AbstractAppMeta $meta, string $context, AdapterInterface $cache, string $injectorId): InjectorInterface + { + $injector = self::factory($meta, $context); + $cache->save($cache->getItem($injectorId)->set([$injector, new FileUpdate($meta)])); + // Check the cache + if ($cache->getItem($injectorId)->get() === null) { + trigger_error('Failed to verify the injector cache. See https://github.com/bearsunday/BEAR.Package/issues/418', E_USER_WARNING); + } + + return $injector; + } }