SingletonTrait::__clone()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
namespace Bedd\Common\Traits;
3
4
/**
5
 * SingletonTrait
6
 */
7
trait SingletonTrait
8
{
9
    /**
10
     * Instance of itselfs
11
     * @var self
12
     */
13
    private static $instance = null;
14
15
    /**
16
     * Return the instance of the used class
17
     * @return self
18
     */
19
    public static function getInstance()
20
    {
21
        if (self::$instance === null) {
22
            self::$instance = new self();
23
        }
24
        return self::$instance;
25
    }
26
27
    /**
28
     * private constructor for calling an optional init-Method
29
     */
30
    final private function __construct()
31
    {
32
        $this->init();
0 ignored issues
show
Unused Code introduced by
The call to the method Bedd\Common\Traits\SingletonTrait::init() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
33
    }
34
35
    private function init()
36
    {
37
    }
38
    final private function __clone()
39
    {
40
    }
41
    final private function __wakeup()
42
    {
43
    }
44
}
45