Completed
Push — master ( 8786b6...8e35b4 )
by Michael
8s
created

onupdate.php ➔ xoops_module_update_randomquote()   F

Complexity

Conditions 12
Paths 450

Size

Total Lines 84
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 12
eloc 55
c 3
b 1
f 1
nc 450
nop 2
dl 0
loc 84
rs 3.517

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 39 and the first side effect is on line 31.

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 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
 * Module: RandomQuote
14
 *
15
 * @category        Module
16
 * @package         randomquote
17
 * @author          XOOPS Module Development Team
18
 * @author          Mamba
19
 * @author          Herve Thouzard
20
 * @copyright       {@link http://xoops.org 2001-2016 XOOPS Project}
21
 * @coypright       Herve Thouzard
22
 * @license         {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
23
 * @link            http://xoops.org XOOPS
24
 * @since           2.00
25
 */
26
27
if ((!defined('XOOPS_ROOT_PATH'))
28
   || !($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...
29
   || !($GLOBALS['xoopsUser']->IsAdmin()))
30
{
31
     exit("Restricted access" . PHP_EOL);
32
}
33
34
/**
35
 * @param string $tablename
36
 *
37
 * @return bool
38
 */
39
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...
40
{
41
    $result = $GLOBALS['xoopsDB']->queryF("SHOW TABLES LIKE '$tablename'");
42
    return ($GLOBALS['xoopsDB']->getRowsNum($result) > 0) ? true : false;
43
}
44
45
/**
46
 *
47
 * Prepares system prior to attempting to install module
48
 * @param obj $module {@link XoopsModule}
49
 *
50
 * @return bool true if ready to install, false if not
51
 */
52 View Code Duplication
function xoops_module_pre_update_randomquote(&$module)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
53
{
54
55
    if (!class_exists('RandomquoteUtilities')) {
56
        xoops_load('utilities', 'randomquote');
57
    }
58
    //check for minimum XOOPS version
59
    if (!RandomquoteUtilities::checkXoopsVer($module)) {
60
        return false;
61
    }
62
63
    // check for minimum PHP version
64
    if (!RandomquoteUtilities::checkPHPVer($module)) {
65
        return false;
66
    }
67
    return true;
68
}
69
70
/**
71
 * @return bool
72
 */
73
function xoops_module_update_randomquote(&$module, $installedVersion = null)
0 ignored issues
show
Coding Style introduced by
xoops_module_update_randomquote 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...
74
{
75
    xoops_loadLanguage('admin', $module->dirname());
76
    $errors = 0;
77
    if (tableExists($GLOBALS['xoopsDB']->prefix('citas'))) {
78
79
        $sql = sprintf('ALTER TABLE '
80
                     . $GLOBALS['xoopsDB']->prefix('citas')
81
                     . ' CHANGE `citas` `quote` TEXT'
82
        );
83
        $result = $GLOBALS['xoopsDB']->queryF($sql);
84
        if (!$result) {
85
            $module->setErrors(_AM_RANDOMQUOTE_UPGRADEFAILED0);
86
            ++$errors;
87
        }
88
89
        $sql = sprintf('ALTER TABLE '
90
                     . $GLOBALS['xoopsDB']->prefix('citas')
91
                     . " ADD COLUMN `quote_status` int (10) NOT NULL default '0',"
92
                     . " ADD COLUMN `quote_waiting` int (10) NOT NULL default '0',"
93
                     . " ADD COLUMN `quote_online` int (10) NOT NULL default '0';"
94
        );
95
        $result = $GLOBALS['xoopsDB']->queryF($sql);
96
        if (!$result) {
97
            $module->setErrors(_AM_RANDOMQUOTE_UPGRADEFAILED1);
98
            ++$errors;
99
        }
100
101
        $sql = sprintf('ALTER TABLE '
102
                     . $GLOBALS['xoopsDB']->prefix('citas')
103
                     . ' RENAME '
104
                     . $GLOBALS['xoopsDB']->prefix('randomquote_quotes')
105
        );
106
        $result = $GLOBALS['xoopsDB']->queryF($sql);
107
        if (!$result) {
108
            $module->setErrors(_AM_RANDOMQUOTE_UPGRADEFAILED2);
109
            ++$errors;
110
        }
111
    }
112
113
    if ($installedVersion < 211) {
114
        // add column for quotes table for date created
115
        $result = $GLOBALS['xoopsDB']->queryF("SHOW COLUMNS FROM " . $GLOBALS['xoopsDB']->prefix('randomquote_quotes') . " LIKE 'create_date'");
116
        $foundCreate = $GLOBALS['xoopsDB']->getRowsNum($result);
117
        if (empty($foundCreate)) {
118
            // column doesn't exist, so try and add it
119
            $success = $GLOBALS['xoopsDB']->queryF("ALTER TABLE " . $GLOBALS['xoopsDB']->prefix('reandomquote_quotes') . " ADD create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP AFTER quote_status");
120
            if (!$success) {
121
                $module->setErrors(sprintf(_AM_RANDOMQUOTE_ERROR_COLUMN, 'create_date'));
122
                ++$errors;
123
            }
124
        }
125
126
        // change status to indicate quote waiting approval
127
        $sql = "UPDATE " . $GLOBALS['xoopsDB']->prefix('randomquote_quotes') . " SET quote_status=2 WHERE `quote_waiting` > 0";
128
        $result = $GLOBALS['xoopsDB']->queryF($sql);
129
        if (!$result) {
130
            $module->setErrors(_AM_RANDOMQUOTE_UPGRADEFAILED1);
131
            ++$errors;
132
        }
133
134
        // change status to indicate quote online
135
        $sql = "UPDATE " . $GLOBALS['xoopsDB']->prefix('randomquote_quotes') . " SET quote_status=1 WHERE `quote_online` > 0";
136
        $result = $GLOBALS['xoopsDB']->queryF($sql);
137
        if (!$result) {
138
            $module->setErrors(_AM_RANDOMQUOTE_UPGRADEFAILED1);
139
            ++$errors;
140
        }
141
142
        // drop the waiting and online columns
143
        $sql = sprintf('ALTER TABLE '
144
            . $GLOBALS['xoopsDB']->prefix('randomquote_quotes')
145
            . " DROP COLUMN `quote_waiting`,"
146
            . " DROP COLUMN `quote_online`;"
147
        );
148
        $result = $GLOBALS['xoopsDB']->queryF($sql);
149
        if (!$result) {
150
            $module->setErrors(_AM_RANDOMQUOTE_UPGRADEFAILED1);
151
            ++$errors;
152
        }
153
    }
154
155
    return ($errors) ? false : true;
156
}
157