Completed
Push — master ( 9a4ef0...878baf )
by
unknown
01:47
created

onupdate.php ➔ xoops_module_update_smallworld()   F

Complexity

Conditions 20
Paths 16632

Size

Total Lines 60

Duplication

Lines 12
Ratio 20 %

Importance

Changes 0
Metric Value
cc 20
nc 16632
nop 1
dl 12
loc 60
rs 0
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
2
/*
3
 You may not change or alter any portion of this comment or credits of
4
 supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit
6
 authors.
7
8
 This program is distributed in the hope that it will be useful, but
9
 WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * Module: xForms
15
 *
16
 * @package   \XoopsModules\Smallworld
17
 * @author    Richard Griffith <[email protected]>
18
 * @author    trabis <[email protected]>
19
 * @author    XOOPS Module Development Team
20
 * @copyright Copyright (c) 2001-2020 {@link https://xoops.org XOOPS Project}
21
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GNU Public License
22
 * @link      https://github.com/XoopsModules25x/smallworld
23
 */
24
25
use XoopsModules\Smallworld;
26
use XoopsModules\Smallworld\Helper;
27
use XoopsModules\Smallworld\Utility;
28
29
/**
30
 * @internal {Make sure you PROTECT THIS FILE}
31
 */
32
if ((!defined('XOOPS_ROOT_PATH'))
33
    || !($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...
34
    || !($GLOBALS['xoopsUser']->isAdmin())) {
35
    exit('Restricted access' . PHP_EOL);
36
}
37
/**
38
 * Prepares system prior to attempting to update module
39
 *
40
 * @param \XoopsModule $module {@link \XoopsModule}
41
 * @return bool true if ready to install, false if not
42
 */
43
function xoops_module_pre_update_smallworld(\XoopsModule $module)
44
{
45
    /**
46
     * @var Smallworld\Helper  $helper
47
     * @var Smallworld\Utility $utility
48
     */
49
    $helper       = Smallworld\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
$helper 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...
50
    $utility      = new Smallworld\Utility();
51
    $xoopsSuccess = $utility::checkVerXoops($module);
52
    $phpSuccess   = $utility::checkVerPhp($module);
53
54
    return $xoopsSuccess && $phpSuccess;
55
}
56
57
/**
58
 * Update Smallworld module
59
 *
60
 * @param \XoopsModule $module
61
 * @return bool true on succeess, false on failure
62
 */
63
function xoops_module_update_smallworld(\XoopsModule $module)
0 ignored issues
show
Unused Code introduced by
The parameter $module is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
{
65
    $tables = new \Xmf\Database\Tables();
66
67
    $cTable   = 'config';
68
    $criteria = new \Criteria('conf_name', 'smallworldprivorpub', '=');
69
    if ($success = $tables->useTable($cTable)) {
70
        $success = $tables->update($cTable, ['conf_value' => 1], $criteria);
71
    }
72
73
    $msgTable = 'smallworld_messages';
74
    $column   = 'message';
75 View Code Duplication
    if ($successA = $tables->useTable($msgTable)) { // if this returns false, there is no table
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...
76
        $attributes = $tables->getColumnAttributes($msgTable, $column);
77
        if (false === mb_strpos($attributes, ' TEXT')) { // assumes it's already been updated if TEXT found
78
            $successA = $tables->alterColumn($msgTable, $column, ' TEXT ');
79
        }
80
    }
81
82
    $comTable = 'smallworld_comments';
83
    $column   = 'comment';
84 View Code Duplication
    if ($successB = $tables->useTable($comTable)) { // if this returns false, there is no table
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...
85
        $attributes = $tables->getColumnAttributes($comTable, $column);
86
        if (false === mb_strpos($attributes, ' TEXT')) { // assumes it's already been updated if 'TEXT' not found
87
            $successB = $tables->alterColumn($comTable, $column, ' TEXT ');
88
        }
89
    }
90
91
    // now create smallworld_settings table if it doesn't exist
92
    $sTable = 'smallworld_settings';
93
    if (false === ($successC = $tables->useTable($sTable))) { // if this returns true then the table exists already
94
        // Table does not exist -> create
95
        $successC = $tables->addTable($sTable);
96
        $successC = $successC && ($tables->setTableOptions($sTable, 'ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1'));
97
        $successC = $successC && ($tables->addColumn($sTable, 'id', 'INT(11) NOT NULL AUTO_INCREMENT'));
98
        $successC = $successC && ($tables->addColumn($sTable, 'userid', 'INT(11) NOT NULL'));
99
        $successC = $successC && ($tables->addColumn($sTable, 'value', 'TEXT'));
100
        $successC = $successC && ($tables->addPrimaryKey($sTable, 'id'));
101
    }
102
103
    $successD = $tables->executeQueue();  // change the tables
104
105
    // setup the criteria to update the avatar fields
106
    $criteria = new \CriteriaCompo();
107
    $criteria->add(new \Criteria('userimage', 'blank.gif', '='));
108
    $criteria->add(new \Criteria('userimage', '\.(jpe?g|gif|png|bmp)', 'NOT REGEXP'), 'OR');
109
110
    // update the admin avatar
111
    $aTable = 'smallworld_admin';
112
    if ($successE = $tables->useTable($aTable)) {
113
        $successE = $tables->update($aTable, ['userimage' => ''], $criteria);
114
    }
115
    // update user table avatar
116
    $uTable = 'smallworld_user';
117
    if ($successF = $tables->useTable($uTable)) {
118
        $successF = $tables->update($uTable, ['userimage' => ''], $criteria);
119
    }
120
121
    return $success && $successA && $successB && $successC && $successD && $successE && $successF;
122
}
123