Completed
Branch develop (420c52)
by
unknown
27:28
created

modulebuilder.lib.php ➔ rebuildObjectClass()   F

Complexity

Conditions 16
Paths 282

Size

Total Lines 89
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 49
nc 282
nop 4
dl 0
loc 89
rs 3.7109
c 0
b 0
f 0

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
2
/* Copyright (C) 2009-2010 Laurent Destailleur  <[email protected]>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 * or see http://www.gnu.org/
17
 */
18
19
/**
20
 *  \file		htdocs/core/lib/memory.lib.php
21
 *  \brief		Set of function for memory/cache management
22
 */
23
24
25
26
27
/**
28
 * 	Save data into a memory area shared by all users, all sessions on server
29
 *
30
 *  @param	string      $destdir		Directory
31
 * 	@param	string		$module			Module name
32
 *  @param	string      $objectname		Name of object
33
 * 	@param	string		$newmask		New mask
34
 * 	@return	int							<0 if KO, >0 if OK
35
 */
36
function rebuildObjectClass($destdir, $module, $objectname, $newmask)
37
{
38
    global $db, $langs;
39
40
    if (empty($objectname)) return -1;
41
42
    $pathoffiletoeditsrc=$destdir.'/class/'.strtolower($objectname).'.class.php';
43
    $pathoffiletoedittarget=$destdir.'/class/'.strtolower($objectname).'.class.php';
44
    if (! dol_is_file($pathoffiletoeditsrc))
45
    {
46
        //$pathoffiletoeditsrc=DOL_DOCUMENT_ROOT.'/modulebuilder/template/class/myobject.class.php';
47
        setEventMessages($langs->trans("ErrorFileNotFound", $pathoffiletoeditsrc), null, 'errors');
48
        return -1;
49
    }
50
51
    //$pathoffiletoedittmp=$destdir.'/class/'.strtolower($objectname).'.class.php.tmp';
52
    //dol_delete_file($pathoffiletoedittmp, 0, 1, 1);
53
54
    try
55
    {
56
        include_once $pathoffiletoeditsrc;
57
        if (class_exists($objectname)) $object=new $objectname($db);
58
        else return -1;
59
60
        // Backup old file
61
        dol_copy($pathoffiletoeditsrc, $pathoffiletoeditsrc.'.back', $newmask, 1);
62
63
        // Edit class files
64
        $contentclass = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r');
65
66
        $i=0;
67
        $texttoinsert = '// BEGIN MODULEBUILDER PROPERTIES'."\n";
68
        $texttoinsert.= "\t".'/**'."\n";
69
        $texttoinsert.= "\t".' * @var array  Array with all fields and their property'."\n";
70
        $texttoinsert.= "\t".' */'."\n";
71
        $texttoinsert.= "\t".'public $fields=array('."\n";
72
73
        if (count($object->fields))
74
        {
75
            foreach($object->fields as $key => $val)
76
            {
77
                $i++;
78
                $typephp='';
79
                $texttoinsert.= "\t\t'".$key."' => array('type'=>'".$val['type']."', 'label'=>'".$val['label']."',";
80
                $texttoinsert.= " 'visible'=>".($val['visible']?$val['visible']:0).",";
81
                $texttoinsert.= " 'enabled'=>".($val['enabled']?$val['enabled']:0).",";
82
                if ($val['position']) $texttoinsert.= " 'position'=>".$val['position'].",";
83
                if ($val['notnull']) $texttoinsert.= " 'notnull'=>".$val['notnull'].",";
84
                if ($val['index']) $texttoinsert.= " 'index'=>".$val['index'].",";
85
                if ($val['searchall']) $texttoinsert.= " 'searchall'=>".$val['searchall'].",";
86
                if ($val['comment']) $texttoinsert.= " 'comment'=>'".$val['comment']."',";
87
                $texttoinsert.= "),\n";
88
            }
89
        }
90
        $texttoinsert.= "\t".');'."\n";
91
92
        $texttoinsert.= "\n";
93
94
        if (count($object->fields))
95
        {
96
            foreach($object->fields as $key => $val)
97
            {
98
                $i++;
99
                $typephp='';
100
                $texttoinsert.= "\t".'public $'.$key.$typephp.";";
101
                //if ($key == 'rowid')  $texttoinsert.= ' AUTO_INCREMENT PRIMARY KEY';
102
                //if ($key == 'entity') $texttoinsert.= ' DEFAULT 1';
103
                //$texttoinsert.= ($val['notnull']?' NOT NULL':'');
104
                //if ($i < count($object->fields)) $texttoinsert.=";";
105
                $texttoinsert.= "\n";
106
            }
107
        }
108
109
        $texttoinsert.= "\t".'// END MODULEBUILDER PROPERTIES';
110
111
        $contentclass = preg_replace('/\/\/ BEGIN MODULEBUILDER PROPERTIES.*END MODULEBUILDER PROPERTIES/ims', $texttoinsert, $contentclass);
112
113
        //file_put_contents($pathoffiletoedittmp, $contentclass);
114
        file_put_contents(dol_osencode($pathoffiletoedittarget), $contentclass);
115
        @chmod($pathoffiletoedittarget, octdec($newmask));
116
117
        return 1;
118
    }
119
    catch(Exception $e)
120
    {
121
        print $e->getMessage();
122
        return -1;
123
    }
124
}
125
126
/**
127
 * 	Save data into a memory area shared by all users, all sessions on server
128
 *
129
 *  @param	string      $destdir		Directory
130
 * 	@param	string		$module			Module name
131
 *  @param	string      $objectname		Name of object
132
 * 	@param	string		$newmask		New mask
133
 * 	@return	int							<0 if KO, >0 if OK
134
 */
