Completed
Push — master ( 266444...2ebe1b )
by Michael
01:31
created

onupdate.php ➔ xoops_module_update_contact()   F

Complexity

Conditions 21
Paths 260

Size

Total Lines 137
Code Lines 65

Duplication

Lines 13
Ratio 9.49 %

Importance

Changes 0
Metric Value
cc 21
eloc 65
nc 260
nop 2
dl 13
loc 137
rs 3.6155
c 0
b 0
f 0

How to fix   Long Method    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 35 and the first side effect is on line 27.

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
 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
 * Contact module
14
 *
15
 * @copyright   XOOPS Project (https://xoops.org)
16
 * @license     http://www.fsf.org/copyleft/gpl.html GNU public license
17
 * @author      Kazumi Ono (aka Onokazu)
18
 * @author      Trabis <[email protected]>
19
 * @author      Hossein Azizabadi (AKA Voltan)
20
 * @param XoopsModule $module
21
 * @param $version
22
 */
23
24
if ((!defined('XOOPS_ROOT_PATH')) || !($GLOBALS['xoopsUser'] instanceof XoopsUser)
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
25
    || !$GLOBALS['xoopsUser']->IsAdmin()
26
) {
27
    exit('Restricted access' . PHP_EOL);
28
}
29
30
/**
31
 * @param string $tablename
32
 *
33
 * @return bool
34
 */
35
function tableExists($tablename)
0 ignored issues
show
Coding Style introduced by
tableExists 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...
36
{
37
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
38
39
    return $GLOBALS['xoopsDB']->getRowsNum($result) > 0;
40
}
41
42
/**
43
 *
44
 * Prepares system prior to attempting to install module
45
 * @param XoopsModule $module {@link XoopsModule}
46
 *
47
 * @return bool true if ready to install, false if not
48
 */
49
function xoops_module_pre_update_contact(XoopsModule $module)
50
{
51
    $moduleDirName = basename(dirname(__DIR__));
52
    /** @var ContactUtility $utilityClass */
53
    $utilityClass = ucfirst($moduleDirName) . 'Utility';
54
    if (!class_exists($utilityClass)) {
55
        xoops_load('utility', $moduleDirName);
56
    }
57
58
    $xoopsSuccess = $utilityClass::checkVerXoops($module);
59
    $phpSuccess   = $utilityClass::checkVerPhp($module);
60
61
    return $xoopsSuccess && $phpSuccess;
62
}
63
64
/**
65
 *
66
 * Performs tasks required during update of the module
67
 * @param XoopsModule $module {@link XoopsModule}
68
 * @param null        $previousVersion
69
 *
70
 * @return bool true if update successful, false if not
71
 */
72
function xoops_module_update_contact(XoopsModule $module, $previousVersion = null)
0 ignored issues
show
Coding Style introduced by
xoops_module_update_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...
73
{
74
    $moduleDirName = basename(dirname(__DIR__));
75
    $capsDirName   = strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
$capsDirName 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...
76
77
    $xoopsDB = XoopsDatabaseFactory::getDatabaseConnection();
78
79
    if ($previousVersion < 180) {
80
        $sql = 'CREATE TABLE ' . $xoopsDB->prefix('contact') . ' (
81
        contact_id int(10) unsigned NOT NULL auto_increment,
82
        contact_uid int(10) NOT NULL,
83
        contact_cid int(10) NOT NULL,
84
        contact_create int(10) NOT NULL,
85
        contact_subject varchar(255) NOT NULL,
86
        contact_name varchar(255) NOT NULL,
87
        contact_mail varchar(255) NOT NULL,
88
        contact_url varchar(255) NOT NULL,
89
        contact_icq varchar(255) NOT NULL,
90
        contact_company varchar(255) NOT NULL,
91
        contact_location varchar(255) NOT NULL,
92
        contact_department varchar(60) NOT NULL,
93
        contact_ip varchar(20) NOT NULL,
94
        contact_phone varchar(20) NOT NULL,
95
        contact_message text NOT NULL,
96
        contact_address text NOT NULL,
97
        contact_reply tinyint(1) NOT NULL,
98
        PRIMARY KEY  (contact_id)
99
        ) ENGINE=MyISAM;';
100
        $xoopsDB->query($sql);
101
    }
102
103
    if ($previousVersion < 181) {
104
        // Add contact_platform
105
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . "` ADD `contact_platform` ENUM('Android','Ios','Web') NOT NULL DEFAULT 'Web'";
106
        $xoopsDB->query($sql);
107
        // Add contact_type
108
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . "` ADD `contact_type` ENUM('Contact','Phone','Mail') NOT NULL DEFAULT 'Contact'";
109
        $xoopsDB->query($sql);
110
        // Add index contact_uid
111
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_uid` ( `contact_uid` )';
112
        $xoopsDB->query($sql);
113
        // Add index contact_cid
114
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_cid` ( `contact_cid` )';
115
        $xoopsDB->query($sql);
116
        // Add index contact_create
117
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_create` ( `contact_create` )';
118
        $xoopsDB->query($sql);
119
        // Add index contact_mail
120
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_mail` ( `contact_mail` )';
121
        $xoopsDB->query($sql);
122
        // Add index contact_phone
123
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_phone` ( `contact_phone` )';
124
        $xoopsDB->query($sql);
125
        // Add index contact_platform
126
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_platform` ( `contact_platform` )';
127
        $xoopsDB->query($sql);
128
        // Add index contact_type
129
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_type` ( `contact_type` )';
130
        $xoopsDB->query($sql);
131
    }
132
133
    if ($previousVersion < 226) {
134
        require_once __DIR__ . '/config.php';
135
        $configurator = new ContactConfigurator();
136
        /** @var ContactUtility $utilityClass */
137
        $utilityClass    = ucfirst($moduleDirName) . 'Utility';
138
        if (!class_exists($utilityClass)) {
139
            xoops_load('utility', $moduleDirName);
140
        }
141
142
        //delete old HTML templates
143
        if (count($configurator->templateFolders) > 0) {
144
            foreach ($configurator->templateFolders as $folder) {
145
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
146
                if (is_dir($templateFolder)) {
147
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
148
                    foreach ($templateList as $k => $v) {
149
                        $fileInfo = new SplFileInfo($templateFolder . $v);
150
                        if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
151
                            if (file_exists($templateFolder . $v)) {
152
                                unlink($templateFolder . $v);
153
                            }
154
                        }
155
                    }
156
                }
157
            }
158
        }
159
160
        //  ---  DELETE OLD FILES ---------------
161
        if (count($configurator->oldFiles) > 0) {
162
            //    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...
163
            foreach (array_keys($configurator->oldFiles) as $i) {
164
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
165
                if (is_file($tempFile)) {
166
                    unlink($tempFile);
167
                }
168
            }
169
        }
170
171
        //  ---  DELETE OLD FOLDERS ---------------
172
        xoops_load('XoopsFile');
173
        if (count($configurator->oldFolders) > 0) {
174
            //    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...
175
            foreach (array_keys($configurator->oldFolders) as $i) {
176
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
177
                /* @var $folderHandler XoopsObjectHandler */
178
                $folderHandler = XoopsFile::getHandler('folder', $tempFolder);
179
                $folderHandler->delete($tempFolder);
180
            }
181
        }
182
183
        //  ---  CREATE FOLDERS ---------------
184 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...
185
            //    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...
186
            foreach (array_keys($configurator->uploadFolders) as $i) {
187
                $utilityClass::createFolder($configurator->uploadFolders[$i]);
188
            }
189
        }
190
191
        //  ---  COPY blank.png FILES ---------------
192 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...
193
            $file = __DIR__ . '/../assets/images/blank.png';
194
            foreach (array_keys($configurator->blankFiles) as $i) {
195
                $dest = $configurator->blankFiles[$i] . '/blank.png';
196
                $utilityClass::copyFile($file, $dest);
197
            }
198
        }
199
200
        //delete .html entries from the tpl table
201
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . '\' AND `tpl_file` LIKE \'%.html%\'';
202
        $GLOBALS['xoopsDB']->queryF($sql);
203
204
        /** @var XoopsGroupPermHandler $gpermHandler */
205
        $gpermHandler = xoops_getHandler('groupperm');
206
        return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
207
    }
208
}
209