Completed
Push — master ( 2ebe1b...b0c184 )
by Michael
01:49
created

xoops_module_update_contact()   F

Complexity

Conditions 21
Paths 260

Size

Total Lines 142
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 21
eloc 67
nc 260
nop 2
dl 0
loc 142
rs 3.6155
c 2
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 type XoopsUser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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)
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
    /** @var Contact\Helper $helper */
52
    /** @var Contact\Utility $utility */
53
    $moduleDirName = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
54
    $helper       = Contact\Helper::getInstance();
0 ignored issues
show
Bug introduced by
The type Contact\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
55
    $utility      = new Contact\Utility();
0 ignored issues
show
Bug introduced by
The type Contact\Utility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
57
    $xoopsSuccess = $utility::checkVerXoops($module);
58
    $phpSuccess   = $utility::checkVerPhp($module);
59
    return $xoopsSuccess && $phpSuccess;
60
}
61
62
/**
63
 *
64
 * Performs tasks required during update of the module
65
 * @param XoopsModule $module {@link XoopsModule}
66
 * @param null        $previousVersion
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $previousVersion is correct as it would always require null to be passed?
Loading history...
67
 *
68
 * @return bool true if update successful, false if not
69
 */
70
function xoops_module_update_contact(\XoopsModule $module, $previousVersion = null)
71
{
72
    $moduleDirName = basename(dirname(__DIR__));
73
    $capsDirName   = strtoupper($moduleDirName);
0 ignored issues
show
Unused Code introduced by
The assignment to $capsDirName is dead and can be removed.
Loading history...
74
75
    /** @var Contact\Helper $helper */
76
    /** @var Contact\Utility $utility */
77
    /** @var Contact\Configurator $configurator */
78
    $helper  = Contact\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
79
    $utility = new Contact\Utility();
0 ignored issues
show
Unused Code introduced by
The assignment to $utility is dead and can be removed.
Loading history...
80
    $configurator = new Contact\Configurator();
0 ignored issues
show
Unused Code introduced by
The assignment to $configurator is dead and can be removed.
Loading history...
Bug introduced by
The type Contact\Configurator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
82
    $xoopsDB = \XoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Bug introduced by
The type XoopsDatabaseFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
83
84
    if ($previousVersion < 180) {
85
        $sql = 'CREATE TABLE ' . $xoopsDB->prefix('contact') . ' (
86
        contact_id int(10) unsigned NOT NULL auto_increment,
87
        contact_uid int(10) NOT NULL,
88
        contact_cid int(10) NOT NULL,
89
        contact_create int(10) NOT NULL,
90
        contact_subject varchar(255) NOT NULL,
91
        contact_name varchar(255) NOT NULL,
92
        contact_mail varchar(255) NOT NULL,
93
        contact_url varchar(255) NOT NULL,
94
        contact_icq varchar(255) NOT NULL,
95
        contact_company varchar(255) NOT NULL,
96
        contact_location varchar(255) NOT NULL,
97
        contact_department varchar(60) NOT NULL,
98
        contact_ip varchar(20) NOT NULL,
99
        contact_phone varchar(20) NOT NULL,
100
        contact_message text NOT NULL,
101
        contact_address text NOT NULL,
102
        contact_reply tinyint(1) NOT NULL,
103
        PRIMARY KEY  (contact_id)
104
        ) ENGINE=MyISAM;';
105
        $xoopsDB->query($sql);
106
    }
