Completed
Branch master (48ec27)
by Michael
03:44
created

oninstall.php ➔ xoops_module_install_extgallery()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 133
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 37
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 133
rs 8.1935

How to fix   Long Method   

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
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
 * @copyright    XOOPS Project http://xoops.org/
14
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @author     XOOPS Development Team
16
 */
17
18
19
/**
20
 *
21
 * Prepares system prior to attempting to install module
22
 * @param XoopsModule $module {@link XoopsModule}
23
 *
24
 * @return bool true if ready to install, false if not
25
 */
26
function xoops_module_pre_install_extgallery(XoopsModule $module)
0 ignored issues
show
Coding Style introduced by
xoops_module_pre_install_extgallery 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...
27
{
28
    $moduleDirName = basename(dirname(__DIR__));
29
    $className = ucfirst($moduleDirName) . 'Utilities';
30
    if (!class_exists($className)) {
31
        xoops_load('utilities', $moduleDirName);
32
    }
33
    //check for minimum XOOPS version
34
    if (!$className::checkXoopsVer($module)) {
35
        return false;
36
    }
37
38
    // check for minimum PHP version
39
    if (!$className::checkPhpVer($module)) {
40
        return false;
41
    }
42
43
    $mod_tables =& $module->getInfo('tables');
44
    foreach ($mod_tables as $table) {
45
        $GLOBALS['xoopsDB']->queryF('DROP TABLE IF EXISTS ' . $GLOBALS['xoopsDB']->prefix($table) . ';');
46
    }
47
48
    return true;
49
}
50
51
/**
52
 *
53
 * Performs tasks required during installation of the module
54
 * @param XoopsModule $module {@link XoopsModule}
0 ignored issues
show
Documentation introduced by
There is no parameter named $module. Did you maybe mean $xoopsModule?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
55
 *
56
 * @return bool true if installation successful, false if not
57
 */
