Completed
Push — master ( 247290...01d05f )
by Michael
08:30
created

XoopsFormRenderer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 8
lcom 2
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 8 2
A __construct() 0 3 1
A __clone() 0 3 1
A __wakeup() 0 3 1
A set() 0 4 1
A get() 0 10 2
1
<?php
2
/**
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 * This program is distributed in the hope that it will be useful,
7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
 */
10
11
/**
12
 * Factory to build handlers
13
 *
14
 * @category  XoopsForm
15
 * @package   XoopsFormRenderer
16
 * @author    Richard Griffith <[email protected]>
17
 * @copyright 2017 XOOPS Project (http://xoops.org)
18
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
19
 * @link      http://xoops.org
20
 */
21
class XoopsFormRenderer
22
{
23
    /**
24
     * @var XoopsFormRenderer The reference to *Singleton* instance of this class
25
     */
26
    private static $instance;
27
28
    /**
29
     * @var XoopsFormRendererInterface The reference to *Singleton* instance of this class
30
     */
31
    protected $renderer;
32
33
    /**
34
     * Returns the *Singleton* instance of this class.
35
     *
36
     * @return XoopsFormRenderer the singleton instance.
37
     */
38
    public static function getInstance()
39
    {
40
        if (null === static::$instance) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
41
            static::$instance = new static();
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
42
        }
43
44
        return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
45
    }
46
47
    /**
48
     * Protected constructor to prevent creating a new instance of the
49
     * *Singleton* via the `new` operator from outside of this class.
50
     */
51
    protected function __construct()
52
    {
53
    }
54
55
    /**
56
     * Private clone method to prevent cloning of the instance of the
57
     * *Singleton* instance.
58
     *
59
     * @return void
60
     */
61
    private function __clone()
62
    {
63
    }
64
65
    /**
66
     * Private unserialize method to prevent unserializing of the *Singleton*
67
     * instance.
68
     *
69
     * @return void
70
     */
71
    private function __wakeup()
72
    {
73
    }
74
75
    /**
76
     * set the renderer
77
     *
78
     * @param XoopsFormRendererInterface $renderer instance of renderer
79
     *
80
     * @return void
81
     */
82
    public function set(XoopsFormRendererInterface $renderer)
83
    {
84
        $this->renderer = $renderer;
85
    }
86
87
    /**
88
     * get the renderer
89
     *
90
     * @return XoopsFormRendererInterface
91
     */
92
    public function get()
93
    {
94
        // return a default if not set
95
        if (null === $this->renderer) {
96
            xoops_load('xoopsformrendererlegacy');
97
            $this->renderer = new XoopsFormRendererLegacy();
98
        }
99
100
        return $this->renderer;
101
    }
102
}
103