Conditions | 21 |
Paths | 260 |
Total Lines | 137 |
Code Lines | 65 |
Lines | 13 |
Ratio | 9.49 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
72 | function xoops_module_update_contact(XoopsModule $module, $previousVersion = null) |
||
73 | { |
||
74 | $moduleDirName = basename(dirname(__DIR__)); |
||
75 | $capsDirName = strtoupper($moduleDirName); |
||
76 | |||
77 | $xoopsDB = XoopsDatabaseFactory::getDatabaseConnection(); |
||
78 | |||
79 | if ($previousVersion < 180) { |
||
80 | $sql = 'CREATE TABLE ' . $xoopsDB->prefix('contact') . ' ( |
||
81 | contact_id int(10) unsigned NOT NULL auto_increment, |
||
82 | contact_uid int(10) NOT NULL, |
||
83 | contact_cid int(10) NOT NULL, |
||
84 | contact_create int(10) NOT NULL, |
||
85 | contact_subject varchar(255) NOT NULL, |
||
86 | contact_name varchar(255) NOT NULL, |
||
87 | contact_mail varchar(255) NOT NULL, |
||
88 | contact_url varchar(255) NOT NULL, |
||
89 | contact_icq varchar(255) NOT NULL, |
||
90 | contact_company varchar(255) NOT NULL, |
||
91 | contact_location varchar(255) NOT NULL, |
||
92 | contact_department varchar(60) NOT NULL, |
||
93 | contact_ip varchar(20) NOT NULL, |
||
94 | contact_phone varchar(20) NOT NULL, |
||
95 | contact_message text NOT NULL, |
||
96 | contact_address text NOT NULL, |
||
97 | contact_reply tinyint(1) NOT NULL, |
||
98 | PRIMARY KEY (contact_id) |
||
99 | ) ENGINE=MyISAM;'; |
||
100 | $xoopsDB->query($sql); |
||
101 | } |
||
102 | |||
103 | if ($previousVersion < 181) { |
||
104 | // Add contact_platform |
||
105 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . "` ADD `contact_platform` ENUM('Android','Ios','Web') NOT NULL DEFAULT 'Web'"; |
||
106 | $xoopsDB->query($sql); |
||
107 | // Add contact_type |
||
108 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . "` ADD `contact_type` ENUM('Contact','Phone','Mail') NOT NULL DEFAULT 'Contact'"; |
||
109 | $xoopsDB->query($sql); |
||
110 | // Add index contact_uid |
||
111 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_uid` ( `contact_uid` )'; |
||
112 | $xoopsDB->query($sql); |
||
113 | // Add index contact_cid |
||
114 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_cid` ( `contact_cid` )'; |
||
115 | $xoopsDB->query($sql); |
||
116 | // Add index contact_create |
||
117 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_create` ( `contact_create` )'; |
||
118 | $xoopsDB->query($sql); |
||
119 | // Add index contact_mail |
||
120 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_mail` ( `contact_mail` )'; |
||
121 | $xoopsDB->query($sql); |
||
122 | // Add index contact_phone |
||
123 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_phone` ( `contact_phone` )'; |
||
124 | $xoopsDB->query($sql); |
||
125 | // Add index contact_platform |
||
126 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_platform` ( `contact_platform` )'; |
||
127 | $xoopsDB->query($sql); |
||
128 | // Add index contact_type |
||
129 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('contact') . '` ADD INDEX `contact_type` ( `contact_type` )'; |
||
130 | $xoopsDB->query($sql); |
||
131 | } |
||
132 | |||
133 | if ($previousVersion < 226) { |
||
134 | require_once __DIR__ . '/config.php'; |
||
135 | $configurator = new ContactConfigurator(); |
||
136 | /** @var ContactUtility $utilityClass */ |
||
137 | $utilityClass = ucfirst($moduleDirName) . 'Utility'; |
||
138 | if (!class_exists($utilityClass)) { |
||
139 | xoops_load('utility', $moduleDirName); |
||
140 | } |
||
141 | |||
142 | //delete old HTML templates |
||
143 | if (count($configurator->templateFolders) > 0) { |
||
144 | foreach ($configurator->templateFolders as $folder) { |
||
145 | $templateFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $folder); |
||
146 | if (is_dir($templateFolder)) { |
||
147 | $templateList = array_diff(scandir($templateFolder, SCANDIR_SORT_NONE), ['..', '.']); |
||
148 | foreach ($templateList as $k => $v) { |
||
149 | $fileInfo = new SplFileInfo($templateFolder . $v); |
||
150 | if ($fileInfo->getExtension() === 'html' && $fileInfo->getFilename() !== 'index.html') { |
||
151 | if (file_exists($templateFolder . $v)) { |
||
152 | unlink($templateFolder . $v); |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | // --- DELETE OLD FILES --------------- |
||
161 | if (count($configurator->oldFiles) > 0) { |
||
162 | // foreach (array_keys($GLOBALS['uploadFolders']) as $i) { |
||
163 | foreach (array_keys($configurator->oldFiles) as $i) { |
||
164 | $tempFile = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFiles[$i]); |
||
165 | if (is_file($tempFile)) { |
||
166 | unlink($tempFile); |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | // --- DELETE OLD FOLDERS --------------- |
||
172 | xoops_load('XoopsFile'); |
||
173 | if (count($configurator->oldFolders) > 0) { |
||
174 | // foreach (array_keys($GLOBALS['uploadFolders']) as $i) { |
||
175 | foreach (array_keys($configurator->oldFolders) as $i) { |
||
176 | $tempFolder = $GLOBALS['xoops']->path('modules/' . $moduleDirName . $configurator->oldFolders[$i]); |
||
177 | /* @var $folderHandler XoopsObjectHandler */ |
||
178 | $folderHandler = XoopsFile::getHandler('folder', $tempFolder); |
||
179 | $folderHandler->delete($tempFolder); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | // --- CREATE FOLDERS --------------- |
||
184 | View Code Duplication | if (count($configurator->uploadFolders) > 0) { |
|
185 | // foreach (array_keys($GLOBALS['uploadFolders']) as $i) { |
||
186 | foreach (array_keys($configurator->uploadFolders) as $i) { |
||
187 | $utilityClass::createFolder($configurator->uploadFolders[$i]); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | // --- COPY blank.png FILES --------------- |
||
192 | View Code Duplication | if (count($configurator->blankFiles) > 0) { |
|
193 | $file = __DIR__ . '/../assets/images/blank.png'; |
||
194 | foreach (array_keys($configurator->blankFiles) as $i) { |
||
195 | $dest = $configurator->blankFiles[$i] . '/blank.png'; |
||
196 | $utilityClass::copyFile($file, $dest); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | //delete .html entries from the tpl table |
||
201 | $sql = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('tplfile') . " WHERE `tpl_module` = '" . $module->getVar('dirname', 'n') . '\' AND `tpl_file` LIKE \'%.html%\''; |
||
202 | $GLOBALS['xoopsDB']->queryF($sql); |
||
203 | |||
204 | /** @var XoopsGroupPermHandler $gpermHandler */ |
||
205 | $gpermHandler = xoops_getHandler('groupperm'); |
||
206 | return $gpermHandler->deleteByModule($module->getVar('mid'), 'item_read'); |
||
207 | } |
||
208 | } |
||
209 |
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.