Passed
Push — master ( ae9b6b...aec4ed )
by Wilmer
11:33 queued 09:14
created

StaticInstanceTrait::instance()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord;
6
7
/**
8
 * StaticInstanceTrait provides methods to satisfy [[StaticInstanceInterface]] interface.
9
 *
10
 * @see StaticInstanceInterface
11
 */
12
trait StaticInstanceTrait
13
{
14
    /**
15
     * @var static[] static instances in format: `[className => object]`
16
     */
17
    private static $instances = [];
18
19
20
    /**
21
     * Returns static class instance, which can be used to obtain meta information.
22
     *
23
     * @param bool $refresh whether to re-create static instance even, if it is already cached.
24
     * @return static class instance.
25
     */
26 45
    public static function instance($refresh = false)
27
    {
28 45
        $className = \get_called_class();
29
30 45
        if ($refresh || !isset(self::$instances[$className])) {
31 3
            self::$instances[$className] = new $className();
32
        }
33
34 45
        return self::$instances[$className];
35
    }
36
}
37