Completed
Pull Request — master (#51)
by Gino
06:03
created

UserPages::getUserPagesHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 1
eloc 10
nc 1
nop 2
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: pages.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
26
/**
27
 * Class UserPages.
28
 */
29
class UserPages 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
    *  @public function constructor
33
    *  @param null
34
    */
35
    /**
36
     *
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41
        $this->tdmcfile = TDMCreateFile::getInstance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TDMCreateFile::getInstance() of type object<TDMCreateFile> is incompatible with the declared type string of property $tdmcfile.

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...
42
        $this->phpcode = TDMCreatePhpCode::getInstance();
0 ignored issues
show
Bug introduced by
The property phpcode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
        $this->xoopscode = TDMCreateXoopsCode::getInstance();
0 ignored issues
show
Bug introduced by
The property xoopscode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
        $this->usercode = UserXoopsCode::getInstance();
0 ignored issues
show
Bug introduced by
The property usercode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
    }
46
47
    /*
48
    *  @static function &getInstance
49
    *  @param null
50
    */
51
    /**
52
     * @return UserPages
53
     */
54
    public static function &getInstance()
55
    {
56
        static $instance = false;
57
        if (!$instance) {
58
            $instance = new self();
59
        }
60
61
        return $instance;
62
    }
63
64
    /**
65
     * @public function write
66
     *
67
     * @param $module
68
     * @param $table
69
     * @param $filename
70
     */
71
    public function write($module, $table, $filename)
72
    {
73
        $this->setModule($module);
74
        $this->setTable($table);
75
        $this->setFileName($filename);
76
    }
77
78
    /**
79
     * @private function getUserPagesHeader
80
     *
81
     * @param $moduleDirname
82
     * @param $tableName
83
     *
84
     * @return string
85
     */
86
    private function getUserPagesHeader($moduleDirname, $tableName)
87
    {
88
        $ret = $this->getInclude();
89
        $ret .= $this->usercode->getUserTplMain($moduleDirname, $tableName);
90
        $ret .= $this->phpcode->getPhpCodeIncludeDir('XOOPS_ROOT_PATH', 'header', true);
91
        $ret .= $this->xoopscode->getXoopsCodeXoopsRequest('start', 'start', '0', 'Int');
92
        $userpager = $this->xoopscode->getXoopsCodeGetConfig($moduleDirname, 'userpager');
93
        $ret .= $this->xoopscode->getXoopsCodeXoopsRequest('limit', 'limit', $userpager, 'Int');
94
        $ret .= $this->getCommentLine('Define Stylesheet');
95
        $ret .= $this->xoopscode->getXoopsCodeAddStylesheet();
96
97
        return $ret;
98
    }
99
100
    /**
101
     * @private function getUserPages
102
     *
103
     * @param $moduleDirname
104
     * @param $tableName
105
     *
106
     * @return string
107
     */
108
    private function getUserPages($moduleDirname, $tableName, $tableSoleName)
109
    {
110
        $stuModuleDirname = strtoupper($moduleDirname);
111
        $ucfTableName = ucfirst($tableName);
112
        $ret = $this->getCommentLine();
113
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('xoops_icons32_url', 'XOOPS_ICONS32_URL');
114
        $ret .= $this->xoopscode->getXoopsCodeTplAssign("{$moduleDirname}_upload_url", "{$stuModuleDirname}_UPLOAD_URL");
115
        $ret .= $this->getCommentLine();
116
        $ret .= $this->xoopscode->getXoopsCodeObjHandlerCount($tableName);
117
        $ret .= $this->xoopscode->getXoopsCodeObjHandlerAll($tableName, '', '$start', '$limit');
118
        $ret .= $this->getSimpleString('$keywords = array();');
119
        $condIf = $this->getCommentLine('Get All', $ucfTableName);
120
        $foreach = $this->xoopscode->getXoopsCodeGetValues($tableName, $tableSoleName);
121
        $foreach .= $this->xoopscode->getXoopsCodeXoopsTplAppend($tableName, "\${$tableSoleName}");
122
        $table = $this->getTable();
123
        // Fields
124
        $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
125
        foreach (array_keys($fields) as $f) {
126
            $fieldName = $fields[$f]->getVar('field_name');
127
            if (1 == $fields[$f]->getVar('field_main')) {
128
                $fieldMain = $fieldName; // fieldMain = fields parameters main field
129
            }
130
        }
131
        $foreach .= $this->xoopscode->getXoopsCodeGetVar('keywords[]', "{$tableName}All[\$i]", $fieldMain);
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...
132
        $condIf .= $this->phpcode->getPhpCodeForeach("\${$tableName}All", true, false, 'i', $foreach, "\t");
133
        $condIf .= $this->xoopscode->getXoopsCodePageNav($tableName);
134
        $tableType = $this->xoopscode->getXoopsCodeGetConfig($moduleDirname, 'table_type');
135
        $condIf .= $this->xoopscode->getXoopsCodeTplAssign('type', $tableType);
136
        $divideby = $this->xoopscode->getXoopsCodeGetConfig($moduleDirname, 'divideby');
137
        $condIf .= $this->xoopscode->getXoopsCodeTplAssign('divideby', $divideby);
138
        $numbCol = $this->xoopscode->getXoopsCodeGetConfig($moduleDirname, 'numb_col');
139
        $condIf .= $this->xoopscode->getXoopsCodeTplAssign('numb_col', $numbCol);
140
141
        $ret .= $this->phpcode->getPhpCodeConditions("\${$tableName}Count", ' > ', '0', $condIf);
142
143
        return $ret;
144
    }
145
146
    /**
147
     * @private function getUserPagesFooter
148
     *
149
     * @param $moduleDirname
150
     * @param $tableName
151
     * @param $tableSoleName
152
     * @param $language
153
     *
154
     * @return string
155
     */
156
    private function getUserPagesFooter($moduleDirname, $tableName, $tableSoleName, $language)
157
    {
158
        $stuModuleDirname = strtoupper($moduleDirname);
159
        $stuTableName = strtoupper($tableName);
160
        $stuTableSoleName = strtoupper($tableSoleName);
161
        $ret = $this->getCommentLine('Breadcrumbs');
162
        $ret .= $this->usercode->getUserBreadcrumbs("{$stuTableName}", $language);
163
        $ret .= $this->getCommentLine('Keywords');
164
        $ret .= $this->usercode->getUserMetaKeywords($moduleDirname);
165
        $ret .= $this->phpcode->getPhpCodeUnset('keywords');
166
        $ret .= $this->getCommentLine('Description');
167
        $ret .= $this->usercode->getUserMetaDesc($moduleDirname, $stuTableSoleName, $language);
168
        $ret .= $this->xoopscode->getXoopsCodeTplAssign('xoops_mpageurl', "{$stuModuleDirname}_URL.'/{$tableName}.php'");
169
        $ret .= $this->getInclude('footer');
170
171
        return $ret;
172
    }
173
174
    /*
175
    *  @public function render
176
    *  @param null
177
    */
178
    /**
179
     * @param $filename
180
     *
181
     * @return bool|string
182
     */
183 View Code Duplication
    public function renderFile()
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...
184
    {
185
        $module = $this->getModule();
186
        $table = $this->getTable();
187
        $tableName = $table->getVar('table_name');
188
        $tableSoleName = $table->getVar('table_solename');
189
        $filename = $this->getFileName();
190
        $moduleDirname = $module->getVar('mod_dirname');
191
        $language = $this->getLanguage($moduleDirname, 'MA');
192
        $content = $this->getHeaderFilesComments($module, $filename);
193
        $content .= $this->getUserPagesHeader($moduleDirname, $tableName);
194
        $content .= $this->getUserPages($moduleDirname, $tableName, $tableSoleName);
195
        $content .= $this->getUserPagesFooter($moduleDirname, $tableName, $tableSoleName, $language);
196
        //
197
        $this->tdmcfile->create($moduleDirname, '/', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
0 ignored issues
show
Bug introduced by
The method create cannot be called on $this->tdmcfile (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...
198
199
        return $this->tdmcfile->renderFile();
0 ignored issues
show
Bug introduced by
The method renderFile cannot be called on $this->tdmcfile (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...
200
    }
201
}
202