Completed
Pull Request — master (#11)
by
unknown
01:52
created

action.module.php ➔ xoops_module_pre_install_about()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
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 12 and the first side effect is on line 2.

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
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
3
4
/**
5
 *
6
 * Prepares system prior to attempting to install module
7
 *
8
 * @param XoopsModule $module
9
 *
10
 * @return bool       true if ready to install, false if not
11
 */
12 View Code Duplication
function xoops_module_pre_install_about(XoopsModule $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...
13
{
14
    $utilsClass = ucfirst($module->dirname()) . "Utility";
15
    if (!class_exists($utilsClass)) {
16
        xoops_load('utility', $module->dirname());
17
    }
18
19
    $xoopsSuccess = $utilsClass::checkVerXoops($module);
20
    $phpSuccess   = $utilsClass::checkVerPHPr($module);
21
22
    return $xoopsSuccess && $phpSuccess;
23
}
24
25
/**
26
 * @param  XoopsObject $module
0 ignored issues
show
Documentation introduced by
Should the type for parameter $module not be XoopsModule?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
27
 * @return bool        true if install successful, false if not
28
 */
29
function xoops_module_install_about(XoopsModule $module)
0 ignored issues
show
Coding Style introduced by
xoops_module_install_about 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
    $success = true;
32
    $data_file = XOOPS_ROOT_PATH . '/modules/about/sql/mysql.about.sql';
33
    $GLOBALS['xoopsDB']->queryF('SET NAMES utf8');
34
    if (!$GLOBALS['xoopsDB']->queryFromFile($data_file)) {
35
        $module->setErrors('Pre-set data was not installed');
36
        // preset info not set, but don't 'fail' install because of this
37
        //$success = false;
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...
38
    }
39
40
    // Delete files from previous version (if they exist)
41
    // this is only executed if this version copied over old version without running module update
42
    $oldFiles = array(XOOPS_ROOT_PATH . "/modules/" . $module->dirname() . "/include/xoopsformloader.php",
43
                      XOOPS_ROOT_PATH . "/modules/" . $module->dirname() . "/include/blockform.php",
44
                      XOOPS_ROOT_PATH . "/modules/" . $module->dirname() . "/class/utilities.php",
45
    );
46
    foreach($oldFiles as $file) {
47
        if (is_file($file)) {
48
            $delOk = unlink($file);
49
            if (false === $delOk) {
50
                $module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_REMOVE, $file));
51
            } else {
52
                $module->setErrors(sprintf(_AM_ABOUT_DELETED, $file));
53
            }
54
            $success = $success && $delOk;
55
        }
56
    }
57
    // Create uploads folder
58
    $dirOk = mkdir(XOOPS_UPLOAD_PATH . '/' . $module->dirname());
59
    if (false === $dirOk) {
60
        $module->setErrors(_AM_ABOUT_ERROR_BAD_UPLOAD_DIR);
61
    }
62
63
    return $dirOk && $success;
64
}
65
66
/**
67
 *
68
 * Prepares system prior to attempting to install module
69
 *
70
 * @param XoopsModule $module
71
 *
72
 * @return bool       true if ready to install, false if not
73
 */
74 View Code Duplication
function xoops_module_pre_update_about(XoopsModule $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...
75
{
76
    $utilsClass = ucfirst($module->dirname()) . "Utility";
77
    if (!class_exists($utilsClass)) {
78
        xoops_load('utility', $module->dirname());
79
    }
80
81
    $xoopsSuccess = $utilsClass::checkVerXoops($module);
82
    $phpSuccess   = $utilsClass::checkVerPHP($module);
83
84
    return $xoopsSuccess && $phpSuccess;
85
}
86
87
88
/**
89
 * @param  XoopsModule $module
90
 * @param  null        $prev_version
91
 * @return bool        true if update successful, false if not
92
 */
93
function xoops_module_update_about(XoopsModule $module, $prev_version = null)
0 ignored issues
show
Unused Code introduced by
The parameter $prev_version 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...
94
{
95
    $success = true;
96
    // Delete files from previous version (if they exist)
97
    $oldFiles = array(XOOPS_ROOT_PATH . "/modules/" . $module->dirname() . "/include/xoopsformloader.php",
98
                      XOOPS_ROOT_PATH . "/modules/" . $module->dirname() . "/include/blockform.php",
99
                      XOOPS_ROOT_PATH . "/modules/" . $module->dirname() . "/class/utilities.php"
100
    );
101 View Code Duplication
    foreach($oldFiles as $file) {
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...
102
        if (is_file($file)) {
103
            if (false === ($delOk = unlink($file))) {
104
                $module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_REMOVE, $file));
105
            }
106
            $success = $success && $delOk;
107
        }
108
    }
109
110
    // Delete files from previous version (if they exist)
111
    // this is only executed if this version copied over old version without running module update
112
    $oldFiles = array(XOOPS_PATH . "/modules/" . $module->dirname() . "/include/xoopsformloader.php",
113
                      XOOPS_PATH . "/modules/" . $module->dirname() . "/include/blockform.php"
114
    );
115 View Code Duplication
    foreach($oldFiles as $file) {
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...
116
        if (is_file($file)) {
117
            if (false === ($delOk = unlink($file))) {
118
                $module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_REMOVE, $file));
119
            }
120
            $success = $success && $delOk;
121
        }
122
    }
123
124
    // Create uploads folder if it doesn't exist
125
    $dirOk = true;
126
    if (false === file_exists(XOOPS_UPLOAD_PATH . '/' . $module->dirname())) {
127
        // File doesn't exist so try and create it
128
        $dirOk = mkdir(XOOPS_UPLOAD_PATH . '/' . $module->dirname());
129
        if (false === $dirOk) {
130
            $module->setErrors(_AM_ABOUT_ERROR_BAD_UPLOAD_DIR);
131
        }
132
    }
133
134
    return $dirOk && $success;
135
}
136
137
/**
138
 *
139
 * Function to complete upon module uninstall
140
 *
141
 * @param XoopsModule $module
142
 *
143
 * @return bool       true if successfully executed uninstall of module, false if not
144
 */
145
function xoops_module_uninstall_about(XoopsModule $module)
146
{
147
    $moduleDirName = $module->dirname();
148
    $aboutHelper = Xmf\Module\Helper::getHelper($moduleDirName);
149
    $utilsClass = ucfirst($moduleDirName) . 'Utility';
150
    if (!class_exists($utilsClass)) {
151
        xoops_load('utility', $moduleDirName);
152
    }
153
154
    $success = true;
155
    $aboutHelper->loadLanguage('admin');
156
157
    //------------------------------------------------------------------
158
    // Remove module uploads folder (and all subfolders) if they exist
159
    //------------------------------------------------------------------
160
161
    $old_directories = array(XOOPS_UPLOAD_PATH . "/{$moduleDirName}");
162
    foreach ($old_directories as $old_dir) {
163
        $dirInfo = new SplFileInfo($old_dir);
164
        if ($dirInfo->isDir()) {
165
            // The directory exists so delete it
166
            if (false === $utilsClass::rrmdir($old_dir)) {
167
                $module->setErrors(sprintf(_AM_ABOUT_ERROR_BAD_DEL_PATH, $old_dir));
168
                $success = false;
169
            }
170
        }
171
        unset($dirInfo);
172
    }
173
    return $success;
174
}
175