58
function xoops_module_install_extgallery(XoopsModule $xoopsModule)
0 ignored issues
show
Coding Style introduced by
xoops_module_install_extgallery 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...
59
{
60
    $module_id = $xoopsModule->getVar('mid');
61
    /** @var XoopsGroupPermHandler $gpermHandler */
62
    $gpermHandler = xoops_getHandler('groupperm');
63
    /** @var XoopsConfigHandler $configHandler */
64
    $configHandler = xoops_getHandler('config');
0 ignored issues
show
Unused Code introduced by
$configHandler 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...
65
66
    /**
67
     * Default public category permission mask
68
     */
69
70
    // Access right
71
    $gpermHandler->addRight('extgallery_public_mask', 1, XOOPS_GROUP_ADMIN, $module_id);
72
    $gpermHandler->addRight('extgallery_public_mask', 1, XOOPS_GROUP_USERS, $module_id);
73
    $gpermHandler->addRight('extgallery_public_mask', 1, XOOPS_GROUP_ANONYMOUS, $module_id);
74
75
    // Public rate
76
    $gpermHandler->addRight('extgallery_public_mask', 2, XOOPS_GROUP_ADMIN, $module_id);
77
    $gpermHandler->addRight('extgallery_public_mask', 2, XOOPS_GROUP_USERS, $module_id);
78
79
    // Public eCard
80
    $gpermHandler->addRight('extgallery_public_mask', 4, XOOPS_GROUP_ADMIN, $module_id);
81
    $gpermHandler->addRight('extgallery_public_mask', 4, XOOPS_GROUP_USERS, $module_id);
82
83
    // Public download
84
    $gpermHandler->addRight('extgallery_public_mask', 8, XOOPS_GROUP_ADMIN, $module_id);
85
    $gpermHandler->addRight('extgallery_public_mask', 8, XOOPS_GROUP_USERS, $module_id);
86
87
    // Public upload
88
    $gpermHandler->addRight('extgallery_public_mask', 16, XOOPS_GROUP_ADMIN, $module_id);
89
90
    // Public autoapprove
91
    $gpermHandler->addRight('extgallery_public_mask', 32, XOOPS_GROUP_ADMIN, $module_id);
92
93
    // Public display
94
    $gpermHandler->addRight('extgallery_public_mask', 128, XOOPS_GROUP_ADMIN, $module_id);
95
    $gpermHandler->addRight('extgallery_public_mask', 128, XOOPS_GROUP_USERS, $module_id);
96
    $gpermHandler->addRight('extgallery_public_mask', 128, XOOPS_GROUP_ANONYMOUS, $module_id);
97
98
    /**
99
     * Default User's category permission
100
     */
101
102
    // Private gallery
103
104
    // Private rate
105
    $gpermHandler->addRight('extgallery_private', 2, XOOPS_GROUP_ADMIN, $module_id);
106
    $gpermHandler->addRight('extgallery_private', 2, XOOPS_GROUP_USERS, $module_id);
107
108
    // Private eCard
109
    $gpermHandler->addRight('extgallery_private', 4, XOOPS_GROUP_ADMIN, $module_id);
110
    $gpermHandler->addRight('extgallery_private', 4, XOOPS_GROUP_USERS, $module_id);
111
112
    // Private download
113
    $gpermHandler->addRight('extgallery_private', 8, XOOPS_GROUP_ADMIN, $module_id);
114
    $gpermHandler->addRight('extgallery_private', 8, XOOPS_GROUP_USERS, $module_id);
115
116
    // Private autoapprove
117
    $gpermHandler->addRight('extgallery_private', 16, XOOPS_GROUP_ADMIN, $module_id);
118
119
  /*
120
121
    // Create eXtGallery main upload directory
122
    $dir = XOOPS_ROOT_PATH . '/uploads/extgallery';
123
    if (!is_dir($dir)) {
124
        mkdir($dir, 0777);
125
    }
126
    chmod($dir, 0777);
127
    // Create directory for photo in public album
128
    $dir = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo';
129
    if (!is_dir($dir)) {
130
        mkdir($dir, 0777);
131
    }
132
    chmod($dir, 0777);
133
    $dir = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/original';
134
    if (!is_dir($dir)) {
135
        mkdir($dir, 0777);
136
    }
137
    chmod($dir, 0777);
138
    $dir = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/large';
139
    if (!is_dir($dir)) {
140
        mkdir($dir, 0777);
141
    }
142
    chmod($dir, 0777);
143
    $dir = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/medium';
144
    if (!is_dir($dir)) {
145
        mkdir($dir, 0777);
146
    }
147
    chmod($dir, 0777);
148
    $dir = XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/thumb';
149
    if (!is_dir($dir)) {
150
        mkdir($dir, 0777);
151
    }
152
    chmod($dir, 0777);
153
154
155
156
    // Create directory for photo in user's album
157
    //mkdir(XOOPS_ROOT_PATH."/uploads/extgallery/user-photo");
158
159
    // Copy index.html files on uploads folders
160
    $indexFile = XOOPS_ROOT_PATH . '/modules/extgallery/include/index.html';
161
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/index.html');
162
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/index.html');
163
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/original/index.html');
164
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/large/index.html');
165
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/medium/index.html');
166
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/extgallery/public-photo/thumb/index.html');
167
168
*/
169
170
    $moduleDirName = $xoopsModule->getVar('dirname');
171
    include_once $GLOBALS['xoops']->path('modules/' . $moduleDirName . '/include/config.php');
172
173
    $classUtilities = ucfirst($moduleDirName) . 'Utilities';
174
    if (!class_exists($classUtilities)) {
175
        xoops_load('utilities', $moduleDirName);
176
    }
177
178
179
    foreach (array_keys($uploadFolders) as $i) {
180
        $classUtilities::createFolder($uploadFolders[$i]);
0 ignored issues
show
Bug introduced by
The variable $uploadFolders does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
181
    }
182
183
    $file = PUBLISHER_ROOT_PATH . '/assets/images/blank.png';
184
    foreach (array_keys($copyFiles) as $i) {
185
        $dest = $copyFiles[$i] . '/blank.png';
0 ignored issues
show
Bug introduced by
The variable $copyFiles does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
186
        $classUtilities::copyFile($file, $dest);
187
    }
188
189
    return true;
190
}
191