Completed
Pull Request — master (#52)
by Gino
04:52
created

UserXoopsCode::getUserModVersion()   F

Complexity

Conditions 12
Paths 544

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 23
rs 3.2093
cc 12
eloc 15
nc 544
nop 7

How to fix   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 29 and the first side effect is on line 24.

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
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * tdmcreate module.
14
 *
15
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
16
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 *
18
 * @since           2.5.0
19
 *
20
 * @author          Txmod Xoops http://www.txmodxoops.org
21
 *
22
 * @version         $Id: UserXoopsCode.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
26
/**
27
 * Class UserXoopsCode.
28
 */
29
class UserXoopsCode
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
30
{
31
    /*
32
    * @var mixed
33
    */
34
    private $phpcode = null;
35
36
    /*
37
    * @var mixed
38
    */
39
    private $xoopscode = null;
40
41
    /*
42
    *  @public function constructor
43
    *  @param null
44
    */
45
    /**
46
     *
47
     */
48
    public function __construct()
49
    {
50
        $this->phpcode = TDMCreatePhpCode::getInstance();
51
        $this->xoopscode = TDMCreateXoopsCode::getInstance();
52
    }
53
54
    /*
55
    *  @static function &getInstance
56
    *  @param null
57
    */
58
    /**
59
     * @return UserXoopsCode
60
     */
61
    public static function &getInstance()
62
    {
63
        static $instance = false;
64
        if (!$instance) {
65
            $instance = new self();
66
        }
67
68
        return $instance;
69
    }
70
71
    /*
72
    *  @public function getUserTplMain
73
    *  @param string $moduleDirname
74
    *  @param string $tableName
75
    *  @return string
76
    */
77
    public function getUserTplMain($moduleDirname, $tableName = 'index')
78
    {
79
        return "\$GLOBALS['xoopsOption']['template_main'] = '{$moduleDirname}_{$tableName}.tpl';\n";
80
    }
81
82
    /*
83
     * @public function getUserAddMeta
84
     * @param $type
85
     * @param $language
86
     * @param $tableName
87
     *  
88
     * @return string
89
     */
90
    public function getUserAddMeta($type = '', $language, $tableName = '')
91
    {
92
        $stuTableName = strtoupper($tableName);
93
94
        return "\$GLOBALS['xoTheme']->addMeta( 'meta', '{$type}', strip_tags({$language}{$stuTableName}));\n";
95
    }
96
97
    /*
98
    *  @public function getUserMetaKeywords
99
    *  @param string $moduleDirname
100
    *  
101
    *  @return string
102
    */
103
    public function getUserMetaKeywords($moduleDirname)
104
    {
105
        return "{$moduleDirname}MetaKeywords(\${$moduleDirname}->getConfig('keywords').', '. implode(', ', \$keywords));\n";
106
    }
107
108
    /*
109
    *  @public function getUserMetaDesc
110
    *  @param string $moduleDirname
111
    *  @param string $stuTableSoleName
112
    *  @param string $language
113
    *  
114
    *  @return string
115
    */
116
    public function getUserMetaDesc($moduleDirname, $stuTableSoleName, $language)
117
    {
118
        $stuModuleDirname = strtoupper($moduleDirname);
0 ignored issues
show
Unused Code introduced by
$stuModuleDirname 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...
119
120
        return "{$moduleDirname}MetaDescription({$language}{$stuTableSoleName}_DESC);\n";
121
    }
122
123
    /*
124
    *  @public function getUserBreadcrumbs
125
    *  @param string $moduleDirname
0 ignored issues
show
Bug introduced by
There is no parameter named $moduleDirname. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

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

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

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

Loading history...
126
    *  @param string $language
127
    *  
128
    *  @return string
129
    */
130
    public function getUserBreadcrumbs($tableName, $language)
131
    {
132
        $stuTableName = strtoupper($tableName);
133
134
        return "\$xoBreadcrumbs[] = array('title' => {$language}{$stuTableName});\n";
135
    }
136
137
    /*
138
    *  @public function getUserBreadcrumbs
139
    *  @param string $moduleDirname
140
    *  
141
    *  @return string
142
    */
143
    public function getUserBreadcrumbsHeaderFile($moduleDirname)
144
    {
145
        $stuModuleDirname = strtoupper($moduleDirname);
146
        $ret = $this->phpcode->getPhpCodeCommentLine('Breadcrumbs');
147
        $ret .= "\$xoBreadcrumbs = array();\n";
148
        $ret .= "\$xoBreadcrumbs[] = array('title' => \$GLOBALS['xoopsModule']->getVar('name'), 'link' => {$stuModuleDirname}_URL . '/');\n";
149
150
        return $ret;
151
    }
152
153
    /*
154
    *  @public function getUserBreadcrumbs
155
    *  @param null
156
    *  
157
    *  @return string
158
    */
159
    public function getUserBreadcrumbsFooterFile()
160
    {
161
        $cond = $this->xoopscode->getXoopsCodeTplAssign('xoBreadcrumbs', '$xoBreadcrumbs');
162
        $ret .= $this->phpcode->getPhpCodeConditions('count($xoBreadcrumbs)', ' > ', '1', $cond, false, "\t\t");
0 ignored issues
show
Bug introduced by
The variable $ret 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...
163
164
        return $ret;
165
    }
166
167
    /*
168
    *  @public function getUserModVersion
169
    *  @param $array
170
    *  
171
    *  @return string
172
    */
173
    public function getUserModVersion($array = array(), $element = 1, $left = '', $index = '', $right = '', $desc = '', $arrayOptions = '')
174
    {
175
        $index = (is_string($index) && !empty($index)) ? "'{$index}'" : $index;
176
        $right = (is_string($right) && !empty($right)) ? "'{$right}'" : $right;
177
        $desc = is_string($desc) ? "'{$desc}'" : $desc;
178
        foreach ($array as $key => $value) {
179
            if ($element == 1) {
180
                $ret = "\$modversion['{$left}'] = {$desc};\n";
181
            }
182
            if ($element == 2) {
183
                $ret = "\$modversion['{$left}'][{$index}] = {$desc};\n";
184
            }
185
            if ($element == 3) {
186
                $ret = "\$modversion['{$left}'][{$index}][{$right}] = {$desc};\n";
187
            }
188
            if (($element == 3) && !empty($arrayOptions)) {
189
                $ret = "\$modversion['{$left}'][{$index}][{$right}] = {$desc};";
190
                $ret .= "\$modversion['{$left}'][{$index}][{$right}] = {$arrayOptions};\n";
191
            }
192
        }
193
194
        return $ret;
0 ignored issues
show
Bug introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
195
    }
196
}
197