107
108
    if ($previousVersion < 181) {
109
        // Add contact_platform
110
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . "` ADD `contact_platform` ENUM('Android','Ios','Web') NOT NULL DEFAULT 'Web'";
111
        $xoopsDB->query($sql);
112
        // Add contact_type
113
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . "` ADD `contact_type` ENUM('Contact','Phone','Mail') NOT NULL DEFAULT 'Contact'";
114
        $xoopsDB->query($sql);
115
        // Add index contact_uid
116
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_uid` ( `contact_uid` )';
117
        $xoopsDB->query($sql);
118
        // Add index contact_cid
119
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_cid` ( `contact_cid` )';
120
        $xoopsDB->query($sql);
121
        // Add index contact_create
122
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_create` ( `contact_create` )';
123
        $xoopsDB->query($sql);
124
        // Add index contact_mail
125
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_mail` ( `contact_mail` )';
126
        $xoopsDB->query($sql);
127
        // Add index contact_phone
128
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_phone` ( `contact_phone` )';
129
        $xoopsDB->query($sql);
130
        // Add index contact_platform
131
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_platform` ( `contact_platform` )';
132
        $xoopsDB->query($sql);
133
        // Add index contact_type
134
        $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_type` ( `contact_type` )';
135
        $xoopsDB->query($sql);
136
    }
137
138
    if ($previousVersion < 227) {
139
        require_once __DIR__ . '/config.php';
140
        $configurator = new ContactConfigurator();
141
        /** @var ContactUtility $utility */
142
        $utility    = ucfirst($moduleDirName) . 'Utility';
143
        if (!class_exists($utility)) {
144
            xoops_load('utility', $moduleDirName);
0 ignored issues
show
Bug introduced by
The function xoops_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
            /** @scrutinizer ignore-call */ 
145
            xoops_load('utility', $moduleDirName);
Loading history...
145
        }
146
147
        //delete old HTML templates
148
        if (count($configurator->templateFolders) > 0) {
149
            foreach ($configurator->templateFolders as $folder) {
150
                $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder);
151
                if (is_dir($templateFolder)) {
152
                    $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']);
153
                    foreach ($templateList as $k => $v) {
154
                        $fileInfo = new \SplFileInfo($templateFolder . $v);
155
                        if ('html' === $fileInfo->getExtension() && 'index.html' !== $fileInfo->getFilename()) {
156
                            if (file_exists($templateFolder . $v)) {
157
                                unlink($templateFolder . $v);
158
                            }
159
                        }
160
                    }
161
                }
162
            }
163
        }
164
165
        //  ---  DELETE OLD FILES ---------------
166
        if (count($configurator->oldFiles) > 0) {
167
            //    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...
168
            foreach (array_keys($configurator->oldFiles) as $i) {
169
                $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]);
170
                if (is_file($tempFile)) {
171
                    unlink($tempFile);
172
                }
173
            }
174
        }
175
176
        //  ---  DELETE OLD FOLDERS ---------------
177
        xoops_load('XoopsFile');
178
        if (count($configurator->oldFolders) > 0) {
179
            //    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...
180
            foreach (array_keys($configurator->oldFolders) as $i) {
181
                $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]);
182
                /* @var $folderHandler XoopsObjectHandler */
183
                $folderHandler = XoopsFile::getHandler('folder', $tempFolder);
0 ignored issues
show
Bug introduced by
The type XoopsFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
184
                $folderHandler->delete($tempFolder);
185
            }
186
        }
187
188
        //  ---  CREATE FOLDERS ---------------
189
        if (count($configurator->uploadFolders) > 0) {
190
            //    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...
191
            foreach (array_keys($configurator->uploadFolders) as $i) {
192
                $utility::createFolder($configurator->uploadFolders[$i]);
193
            }
194
        }
195
196
        //  ---  COPY blank.png FILES ---------------
197
        if (count($configurator->copyBlankFiles) > 0) {
0 ignored issues
show
Bug introduced by
The property copyBlankFiles does not seem to exist on ContactConfigurator.
Loading history...
198
            $file = __DIR__ . '/../assets/images/blank.png';
199
            foreach (array_keys($configurator->copyBlankFiles) as $i) {
200
                $dest = $configurator->copyBlankFiles[$i] . '/blank.png';
201
                $utility::copyFile($file, $dest);
202
            }
203
        }
204
205
        //delete .html entries from the tpl table
206
        $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . '\' AND `tpl_file` LIKE \'%.html%\'';
207
        $GLOBALS['xoopsDB']->queryF($sql);
208
209
        /** @var XoopsGroupPermHandler $grouppermHandler */
210
        $grouppermHandler = xoops_getHandler('groupperm');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

210
        $grouppermHandler = /** @scrutinizer ignore-call */ xoops_getHandler('groupperm');
Loading history...
211
        return $grouppermHandler->deleteByModule($module->getVar('mid'), 'item_read');
212
    }
213
}
214