Passed
Pull Request — master (#215)
by Wilmer
18:05 queued 03:12
created

SchemaCacheFactory::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 12
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\SchemaCache;
10
11
final class SchemaCacheFactory
12
{
13
    private static ?ContainerInterface $container = null;
14
    private static self $schemaCacheFactory;
15
16
    private function __construct(ContainerInterface $container = null)
17
    {
18
        self::$container = $container;
19
    }
20
21
    public static function initialize(ContainerInterface $container = null): void
22
    {
23
        self::$schemaCacheFactory = new self($container);
24
    }
25
26
    /**
27
     * Get `SchemaCache` instance.
28
     *
29
     * @throws RuntimeException If the get object is not an instance of the `SchemaCache`.
30
     *
31
     * @return SchemaCache The `SchemaCache` instance.
32
     *
33
     * @psalm-suppress RedundantConditionGivenDocblockType
34
     * @psalm-suppress DocblockTypeContradiction
35
     */
36
    public static function get(): SchemaCache
37
    {
38
        $schemaCache = self::$container->get(SchemaCache::class);
0 ignored issues
show
Bug introduced by
The method get() 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

38
        /** @scrutinizer ignore-call */ 
39
        $schemaCache = self::$container->get(SchemaCache::class);

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...
39
40
        if (!($schemaCache instanceof SchemaCache)) {
41
            throw new RuntimeException(sprintf(
42
                'The "%s" is not an instance of the "Psr\Log\LoggerInterface".',
43
                (is_object($schemaCache) ? get_class($schemaCache) : gettype($schemaCache))
44
            ));
45
        }
46
47
        return $schemaCache;
48
    }
49
}
50