Completed
Push — master ( 720064...d5ad83 )
by Michael
02:05
created

oninstall.php ➔ xoops_module_install_contact()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 58
Code Lines 34

Duplication

Lines 13
Ratio 22.41 %

Importance

Changes 0
Metric Value
cc 8
eloc 34
nc 32
nop 1
dl 13
loc 58
rs 7.1977
c 0
b 0
f 0

How to fix   Long Method   

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
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
/**
13
 * @copyright    XOOPS Project http://xoops.org/
14
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team
18
 */
19
20
//require_once __DIR__ . '/setup.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
21
22
/**
23
 *
24
 * Prepares system prior to attempting to install module
25
 * @param XoopsModule $module {@link XoopsModule}
26
 *
27
 * @return bool true if ready to install, false if not
28
 */
29
function xoops_module_pre_install_contact(XoopsModule $module)
0 ignored issues
show
Coding Style introduced by
xoops_module_pre_install_contact 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...
30
{
31
    $moduleDirName = basename(dirname(__DIR__));
32
    $classUtility     = ucfirst($moduleDirName) . 'Utility';
33
    if (!class_exists($classUtility)) {
34
        xoops_load('utility', $moduleDirName);
35
    }
36
    //check for minimum XOOPS version
37
    if (!$classUtility::checkVerXoops($module)) {
38
        return false;
39
    }
40
41
    // check for minimum PHP version
42
    if (!$classUtility::checkVerPhp($module)) {
43
        return false;
44
    }
45
46
    $mod_tables =& $module->getInfo('tables');
47
    foreach ($mod_tables as $table) {
48
        $GLOBALS['xoopsDB']->queryF('DROP TABLE IF EXISTS ' . $GLOBALS['xoopsDB']->prefix($table) . ';');
49
    }
50
51
    return true;
52
}
53
54
/**
55
 *
56
 * Performs tasks required during installation of the module
57
 * @param XoopsModule $module {@link XoopsModule}
58
 *
59
 * @return bool true if installation successful, false if not
60
 */
61
function xoops_module_install_contact(XoopsModule $module)
0 ignored issues
show
Coding Style introduced by
xoops_module_install_contact 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...
62
{
63
    require_once  __DIR__ . '/../../../mainfile.php';
64
    require_once  __DIR__ . '/../include/config.php';
65
66
    if (!isset($moduleDirName)) {
0 ignored issues
show
Bug introduced by
The variable $moduleDirName seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
67
        $moduleDirName = basename(dirname(__DIR__));
68
    }
69
70
    if (false !== ($moduleHelper = Xmf\Module\Helper::getHelper($moduleDirName))) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
71
    } else {
72
        $moduleHelper = Xmf\Module\Helper::getHelper('system');
73
    }
74
75
    // Load language files
76
    $moduleHelper->loadLanguage('admin');
77
    $moduleHelper->loadLanguage('modinfo');
78
79
    $configurator = new ContactConfigurator();
80
    $classUtility    = ucfirst($moduleDirName) . 'Utility';
81
    if (!class_exists($classUtility)) {
82
        xoops_load('utility', $moduleDirName);
83
    }
84
85
    // default Permission Settings ----------------------
86
    global $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...
87
    $moduleId     = $xoopsModule->getVar('mid');
88
    $moduleId2    = $moduleHelper->getModule()->mid();
0 ignored issues
show
Unused Code introduced by
$moduleId2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
    $gpermHandler = xoops_getHandler('groupperm');
90
    // access rights ------------------------------------------
91
    $gpermHandler->addRight($moduleDirName . '_approve', 1, XOOPS_GROUP_ADMIN, $moduleId);
92
    $gpermHandler->addRight($moduleDirName . '_submit', 1, XOOPS_GROUP_ADMIN, $moduleId);
93
    $gpermHandler->addRight($moduleDirName . '_view', 1, XOOPS_GROUP_ADMIN, $moduleId);
94
    $gpermHandler->addRight($moduleDirName . '_view', 1, XOOPS_GROUP_USERS, $moduleId);
95
    $gpermHandler->addRight($moduleDirName . '_view', 1, XOOPS_GROUP_ANONYMOUS, $moduleId);
96
97
    //  ---  CREATE FOLDERS ---------------
98 View Code Duplication
    if (count($configurator->uploadFolders) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
        //    foreach (array_keys($GLOBALS['uploadFolders']) as $i) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
100
        foreach (array_keys($configurator->uploadFolders) as $i) {
101
            $classUtility::createFolder($configurator->uploadFolders[$i]);
102
        }
103
    }
104
105
    //  ---  COPY blank.png FILES ---------------
106 View Code Duplication
    if (count($configurator->blankFiles) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
        $file = __DIR__ . '/../assets/images/blank.png';
108
        foreach (array_keys($configurator->blankFiles) as $i) {
109
            $dest = $configurator->blankFiles[$i] . '/blank.png';
110
            $classUtility::copyFile($file, $dest);
111
        }
112
    }
113
    //delete .html entries from the tpl table
114
    $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $xoopsModule->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
115
    $GLOBALS['xoopsDB']->queryF($sql);
116
117
    return true;
118
}
119
120