Skip to content

Commit

Permalink
Scrutinizer 10.00
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
koriym committed Jun 4, 2024
1 parent 9e7de07 commit 483e717
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Injector/PackageInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 483e717

Please sign in to comment.