Completed
Push — master ( 5eefd1...62af87 )
by Michael
14s
created

Utility::prepareFolder()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php namespace  Xoopsmodules\newbb;
2
3
4
use Xoopsmodules\newbb;
5
/**
6
 * Class Utility
7
 */
8
class Utility
9
{
10
    /**
11
     * Verify that a mysql table exists
12
     *
13
     * @package       News
14
     * @author        Hervé Thouzard (http://www.herve-thouzard.com)
15
     * @copyright (c) Hervé Thouzard
16
     * @param $tablename
17
     * @return bool
18
     */
19
    public function tableExists($tablename)
0 ignored issues
show
Coding Style introduced by
function tableExists() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
20
    {
21
        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...
22
        /** @var \XoopsMySQLDatabase $xoopsDB */
23
        $result = $xoopsDB->queryF("SHOW TABLES LIKE '$tablename'");
24
25
        return ($xoopsDB->getRowsNum($result) > 0);
26
    }
27
28
    /**
29
     * Verify that a field exists inside a mysql table
30
     *
31
     * @package       News
32
     * @author        Hervé Thouzard (http://www.herve-thouzard.com)
33
     * @copyright (c) Hervé Thouzard
34
     * @param $fieldname
35
     * @param $table
36
     * @return bool
37
     */
38
    public function fieldExists($fieldname, $table)
0 ignored issues
show
Coding Style introduced by
function fieldExists() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
39
    {
40
        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...
41
        $result = $xoopsDB->queryF("SHOW COLUMNS FROM   $table LIKE '$fieldname'");
42
43
        return ($xoopsDB->getRowsNum($result) > 0);
44
    }
45
46
    /**
47
     * Add a field to a mysql table
48
     *
49
     * @package       News
50
     * @author        Hervé Thouzard (http://www.herve-thouzard.com)
51
     * @copyright (c) Hervé Thouzard
52
     * @param $field
53
     * @param $table
54
     * @return bool|\mysqli_result
55
     */
56
    public function addField($field, $table)
57
    {
58
        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...
59
        $result = $xoopsDB->queryF('ALTER TABLE ' . $table . " ADD $field;");
60
61
        return $result;
62
    }
63
64
    /**
65
     * Function responsible for checking if a directory exists, we can also write in and create an index.html file
66
     *
67
     * @param string $folder Le chemin complet du répertoire à vérifier
68
     *
69
     * @return void
70
     */
71
    public static function prepareFolder($folder)
72
    {
73
        try {
74
            if (!@mkdir($folder) && !is_dir($folder)) {
75
                throw new \RuntimeException(sprintf('Unable to create the %s directory', $folder));
76
            } else {
77
                file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>');
78
            }
79
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class Xoopsmodules\newbb\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
80
            echo 'Caught exception: ', $e->getMessage(), "\n", '<br>';
81
        }
82
    }
83
        
84
    public static function cleanCache()
85
    {
86
        $cacheHelper = new \Xmf\Module\Helper\Cache('newbb');
87
        if (method_exists($cacheHelper, 'clear')) {
88
            $cacheHelper->clear();
89
            return;
90
        }
91
        // for 2.5 systems, clear everything
92
        require_once XOOPS_ROOT_PATH . '/modules/system/class/maintenance.php';
93
        $maintenance = new \SystemMaintenance();
94
        $cacheList = [
95
            3, // xoops_cache
96
            ];
97
        $maintenance->CleanCache($cacheList);
98
        xoops_setActiveModules();
99
    }
100
101
    /**
102
     * Checks if a user is admin of NewBB
103
     *
104
     * @return boolean
105
     */
106
    public static function userIsAdmin()
0 ignored issues
show
Coding Style introduced by
function userIsAdmin() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
userIsAdmin 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...
107
    {
108
        $helper = newbb\Helper::getInstance();
109
110
        static $newbbIsAdmin;
111
112
        if (isset($newbbIsAdmin)) {
113
            return $newbbIsAdmin;
114
        }
115
116
        if (!$GLOBALS['xoopsUser']) {
117
            $newbbIsAdmin = false;
118
        } else {
119
            $newbbIsAdmin = $GLOBALS['xoopsUser']->isAdmin($helper->getModule()->getVar('mid'));
120
        }
121
122
        return $newbbIsAdmin;
123
    }
124
}
125