Completed
Push — master ( 59763f...201c15 )
by Fran
05:04 queued 15s
created

Singleton::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace CloudFramework\Patterns;
3
4
5
/**
6
 * Class Singleton
7
 * @package CloudFramework\Patterns
8
 */
9
class Singleton
10
{
11
12
    private static $instance = array();
13
    /**
14
     * @var float $loadTs
15
     */
16
    protected $loadTs = 0;
17
    /**
18
     * @var float $loadMem
19
     */
20
    protected $loadMem = 0;
21
22
    /**
23
     * @var bool Flag thath indicates that actual class is already loaded
24
     */
25
    protected $loaded = false;
26
27
    public function __construct()
28
    {
29
    }
30
31
    /**
32
     * Singleton instance generator
33
     * @return $this
34
     */
35 1
    public static function getInstance()
36
    {
37 1
        $ts = microtime(true);
0 ignored issues
show
Unused Code introduced by
$ts is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
38 1
        $class = get_called_class();
39 1
        if (!array_key_exists($class, self::$instance) || null === self::$instance[$class]) {
40 1
            self::$instance[$class] = new $class(func_get_args());
41 1
        }
42 1
        return self::$instance[$class];
43
    }
44
45
    /**
46
     * Instance generator alias
47
     * @return $this
48
     */
49
    public static function create()
50
    {
51
        return self::getInstance(func_get_args());
52
    }
53
54
    /**
55
     * Magic setter
56
     * @param string $variable
57
     * @param mixed $value
58
     */
59
    public function __set($variable, $value)
60
    {
61
        $this->$variable = $value;
62
    }
63
64
    /**
65
     * Magic getter
66
     * @param string $variable
67
     * @return mixed
68
     */
69
    public function __get($variable)
70
    {
71
        return $this->$variable;
72
    }
73
74
    /**
75
     * Prevent the instance from being cloned
76
     * @return void
77
     */
78
    private function __clone() {}
79
80
    public function __toString()
81
    {
82
        $size = round(strlen(print_r($this, true)) / 1024, 4);
83
        return get_class($this) . " " . $size . " Kbytes";
84
    }
85
}
86