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

StaticInstanceTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 6
dl 0
loc 23
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A instance() 0 9 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