Passed
Branch timgno (61fada)
by Gino
05:14
created

UserPrint::getUserPrint()   C

Complexity

Conditions 11
Paths 160

Size

Total Lines 73
Code Lines 61

Duplication

Lines 26
Ratio 35.62 %
Metric Value
dl 26
loc 73
rs 5.2343
cc 11
eloc 61
nc 160
nop 2

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
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: UserPrint.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
26
/**
27
 * Class UserPrint.
28
 */
29
class UserPrint extends TDMCreateFile
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 $usercode = null;
35
36
    /*
37
    * @var mixed
38
    */
39
    private $phpcode = null;
40
41
    /*
42
    * @var string
43
    */
44
    private $xoopscode;
45
46
    /*
47
    *  @public function constructor
48
    *  @param null
49
    */
50
    /**
51
     *
52
     */
53
    public function __construct()
54
    {
55
        parent::__construct();
56
        $this->xoopscode = TDMCreateXoopsCode::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreateXoopsCode::getInstance() of type object<TDMCreateXoopsCode> is incompatible with the declared type string of property $xoopscode.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        $this->phpcode = TDMCreatePhpCode::getInstance();
58
        $this->usercode = UserXoopsCode::getInstance();
59
    }
60
61
    /*
62
    *  @static function &getInstance
63
    *  @param null
64
    */
65
    /**
66
     * @return UserPrint
67
     */
68
    public static function &getInstance()
69
    {
70
        static $instance = false;
71
        if (!$instance) {
72
            $instance = new self();
73
        }
74
75
        return $instance;
76
    }
77
78
    /*
79
    *  @public function write
80
    *  @param string $module
81
    *  @param mixed $table
82
    *  @param string $filename
83
    */
84
    /**
85
     * @param $module
86
     * @param $table
87
     * @param $filename
88
     */
89
    public function write($module, $table, $filename)
90
    {
91
        $this->setModule($module);
92
        $this->setTable($table);
93
        $this->setFileName($filename);
94
    }
95
96
    /*
97
    *  @public function getUserPrint
98
    *  @param string $moduleDirname
99
    *  @param string $language
100
    */
101
    /**
102
     * @param $moduleDirname
103
     * @param $language
104
     *
105
     * @return string
106
     */
107
    public function getUserPrint($moduleDirname, $language)
