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

SchemaCacheFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 3 1
A run() 0 12 3
A __construct() 0 4 1
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\SchemaCache;
10
use Yiisoft\Factory\Definitions\DefinitionInterface;
11
use Yiisoft\Factory\Definitions\Normalizer;
12
use Yiisoft\Factory\Factory;
13
14
final class SchemaCacheFactory extends Factory
15
{
16
    private static ?ContainerInterface $container = null;
17
    private static ?DefinitionInterface $definition = null;
18
    private static Factory $schemaCacheFactory;
19
20
    private function __construct(ContainerInterface $container = null)
21
    {
22
        self::$container = $container;
23
        self::$definition = Normalizer::normalize(SchemaCache::class);
24
    }
25
26
    public static function initialize(ContainerInterface $container = null): void
27
    {
28
        self::$schemaCacheFactory = new self($container);
29
    }
30
31
    /**
32
     * Creates a `SchemaCache` instance.
33
     *
34
     * @throws RuntimeException If the created object is not an instance of the `SchemaCache`.
35
     *
36
     * @return SchemaCache The `SchemaCache` instance.
37
     *
38
     * @psalm-suppress RedundantConditionGivenDocblockType
39
     * @psalm-suppress DocblockTypeContradiction
40
     */
41
    public static function run(): SchemaCache
42
    {
43
        $schemaCache = 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
        $schemaCache = 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
        $schemaCache = self::$definition->resolve(/** @scrutinizer ignore-type */ self::$container);
Loading history...
44
45
        if (!($schemaCache instanceof SchemaCache)) {
46
            throw new RuntimeException(sprintf(
47
                'The "%s" is not an instance of the "Psr\Log\LoggerInterface".',
48
                (is_object($schemaCache) ? get_class($schemaCache) : gettype($schemaCache))
49
            ));
50
        }
51
52
        return $schemaCache;
53
    }
54
}
55