Completed
Push — master ( 60e9af...3a9521 )
by Cees-Jan
01:55
created

AsyncTableRegistry::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace WyriHaximus\React\Cake\Orm;
4
5
use Cake\Core\App;
6
use React\EventLoop\LoopInterface;
7
use WyriHaximus\React\ChildProcess\Pool\PoolUtilizerInterface;
8
9
class AsyncTableRegistry implements PoolUtilizerInterface
10
{
11
    /**
12
     * @var AsyncTable[]
13
     */
14
    protected static $tables = [];
15
16
    /**
17
     * @param LoopInterface $loop
18
     */
19 2
    public static function init(LoopInterface $loop)
20
    {
21 2
        Pool::getInstance($loop);
22 2
    }
23
24
    /**
25
     * @param $tableName
26
     *
27
     * @return AsyncTable
28
     */
29 1
    public static function get($tableName)
30
    {
31 1
        if (isset(static::$tables[$tableName])) {
32 1
            return static::$tables[$tableName];
33
        }
34
35 1
        static::$tables[$tableName] = new AsyncTable(
36 1
            Pool::getInstance(),
0 ignored issues
show
Bug introduced by
It seems like \WyriHaximus\React\Cake\Orm\Pool::getInstance() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
37 1
            $tableName,
38 1
            App::className($tableName, 'Model/Table', 'Table')
39 1
        );
40 1
        return static::$tables[$tableName];
41
    }
42
43 3
    public static function getInstance()
44
    {
45 3
        static $instance = null;
46 3
        if (null === $instance) {
47 1
            $instance = new static();
48 1
        }
49
50 3
        return $instance;
51
    }
52
53 18
    public static function reset()
54
    {
55 18
        static $instance = null;
56 18
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61 1
    public function info()
62
    {
63 1
        return Pool::getInstance()->info();
64
    }
65
}
66