Completed
Pull Request — master (#6)
by Michael
01:29
created

functions.php ➔ myiframe_getmoduleoption()   D

Complexity

Conditions 10
Paths 6

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 19
nc 6
nop 2
dl 0
loc 29
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * ****************************************************************************
4
 * MYIFRAME - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
6
 * ****************************************************************************
7
 */
8
9
if (!defined('XOOPS_ROOT_PATH')) {
10
    die('XOOPS root path not defined');
11
}
12
13
/**
14
 * Returns a module's option
15
 *
16
 * Return's a module's option (for the myiframe module)
17
 *
18
 * @package      Myiframe
19
 * @author       Instant Zero (http://www.instant-zero.com)
20
 * @copyright    Instant Zero (http://www.instant-zero.com)
21
 * @param string $option module option's name
22
 * @param string $repmodule
23
 * @return bool
24
 */
25
function myiframe_getmoduleoption($option, $repmodule = 'myiframe')
26
{
27
    global $xoopsModuleConfig, $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
28
    static $tbloptions = [];
29
    if (is_array($tbloptions) && array_key_exists($option, $tbloptions)) {
30
        return $tbloptions[$option];
31
    }
32
33
    $retval = false;
34
    if (isset($xoopsModuleConfig) && (is_object($xoopsModule) && $xoopsModule->getVar('dirname') == $repmodule && $xoopsModule->getVar('isactive'))) {
35
        if (isset($xoopsModuleConfig[$option])) {
36
            $retval = $xoopsModuleConfig[$option];
37
        }
38
    } else {
39
        /** @var \XoopsModuleHandler $moduleHandler */
40
        $moduleHandler = xoops_getHandler('module');
41
        $module        = $moduleHandler->getByDirname($repmodule);
42
        /** @var \XoopsConfigHandler $configHandler */
43
        $configHandler = xoops_getHandler('config');
44
        if ($module) {
45
            $moduleConfig = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
46
            if (isset($moduleConfig[$option])) {
47
                $retval = $moduleConfig[$option];
48
            }
49
        }
50
    }
51
    $tbloptions[$option] = $retval;
52
    return $retval;
53
}
54
55
/**
56
 * Verify that a field exists inside a mysql table
57
 *
58
 * @package      Myiframe
59
 * @author       Instant Zero (http://www.instant-zero.com)
60
 * @copyright    Instant Zero (http://www.instant-zero.com)
61
 * @param $fieldname
62
 * @param $table
63
 * @return bool
64
 */
65
function myiframe_FieldExists($fieldname, $table)
0 ignored issues
show
Coding Style introduced by
myiframe_FieldExists 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...
66
{
67
    $result = $GLOBALS['xoopsDB']->queryF("SHOW COLUMNS FROM	$table LIKE '$fieldname'");
68
    return ($GLOBALS['xoopsDB']->getRowsNum($result) > 0);
69
}
70
71
/**
72
 * Set the page's title, meta description and meta keywords
73
 * Datas are supposed to be sanitized
74
 *
75
 * @package          Myiframe
76
 * @author           Instant Zero http://www.instant-zero.com
77
 * @copyright    (c) Instant Zero http://www.instant-zero.com
78
 *
79
 * @param string $page_title       Page's Title
80
 * @param string $meta_description Page's meta description
81
 * @param string $meta_keywords    Page's meta keywords
82
 * @return none
0 ignored issues
show
Documentation introduced by
Should the return type not be none|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
83
 */
84
function myiframe_set_metas($page_title = '', $meta_description = '', $meta_keywords = '')
85
{
86
    global $xoTheme, $xoTheme, $xoopsTpl;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
87
    $xoopsTpl->assign('xoops_pagetitle', $page_title);
88
    if (isset($xoTheme) && is_object($xoTheme)) {
89
        if (!empty($meta_keywords)) {
90
            $xoTheme->addMeta('meta', 'keywords', $meta_keywords);
91
        }
92
        if (!empty($meta_description)) {
93
            $xoTheme->addMeta('meta', 'description', $meta_description);
94
        }
95
    } elseif (isset($xoopsTpl) && is_object($xoopsTpl)) {    // Compatibility for old Xoops versions
96
        if (!empty($meta_keywords)) {
97
            $xoopsTpl->assign('xoops_meta_keywords', $meta_keywords);
98
        }
99
        if (!empty($meta_description)) {
100
            $xoopsTpl->assign('xoops_meta_description', $meta_description);
101
        }
102
    }
103
}
104