135
function rebuildObjectSql($destdir, $module, $objectname, $newmask)
136
{
137
    global $db, $langs;
138
139
    if (empty($objectname)) return -1;
140
141
    try
142
    {
143
        dol_include_once(strtolower($module).'/class/'.strtolower($objectname).'.class.php');
144
        if (class_exists($objectname)) $object=new $objectname($db);
145
        else return -1;
146
    }
147
    catch(Exception $e)
148
    {
149
        print $e->getMessage();
150
    }
151
152
    // Edit .sql file
153
    $pathoffiletoeditsrc=dol_osencode($destdir.'/sql/llx_'.strtolower($objectname).'.sql');
154
    $pathoffiletoedittarget=dol_osencode($destdir.'/sql/llx_'.strtolower($objectname).'.sql');
155
156
    $contentsql = file_get_contents($pathoffiletoeditsrc, 'r');
157
158
    $i=0;
159
    $texttoinsert = '-- BEGIN MODULEBUILDER FIELDS'."\n";
160
    if (count($object->fields))
161
    {
162
        foreach($object->fields as $key => $val)
163
        {
164
            $i++;
165
            $texttoinsert.= "\t".$key." ".$val['type'];
166
            if ($key == 'rowid')  $texttoinsert.= ' AUTO_INCREMENT PRIMARY KEY';
167
            if ($key == 'entity') $texttoinsert.= ' DEFAULT 1';
168
            $texttoinsert.= ($val['notnull']?' NOT NULL':'');
169
            if ($i < count($object->fields)) $texttoinsert.=", ";
0 ignored issues
show
Bug introduced by
The variable $object 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...
170
            $texttoinsert.= "\n";
171
        }
172
    }
173
    $texttoinsert.= "\t".'-- END MODULEBUILDER FIELDS';
174
175
    $contentsql = preg_replace('/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql);
176
177
    file_put_contents($pathoffiletoedittarget, $contentsql);
178
    @chmod($pathoffiletoedittarget, octdec($newmask));
179
180
181
    // Edit .key.sql file
182
    $pathoffiletoeditsrc=dol_osencode($destdir.'/sql/llx_'.strtolower($objectname).'.key.sql');
183
    $pathoffiletoedittarget=dol_osencode($destdir.'/sql/llx_'.strtolower($objectname).'.key.sql');
184
185
    $contentsql = file_get_contents($pathoffiletoeditsrc, 'r');
186
187
    $i=0;
188
    $texttoinsert = '-- BEGIN MODULEBUILDER INDEXES'."\n";
189
    if (count($object->fields))
190
    {
191
        foreach($object->fields as $key => $val)
192
        {
193
            $i++;
194
            if ($val['index'])
195
            {
196
                $texttoinsert.= "ALTER TABLE llx_".strtolower($objectname)." ADD INDEX idx_".strtolower($objectname)."_".$key." (".$key.");";
197
                $texttoinsert.= "\n";
198
            }
199
        }
200
    }
201
    $texttoinsert.= '-- END MODULEBUILDER INDEXES';
202
203
    $contentsql = preg_replace('/-- BEGIN MODULEBUILDER INDEXES.*END MODULEBUILDER INDEXES/ims', $texttoinsert, $contentsql);
204
205
    file_put_contents($pathoffiletoedittarget, $contentsql);
206
    @chmod($pathoffiletoedittarget, octdec($newmask));
207
208
    return 1;
209
}
210
211
212