Passed
Pull Request — master (#215)
by Wilmer
11:32
created

QueryCacheFactory::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Factory;
6
7
use Psr\Container\ContainerInterface;
8
use RuntimeException;
9
use Yiisoft\Db\Cache\QueryCache;
10
use Yiisoft\Factory\Definitions\DefinitionInterface;
11
use Yiisoft\Factory\Definitions\Normalizer;
12
use Yiisoft\Factory\Factory;
13
14
final class QueryCacheFactory extends Factory
15
{
16
    private static ?ContainerInterface $container = null;
17
    private static ?DefinitionInterface $definition = null;
18
    private static Factory $queryCacheFactory;
19
20
    private function __construct(ContainerInterface $container = null)
21
    {
22
        self::$container = $container;
23
        self::$definition = Normalizer::normalize(QueryCache::class);
24
    }
25
26
    public static function initialize(ContainerInterface $container = null): void
27
    {
28
        self::$queryCacheFactory = new self($container);
29
    }
30
31
    /**
32
     * Creates a `QueryCache` instance.
33
     *
34
     * @throws RuntimeException If the created object is not an instance of the `QueryCache`.
35
     *
36
     * @return QueryCache The `QueryCache` instance.
37
     *
38
     * @psalm-suppress RedundantConditionGivenDocblockType
39
     * @psalm-suppress DocblockTypeContradiction
40
     */
41
    public static function run(): QueryCache
42
    {
43
        $queryCache = self::$definition->resolve(self::$container);
0 ignored issues
show
Bug introduced by
The method resolve() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        /** @scrutinizer ignore-call */ 
44
        $queryCache = self::$definition->resolve(self::$container);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like self::container can also be of type null; however, parameter $container of Yiisoft\Factory\Definiti...ionInterface::resolve() does only seem to accept Psr\Container\ContainerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
        $queryCache = self::$definition->resolve(/** @scrutinizer ignore-type */ self::$container);
Loading history...
44
45
        if (!($queryCache instanceof QueryCache)) {
46
            throw new RuntimeException(sprintf(
47
                'The "%s" is not an instance of the "Psr\Log\LoggerInterface".',
48
                (is_object($queryCache) ? get_class($queryCache) : gettype($queryCache))
49
            ));
50
        }
51
52
        return $queryCache;
53
    }
54
}
55