Completed
Push — master ( fe4c2e...b05117 )
by Michael
12s
created

AbstractHelper::__construct()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 24
rs 8.5125
ccs 0
cts 13
cp 0
cc 5
eloc 14
nc 8
nop 1
crap 30
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
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf\Module\Helper;
13
14
/**
15
 * Xmf\Module\Helper\AbstractHelper defines the basis for various
16
 * helpers that simplify routine module tasks.
17
 *
18
 * @category  Xmf\Module\Helper\AbstractHelper
19
 * @package   Xmf
20
 * @author    trabis <[email protected]>
21
 * @author    Richard Griffith <[email protected]>
22
 * @copyright 2011-2016 XOOPS Project (http://xoops.org)
23
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24
 * @version   Release: 1.0
25
 * @link      http://xoops.org
26
 * @since     1.0
27
 */
28
abstract class AbstractHelper
29
{
30
    /**
31
     * @var XoopsModule
32
     */
33
    protected $module;
34
35
    /**
36
     * @var bool true if debug is enabled
37
     */
38
    protected $debug;
39
40
    /**
41
     * Instantiate a XoopsModule object for the helper to use.
42
     * The module is determined as follows:
43
     * - if null is passed, use the current module
44
     * - if a string is passed, use as dirname to load
45
     *
46
     * @param string|null $dirname dirname
47
     */
48
    public function __construct($dirname = null)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
49
    {
50
        $this->module = null;
51
52
        if (empty($dirname)) {
53
            // nothing specified, use current module
54
            // check if we are running in 2.6
55
            if (class_exists('Xoops', false)) {
56
                $xoops = \Xoops::getInstance();
57
                if ($xoops->isModule()) {
58
                    $this->module = $xoops->module;
0 ignored issues
show
Documentation Bug introduced by
It seems like $xoops->module can also be of type object<Xoops\Core\Kernel\Handlers\XoopsModule>. However, the property $module is declared as type object<Xmf\Module\Helper\XoopsModule>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
                }
60
            } else {
61
                $this->module = $GLOBALS['xoopsModule'];
62
            }
63
        } else {
64
            // assume dirname specified, try to get a module object
65
            $moduleHandler = xoops_getHandler('module');
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getHandler() has been deprecated.

This function has been deprecated.

Loading history...
66
            $this->module = $moduleHandler->getByDirname($dirname);
67
        }
68
        if (is_object($this->module)) {
69
            $this->init();
70
        }
71
    }
72
73
    /**
74
     * init() is called once/if __construct has a module object.
75
     * $this->module will have a module object that any further
76
     * initialization can use.
77
     *
78
     * @return void
79
     */
80
    abstract public function init();
81
82
    /**
83
     * Set debug option on or off
84
     *
85
     * @param bool $bool true to turn on debug logging, false for off
86
     *
87
     * @return void
88
     */
89 1
    public function setDebug($bool = true)
90
    {
91 1
        $this->debug = (bool) $bool;
92 1
    }
93
94
    /**
95
     * Add a message to the module log
96
     *
97
     * @param string $log log message
98
     *
99
     * @return void
100
     */
101 1
    public function addLog($log)
102
    {
103 1
        if ($this->debug) {
104
            \Xoops::getInstance()->logger()->debug($log, array('channel'=>'Extra'));
105
        }
106 1
    }
107
}
108