Completed
Push — master ( 53ea58...16270d )
by Michael
04:51
created

onupdate.php ➔ xoops_module_update_publisher()   D

Complexity

Conditions 13
Paths 2

Size

Total Lines 84
Code Lines 49

Duplication

Lines 20
Ratio 23.81 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 49
c 2
b 0
f 0
nc 2
nop 2
dl 20
loc 84
rs 4.9922

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
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         http://www.fsf.org/copyleft/gpl.html GNU public license
15
 * @author          trabis <[email protected]>
16
 *
17
 * @param      $module
18
 * @param null $oldversion
19
 *
20
 * @return
21
 */
22
23
function xoops_module_update_publisher(XoopsModule $module, $oldversion = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style introduced by
xoops_module_update_publisher 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...
24
{
25
    global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
26
    if ($oldversion < 102) {
27
        // delete old html template files
28
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/templates/');
29
        $templateList      = array_diff(scandir($templateDirectory), array('..', '.'));
30 View Code Duplication
        foreach ($templateList as $k => $v) {
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...
31
            $fileInfo = new SplFileInfo($templateDirectory . $v);
32
            if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
33
                if (file_exists($templateDirectory . $v)) {
34
                    unlink($templateDirectory . $v);
35
                }
36
            }
37
        }
38
        // delete old block html template files
39
        $templateDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/templates/blocks/');
40
        $templateList      = array_diff(scandir($templateDirectory), array('..', '.'));
41 View Code Duplication
        foreach ($templateList as $k => $v) {
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...
42
            $fileInfo = new SplFileInfo($templateDirectory . $v);
43
            if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') {
44
                if (file_exists($templateDirectory . $v)) {
45
                    unlink($templateDirectory . $v);
46
                }
47
            }
48
        }
49
50
        //delete old files:
51
        $oldFiles = array(
52
            '/class/request.php',
53
            '/class/registry.php',
54
            '/include/constants.php',
55
            '/ajaxrating.txt');
56
57
        foreach (array_keys($oldFiles) as $i) {
58
            unlink($GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . $oldFiles[$i]));
59
        }
60
61
        //delete .html entries from the tpl table
62
        $sql = 'DELETE FROM ' . $xoopsDB->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . "' AND `tpl_file` LIKE '%.html%'";
63
        $xoopsDB->queryF($sql);
64
65
        // Load class XoopsFile
66
        xoops_load('XoopsFile');
67
        //delete /images directory
68
        $imagesDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/images/');
69
        $folderHandler   = XoopsFile::getHandler('folder', $imagesDirectory);
70
        $folderHandler->delete($imagesDirectory);
71
        //delete /css directory
72
        $cssDirectory  = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/css/');
73
        $folderHandler = XoopsFile::getHandler('folder', $cssDirectory);
74
        $folderHandler->delete($cssDirectory);
75
        //delete /js directory
76
        $jsDirectory   = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/js/');
77
        $folderHandler = XoopsFile::getHandler('folder', $jsDirectory);
78
        $folderHandler->delete($jsDirectory);
79
        //delete /tcpdf directory
80
        $tcpdfDirectory = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/tcpdf/');
81
        $folderHandler  = XoopsFile::getHandler('folder', $tcpdfDirectory);
82
        $folderHandler->delete($tcpdfDirectory);
83
        //delete /templates/style.css file
84
        //       $cssFile = $GLOBALS['xoops']->path('modules/' . $module->getVar('dirname', 'n') . '/templates/style.css');
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
85
        //       $folderHandler   = XoopsFile::getHandler('file', $cssFile);
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
86
        //       $folderHandler->delete($cssFile);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
87
88
        //create upload directories, if needed
89
        $moduleDirName = $module->getVar('dirname');
90
        include $GLOBALS['xoops']->path('modules/' . $moduleDirName . '/include/config.php');
91
92
        foreach (array_keys($uploadFolders) as $i) {
93
            PublisherUtilities::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...
94
        }
95
        //copy blank.png files, if needed
96
        $file = PUBLISHER_ROOT_PATH . '/assets/images/blank.png';
97 View Code Duplication
        foreach (array_keys($copyFiles) as $i) {
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...
98
            $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...
99
            PublisherUtilities::copyFile($file, $dest);
100
        }
101
    }
102
103
    $gpermHandler = xoops_getHandler('groupperm');
104
105
    return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read');
106
}
107