SingletonTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 38
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 7 2
A __construct() 0 4 1
A init() 0 3 1
A __clone() 0 3 1
A __wakeup() 0 3 1
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