Completed
Push — 2.1-master-merge ( 240673 )
by Alexander
13:45
created

StaticInstanceTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 22
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A instance() 0 8 3
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\base;
9
10
use Yii;
11
12
/**
13
 * StaticInstanceTrait provides methods to satisfy [[StaticInstanceInterface]] interface.
14
 *
15
 * @see StaticInstanceInterface
16
 *
17
 * @author Paul Klimov <[email protected]>
18
 * @since 2.0.13
19
 */
20
trait StaticInstanceTrait
21
{
22
    /**
23
     * @var static[] static instances in format: `[className => object]`
24
     */
25
    private static $_instances = [];
26
27
28
    /**
29
     * Returns static class instance, which can be used to obtain meta information.
30
     * @param bool $refresh whether to re-create static instance even, if it is already cached.
31
     * @return static class instance.
32
     */
33 67
    public static function instance($refresh = false)
34
    {
35 67
        $className = get_called_class();
36 67
        if ($refresh || !isset(self::$_instances[$className])) {
37 5
            self::$_instances[$className] = Yii::createObject($className);
38
        }
39 67
        return self::$_instances[$className];
40
    }
41
}