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 f0bcb48
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 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,15 @@ public static function factory(AbstractAppMeta $meta, string $context, AbstractM

return $injector;
}

private static function getInjector(AbstractAppMeta $meta, string $context, CacheInterface|AdapterInterface $cache, string $injectorId): InjectorInterface
{
$injector = self::factory($meta, $context);
$cache->save($cache->getItem($injectorId)->set([$injector, new FileUpdate($meta)]));

Check failure on line 88 in src/Injector/PackageInjector.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Call to an undefined method Symfony\Component\Cache\Adapter\AdapterInterface|Symfony\Contracts\Cache\CacheInterface::getItem().

Check failure on line 88 in src/Injector/PackageInjector.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Call to an undefined method Symfony\Component\Cache\Adapter\AdapterInterface|Symfony\Contracts\Cache\CacheInterface::save().

Check failure on line 88 in src/Injector/PackageInjector.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

PossiblyUndefinedMethod: Method Symfony\Contracts\Cache\CacheInterface::save does not exist

Check failure on line 88 in src/Injector/PackageInjector.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

MixedArgument: Argument 1 of Symfony\Component\Cache\Adapter\AdapterInterface::save cannot be mixed, expecting Psr\Cache\CacheItemInterface

Check failure on line 88 in src/Injector/PackageInjector.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

PossiblyUndefinedMethod: Method Symfony\Contracts\Cache\CacheInterface::getItem does not exist

Check failure on line 88 in src/Injector/PackageInjector.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

MixedMethodCall: Cannot determine the type of the object on the left hand side of this expression
// Check the cache
if ($cache->getItem($injectorId)->get() === null) {

Check failure on line 90 in src/Injector/PackageInjector.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Expected 1 line after "if", found 0.

Check failure on line 90 in src/Injector/PackageInjector.php

View workflow job for this annotation

GitHub Actions / sa / PHPStan

Call to an undefined method Symfony\Component\Cache\Adapter\AdapterInterface|Symfony\Contracts\Cache\CacheInterface::getItem().

Check failure on line 90 in src/Injector/PackageInjector.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

PossiblyUndefinedMethod: Method Symfony\Contracts\Cache\CacheInterface::getItem does not exist

Check failure on line 90 in src/Injector/PackageInjector.php

View workflow job for this annotation

GitHub Actions / sa / Psalm

MixedMethodCall: Cannot determine the type of the object on the left hand side of this expression
trigger_error('Failed to verify the injector cache. See https://github.com/bearsunday/BEAR.Package/issues/418', E_USER_WARNING);
}
return $injector;

Check failure on line 93 in src/Injector/PackageInjector.php

View workflow job for this annotation

GitHub Actions / cs / Coding Standards

Expected 1 line before "return", found 0.
}
}

0 comments on commit f0bcb48

Please sign in to comment.