Completed
Push — master ( 2ee26c...778493 )
by Gino
06:21 queued 02:34
created

IncludeInstall   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 24
c 1
b 1
f 0
lcom 2
cbo 3
dl 0
loc 272
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getInstance() 0 9 2
A write() 0 7 1
A getInstallDirectory() 0 9 1
A getInstallModuleFolder() 0 11 1
A getInstallTableFolder() 0 8 1
A getInstallImagesFolder() 0 9 1
A getInstallImagesShotsFolder() 0 9 1
A getInstallTableImagesFolder() 0 9 1
A getInstallFilesFolder() 0 8 1
A getInstallTableFilesFolder() 0 8 1
A getInstallFooter() 0 4 1
C render() 0 49 11
1
<?php
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: IncludeInstall.php 12258 2014-01-02 09:33:29Z timgno $
23
 */
24
25
/**
26
 * Class IncludeInstall.
27
 */
28
class IncludeInstall 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...
29
{
30
    /*
31
    * @var mixed
32
    */
33
    private $pc = null;
34
35
    /*
36
    * @var mixed
37
    */
38
    private $xc = null;
39
40
    /*
41
    *  @public function constructor
42
    *  @param null
43
    */
44
    /**
45
     *
46
     */
47
    public function __construct()
48
    {
49
        parent::__construct();
50
        $this->pc = TDMCreatePhpCode::getInstance();
51
        $this->xc = TDMCreateXoopsCode::getInstance();
52
    }
53
54
    /*
55
    *  @static function &getInstance
56
    *  @param null
57
    */
58
    /**
59
     * @return IncludeInstall
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 write
73
    *  @param string $module
74
    *  @param mixed $tables
75
    *  @param string $filename
76
    */
77
    /**
78
     * @param $module
79
     * @param $table
80
     * @param $tables
81
     * @param $filename
82
     */
83
    public function write($module, $table, $tables, $filename)
84
    {
85
        $this->setModule($module);
86
        $this->setTable($table);
87
        $this->setTables($tables);
88
        $this->setFileName($filename);
89
    }
90
91
    /**
92
     * @private function getInstallDirectory    
93
     *
94
     * @param $dirname
95
     *
96
     * @return string
97
     */
98
    private function getInstallDirectory($dirname)
99
    {
100
        $contentIf = $this->pc->getPhpCodeMkdir("{$dirname}", '0777', "\t");
101
        $contentIf .= $this->pc->getPhpCodeChmod("{$dirname}", '0777', "\t");
102
        $ret = $this->pc->getPhpCodeConditions("!is_dir(\${$dirname})", '', '', $contentIf);
103
        $ret .= $this->pc->getPhpCodeCopy('$indexFile', "\${$dirname}.'/index.html'");
104
105
        return $ret;
106
    }
107
108
    /**
109
     *  @private function getInstallModuleFolder
110
     *
111
     *  @param $moduleDirname
112
     *
113
     * @return string
114
     */
115
    private function getInstallModuleFolder($moduleDirname)
116
    {
117
        $ret = $this->pc->getPhpCodeCommentLine('Copy base file');
118
        $ret .= $this->xc->getXoopsCodeEqualsOperator('$indexFile', "XOOPS_UPLOAD_PATH.'/index.html'");
119
        $ret .= $this->xc->getXoopsCodeEqualsOperator('$blankFile', "XOOPS_UPLOAD_PATH.'/blank.gif'");
120
        $ret .= $this->pc->getPhpCodeCommentLine("Making of uploads/{$moduleDirname} folder");
121
        $ret .= $this->xc->getXoopsCodeEqualsOperator("\${$moduleDirname}", "XOOPS_UPLOAD_PATH.'/{$moduleDirname}'");
122
        $ret .= $this->getInstallDirectory($moduleDirname);
123
124
        return $ret;
125
    }
126
127
    /**
128
     *  @private function getHeaderTableFolder
129
     *
130
     *  @param $moduleDirname
131
     *  @param $tableName    
132
     *
133
     * @return string
134
     */
135
    private function getInstallTableFolder($moduleDirname, $tableName)
136
    {
137
        $ret = $this->pc->getPhpCodeCommentLine("Making of {$tableName} uploads folder");
138
        $ret .= $this->xc->getXoopsCodeEqualsOperator("\${$tableName}", "\${$moduleDirname}.'/{$tableName}'");
139
        $ret .= $this->getInstallDirectory($tableName);
140
141
        return $ret;
142
    }
143
144
    /*
145
    *  @private function getInstallImagesFolder
146
    *  @param string $moduleDirname
147
    */
148
    /**
149
     * @param $moduleDirname
150
     *
151
     * @return string
152
     */
153
    private function getInstallImagesFolder($moduleDirname)
154
    {
155
        $ret = $this->pc->getPhpCodeCommentLine('Making of images folder');
156
        $ret .= $this->xc->getXoopsCodeEqualsOperator('$images', "\${$moduleDirname}.'/images'");
157
        $ret .= $this->getInstallDirectory('images');
158
        $ret .= $this->pc->getPhpCodeCopy('$blankFile', "\$images.'/blank.gif'");
159
160
        return $ret;
161
    }
162
163
    /**
164
     * @private function getInstallImagesShotsFolder
165
     *
166
     * @param $moduleDirname    
167
     *
168
     * @return string
169
     */
170
    private function getInstallImagesShotsFolder($moduleDirname)
171
    {
172
        $ret = $this->pc->getPhpCodeCommentLine('Making of shots folder');
173
        $ret .= $this->xc->getXoopsCodeEqualsOperator('$shots', "\${$moduleDirname}.'/shots'");
174
        $ret .= $this->getInstallDirectory('shots');
175
        $ret .= $this->pc->getPhpCodeCopy('$blankFile', "\$shots.'/blank.gif'");
176
177
        return $ret;
178
    }
179
180
    /**
181
     *  @private function getInstallTableImagesFolder
182
     *
183
     *  @param $tableName    
184
     *
185
     * @return string
186
     */
187
    private function getInstallTableImagesFolder($tableName)
188
    {
189
        $ret = $this->pc->getPhpCodeCommentLine("Making of images/{$tableName} folder");
190
        $ret .= $this->xc->getXoopsCodeEqualsOperator("\${$tableName}", "\$images.'/{$tableName}'");
191
        $ret .= $this->getInstallDirectory($tableName);
192
        $ret .= $this->pc->getPhpCodeCopy('$blankFile', "\${$tableName}.'/blank.gif'");
193
194
        return $ret;
195
    }
196
197
    /**
198
     *  @private function getInstallFilesFolder
199
     *
200
     *  @param $moduleDirname     
201
     *
202
     * @return string
203
     */
204
    private function getInstallFilesFolder($moduleDirname)
205
    {
206
        $ret = $this->pc->getPhpCodeCommentLine('Making of files folder');
207
        $ret .= $this->xc->getXoopsCodeEqualsOperator('$files', "\${$moduleDirname}.'/files'");
208
        $ret .= $this->getInstallDirectory('files');
209
210
        return $ret;
211
    }
212
213
    /*
214
    *  @private function getInstallTableFilesFolder
215
    *  @param string $tableName
216
    */
217
    /**
218
     * @param $tableName
219
     *
220
     * @return string
221
     */
222
    private function getInstallTableFilesFolder($tableName)
223
    {
224
        $ret = $this->pc->getPhpCodeCommentLine("Making of {$tableName} files folder");
225
        $ret .= $this->xc->getXoopsCodeEqualsOperator("\${$tableName}", "\$files.'/{$tableName}'");
226
        $ret .= $this->getInstallDirectory($tableName);
227
228
        return $ret;
229
    }
230
231
    /*
232
    *  @private function getInstallFooter
233
    *  @param null
234
    */
235
    /**
236
     * @return string
237
     */
238
    private function getInstallFooter()
239
    {
240
        return $this->getDashComment('Install Footer');
241
    }
242
243
    /*
244
    *  @public function render
245
    *  @param null
246
    */
247
    /**
248
     * @return bool|string
249
     */
250
    public function render()
251
    {
252
        $module = $this->getModule();
253
        $moduleDirname = $module->getVar('mod_dirname');
254
        $table = $this->getTable();
255
        $tables = $this->getTables();
256
        $filename = $this->getFileName();
257
        $content = $this->getHeaderFilesComments($module, $filename);
258
        $content .= $this->getInstallModuleFolder($moduleDirname);
259
260
        foreach (array_keys($tables) as $t) {
261
            $tableName = $tables[$t]->getVar('table_name');
262
            $tableInstall[] = $tables[$t]->getVar('table_install');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tableInstall was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tableInstall = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
263
            $content .= $this->getInstallTableFolder($moduleDirname, $tableName);
264
        }
265
        if (in_array(1, $tableInstall)) {
0 ignored issues
show
Bug introduced by
The variable $tableInstall 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...
266
            $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id'));
267
            foreach (array_keys($fields) as $f) {
268
                $fieldElement = $fields[$f]->getVar('field_element');
269
                // All fields elements selected
270
                switch ($fieldElement) {
271
                    case 10:
272
                    case 13:
273
                        $content .= $this->getInstallImagesFolder($moduleDirname);
274
                        foreach (array_keys($tables) as $t) {
275
                            $tableName = $tables[$t]->getVar('table_name');
276
                            $content .= $this->getInstallTableImagesFolder($tableName);
277
                        }
278
                        break;
279
                    case 11:
280
                        $content .= $this->getInstallImagesShotsFolder();
0 ignored issues
show
Bug introduced by
The call to getInstallImagesShotsFolder() misses a required argument $moduleDirname.

This check looks for function calls that miss required arguments.

Loading history...
281
                        break;
282
                    case 12:
283
                    case 14:
284
                        $content .= $this->getInstallFilesFolder($moduleDirname);
285
                        foreach (array_keys($tables) as $t) {
286
                            $tableName = $tables[$t]->getVar('table_name');
287
                            $content .= $this->getInstallTableFilesFolder($tableName);
288
                        }
289
                        break;
290
                }
291
            }
292
        }
293
        $content .= $this->getInstallFooter();
294
        //
295
        $this->create($moduleDirname, 'include', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED);
296
297
        return $this->renderFile();
298
    }
299
}
300