108
    {
109
        $stuModuleDirname = strtoupper($moduleDirname);
110
        $table = $this->getTable();
111
        $tableName = $table->getVar('table_name');
112
        $tableSoleName = $table->getVar('table_solename');
113
        $ucfModuleDirname = ucfirst($moduleDirname);
0 ignored issues
show
Unused Code introduced by
$ucfModuleDirname 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...
114
        $ucfTableName = ucfirst($tableName);
0 ignored issues
show
Unused Code introduced by
$ucfTableName 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...
115
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
116
        foreach (array_keys($fields) as $f) {
117
            $fieldName = $fields[$f]->getVar('field_name');
118
            $rpFieldName = $fieldName;
0 ignored issues
show
Unused Code introduced by
$rpFieldName 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 View Code Duplication
            if (strpos($fieldName, '_')) {
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...
120
                $str = strpos($fieldName, '_');
121
                if ($str !== false) {
122
                    $rpFieldName = substr($fieldName, $str + 1, strlen($fieldName));
0 ignored issues
show
Unused Code introduced by
$rpFieldName 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...
123
                }
124
            }
125
            if ((0 == $f) && (1 == $this->table->getVar('table_autoincrement'))) {
126
                $fieldId = $fieldName;
127
            } else {
128
                if (1 == $fields[$f]->getVar('field_main')) {
129
                    $fieldMain = $fieldName; // fieldMain = fields parameters main field
130
                }
131
            }
132
            $ucfFieldName = ucfirst($fieldName);
133
        }
134
        $ccFieldId = $this->getCamelCase($fieldId, false, true);
0 ignored issues
show
Bug introduced by
The variable $fieldId 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...
135
        $stuLpFieldName = strtoupper($ccFieldId);
136
        $ret = $this->getInclude();
137
        $ret .= $this->xoopscode->getXoopsCodeXoopsRequest("{$ccFieldId}", "{$fieldId}", '', 'Int');
0 ignored issues
show
Bug introduced by
The method getXoopsCodeXoopsRequest cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
138
        $ret .= $this->getCommentLine('Define Stylesheet');
139
        $ret .= $this->xoopscode->getXoopsCodeAddStylesheet();
0 ignored issues
show
Bug introduced by
The method getXoopsCodeAddStylesheet cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
140
        $redirectHeader = $this->xoopscode->getXoopsCodeRedirectHeader("{$stuModuleDirname}_URL . '/index.php'", '', '2', "{$language}NO{$stuLpFieldName}", false);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeRedirectHeader cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
141
        $ret .= $this->phpcode->getPhpCodeConditions("empty(\${$ccFieldId})", '', '', $redirectHeader);
142
        $ret .= $this->getCommentLine('Verify that the article is published');
143 View Code Duplication
        if (strstr($fieldName, 'published')) {
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...
144
            $ret .= $this->getCommentLine('Not yet', $fieldName);
0 ignored issues
show
Bug introduced by
The variable $fieldName 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...
145
            $redirectHeader .= $this->getSimpleString('exit();');
146
            $ret .= $this->phpcode->getPhpCodeConditions("\${$ccFieldId}->getVar('{$fieldName}') == 0 || \${$ccFieldId}->getVar('{$fieldName}') > time()", '', '', $redirectHeader);
147
        }
148 View Code Duplication
        if (strstr($fieldName, 'expired')) {
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...
149
            $ret .= $this->getCommentLine('Expired', $ucfFieldName);
0 ignored issues
show
Bug introduced by
The variable $ucfFieldName 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...
150
            $redirectHeader .= $this->getSimpleString('exit();');
151
            $ret .= $this->phpcode->getPhpCodeConditions("\${$ccFieldId}->getVar('{$fieldName}') != 0 && \${$ccFieldId}->getVar('{$fieldName}') < time()", '', '', $redirectHeader);
152
        }
153 View Code Duplication
        if (strstr($fieldName, 'date')) {
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...
154
            $ret .= $this->getCommentLine('Date', $ucfFieldName);
155
            $redirectHeader .= $this->getSimpleString('exit();');
156
            $ret .= $this->phpcode->getPhpCodeConditions("\${$ccFieldId}->getVar('{$fieldName}') != 0 && \${$ccFieldId}->getVar('{$fieldName}') < time()", '', '', $redirectHeader);
157
        }
158 View Code Duplication
        if (strstr($fieldName, 'time')) {
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...
159
            $ret .= $this->getCommentLine('Time', $ucfFieldName);
160
            $redirectHeader .= $this->getSimpleString('exit();');
161
            $ret .= $this->phpcode->getPhpCodeConditions("\${$ccFieldId}->getVar('{$fieldName}') != 0 && \${$ccFieldId}->getVar('{$fieldName}') < time()", '', '', $redirectHeader);
162
        }
163
        $ret .= $this->xoopscode->getXoopsCodeGet($tableName, "{$ccFieldId}", '', true);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeGet cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
164
        $gperm = $this->xoopscode->getXoopsCodeCheckRight('!$gpermHandler', "{$moduleDirname}_view", "\${$ccFieldId}->getVat('{$fieldId}')", '$groups', "\$GLOBALS['xoopsModule']->getVar('mid')", true);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeCheckRight cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
165
        $ret .= $this->getCommentLine('Verify permissions');
166
        $noPerm = $this->xoopscode->getXoopsCodeRedirectHeader("{$stuModuleDirname}_URL . '/index.php'", '', '3', '_NOPERM');
0 ignored issues
show
Bug introduced by
The method getXoopsCodeRedirectHeader cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
167
        $noPerm .= $this->getSimpleString('exit();');
168
        $ret .= $this->phpcode->getPhpCodeConditions($gperm, '', '', $noPerm);
169
        $ret .= $this->xoopscode->getXoopsCodeGetValues($tableName, $tableSoleName, '', true);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeGetValues cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
170
        $contentForeach = $this->xoopscode->getXoopsCodeTplAssign('"{$k}"', '$v', false);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeTplAssign cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
171
        $ret .= $this->phpcode->getPhpCodeForeach($tableSoleName, false, 'k', 'v', $contentForeach);
172
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('xoops_sitename', "\$GLOBALS['xoopsConfig']['sitename']");
0 ignored issues
show
Bug introduced by
The method getXoopsCodeTplAssign cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
173
        $getVar = $this->xoopscode->getXoopsCodeGetVar('', $tableSoleName, $fieldMain, true);
0 ignored issues
show
Bug introduced by
The variable $fieldMain 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...
Bug introduced by
The method getXoopsCodeGetVar cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
174
        $stripTags = $this->phpcode->getPhpCodeStripTags('', $getVar.' - '."{$language}PRINT".' - '."\$GLOBALS['xoopsModule']->name()", true);
175
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('xoops_pagetitle', $stripTags);
0 ignored issues
show
Bug introduced by
The method getXoopsCodeTplAssign cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
176
        $ret .= $this->xoopscode->getXoopsCodeTplDisplay($tableName.'_print.tpl');
0 ignored issues
show
Bug introduced by
The method getXoopsCodeTplDisplay cannot be called on $this->xoopscode (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
177
178
        return $ret;
179
    }
180
181
    /*
182
    *  @public function render
183
    *  @param null
184
    */
185
    /**
186
     * @return bool|string
187
     */
188 View Code Duplication
    public function render()
0 ignored issues
show
Duplication introduced by
This method 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...
189
    {
190
        $module = $this->getModule();
191
        $filename = $this->getFileName();
192
        $moduleDirname = $module->getVar('mod_dirname');
193
        $language = $this->getLanguage($moduleDirname, 'MA');
194
        $content = $this->getHeaderFilesComments($module, $filename);
195
        $content .= $this->getUserPrint($moduleDirname, $language);
196
197
        $this->create($moduleDirname, '/', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
198
199
        return $this->renderFile();
200
    }
201
}
202