| Conditions | 34 |
| Paths | > 20000 |
| Total Lines | 172 |
| Code Lines | 98 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 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 |
||
| 175 | public static function addLabels($language, $labels, $moduleName, $basepath = null, $forRelationshipLabel = false) |
||
| 176 | { |
||
| 177 | $GLOBALS [ 'log' ]->debug("ParserLabel->addLabels($language, \$labels, $moduleName, $basepath );"); |
||
| 178 | $GLOBALS [ 'log' ]->debug('$labels:'.print_r($labels, true)); |
||
| 179 | |||
| 180 | $deployedModule = false; |
||
| 181 | if (is_null($basepath)) { |
||
| 182 | $deployedModule = true; |
||
| 183 | $basepath = "custom/modules/$moduleName/language"; |
||
| 184 | if ($forRelationshipLabel) { |
||
| 185 | $basepath = "custom/modules/$moduleName/Ext/Language"; |
||
| 186 | } |
||
| 187 | if (!is_dir($basepath)) { |
||
| 188 | mkdir_recursive($basepath); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | $filename = "$basepath/$language.lang.php"; |
||
| 193 | if ($forRelationshipLabel) { |
||
| 194 | $filename = "$basepath/$language.lang.ext.php"; |
||
| 195 | } |
||
| 196 | $dir_exists = is_dir($basepath); |
||
| 197 | |||
| 198 | $mod_strings = array(); |
||
| 199 | |||
| 200 | if ($dir_exists) { |
||
| 201 | if (file_exists($filename)) { |
||
| 202 | // obtain $mod_strings |
||
| 203 | include $filename; |
||
| 204 | } elseif ($forRelationshipLabel) { |
||
| 205 | $fh = fopen($filename, 'a'); |
||
| 206 | fclose($fh); |
||
| 207 | } |
||
| 208 | } else { |
||
| 209 | return false; |
||
| 210 | } |
||
| 211 | |||
| 212 | $changed = false; |
||
| 213 | |||
| 214 | //$charset = (isset($app_strings['LBL_CHARSET'])) ? $app_strings['LBL_CHARSET'] : $GLOBALS['sugar_config']['default_charset'] ; |
||
| 215 | |||
| 216 | foreach ($labels as $key => $value) { |
||
| 217 | if (!isset($mod_strings [ $key ]) || strcmp($value, $mod_strings [ $key ]) != 0) { |
||
| 218 | $mod_strings [$key] = to_html(strip_tags(from_html($value))); // must match encoding used in view.labels.php |
||
| 219 | $changed = true; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | if ($changed) { |
||
| 224 | $GLOBALS [ 'log' ]->debug("ParserLabel->addLabels: writing new mod_strings to $filename"); |
||
| 225 | $GLOBALS [ 'log' ]->debug('ParserLabel->addLabels: mod_strings='.print_r($mod_strings, true)); |
||
| 226 | if (!write_array_to_file('mod_strings', $mod_strings, $filename)) { |
||
| 227 | $GLOBALS [ 'log' ]->fatal("Could not write $filename"); |
||
| 228 | } else { |
||
| 229 | // if we have a cache to worry about, then clear it now |
||
| 230 | if ($deployedModule) { |
||
| 231 | SugarCache::cleanOpcodes(); |
||
| 232 | $GLOBALS [ 'log' ]->debug('PaserLabel->addLabels: clearing language cache'); |
||
| 233 | $cache_key = 'module_language.'.$language.$moduleName; |
||
| 234 | sugar_cache_clear($cache_key); |
||
| 235 | LanguageManager::clearLanguageCache($moduleName, $language); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | // Fix for bug #51 |
||
| 241 | // when the label is recreated it defaults back to the original value (In this case its "User"). |
||
| 242 | |||
| 243 | // Solution: |
||
| 244 | // 1. Changes to the label names should go to custom/Extension/modules/{ModuleName}/Ext/Language |
||
| 245 | // This is done in case different users edit the same Relationship concurrently. |
||
| 246 | // The changes from custom/Extension/modules/{ModuleName}/Ext/Language |
||
| 247 | // will overwrite stuff in custom/modules/{ModuleName}/Ext/Language/en_us.lang.ext.php after |
||
| 248 | // Quick Repair and Rebuild is applied. |
||
| 249 | if ($forRelationshipLabel) { |
||
| 250 | if (!empty($_POST[view_module]) && !empty($_POST[relationship_name]) && !empty($_POST[rhs_label]) && !empty($_POST[lhs_module])) { |
||
| 251 | // 1. Overwrite custom/Extension/modules/{ModuleName}/Ext/Language |
||
| 252 | $extension_basepath = 'custom/Extension/modules/'.$_POST[view_module].'/Ext/Language'; |
||
| 253 | mkdir_recursive($extension_basepath); |
||
| 254 | |||
| 255 | $headerString = "<?php\n//THIS FILE IS AUTO GENERATED, DO NOT MODIFY\n"; |
||
| 256 | $out = $headerString; |
||
| 257 | |||
| 258 | $extension_filename = "$extension_basepath/$language.custom".$_POST[relationship_name].'.php'; |
||
| 259 | |||
| 260 | $mod_strings = array(); |
||
| 261 | if (file_exists($extension_filename)) { |
||
| 262 | // obtain $mod_strings |
||
| 263 | include $extension_filename; |
||
| 264 | } |
||
| 265 | |||
| 266 | foreach ($labels as $key => $value) { |
||
| 267 | foreach ($mod_strings as $key_mod_string => $value_mod_string) { |
||
| 268 | if (strpos($key_mod_string, strtoupper($_POST[relationship_name])) !== false) { |
||
| 269 | $mod_strings[$key_mod_string] = to_html(strip_tags(from_html($_POST[rhs_label]))); // must match encoding used in view.labels.php |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | foreach ($mod_strings as $key => $val) { |
||
| 275 | $out .= override_value_to_string_recursive2('mod_strings', $key, $val); |
||
| 276 | } |
||
| 277 | |||
| 278 | try { |
||
| 279 | $file_contents = fopen($extension_filename, 'w'); |
||
| 280 | fputs($file_contents, $out, strlen($out)); |
||
| 281 | fclose($file_contents); |
||
| 282 | } catch (Exception $e) { |
||
| 283 | $GLOBALS ['log']->fatal("Could not write $filename"); |
||
| 284 | $GLOBALS ['log']->fatal('Exception '.$e->getMessage()); |
||
| 285 | } |
||
| 286 | |||
| 287 | //2. Overwrite custom/Extension/modules/relationships/language/{ModuleName}.php |
||
| 288 | // Also need to overwrite custom/Extension/modules/relationships/language/{ModuleName}.php |
||
| 289 | // because whenever new relationship is created this place is checked by the system to get |
||
| 290 | // all the label names |
||
| 291 | $relationships_basepath = 'custom/Extension/modules/relationships/language'; |
||
| 292 | mkdir_recursive($relationships_basepath); |
||
| 293 | |||
| 294 | $headerString = "<?php\n//THIS FILE IS AUTO GENERATED, DO NOT MODIFY\n"; |
||
| 295 | $out = $headerString; |
||
| 296 | |||
| 297 | $relationships_filename = "$relationships_basepath/".$_POST[lhs_module].'.php'; |
||
| 298 | |||
| 299 | $mod_strings = array(); |
||
| 300 | if (file_exists($relationships_filename)) { |
||
| 301 | // obtain $mod_strings |
||
| 302 | include $relationships_filename; |
||
| 303 | } |
||
| 304 | |||
| 305 | $changed_mod_strings = false; |
||
| 306 | foreach ($labels as $key => $value) { |
||
| 307 | foreach ($mod_strings as $key_mod_string => $value_mod_string) { |
||
| 308 | if (strpos($key_mod_string, strtoupper($_POST[relationship_name])) !== false) { |
||
| 309 | $mod_strings[$key_mod_string] = to_html(strip_tags(from_html($_POST[rhs_label]))); // must match encoding used in view.labels.php |
||
| 310 | $changed_mod_strings = true; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | foreach ($mod_strings as $key => $val) { |
||
| 316 | $out .= override_value_to_string_recursive2('mod_strings', $key, $val); |
||
| 317 | } |
||
| 318 | |||
| 319 | $failed_to_write = false; |
||
| 320 | try { |
||
| 321 | $file_contents = fopen($relationships_filename, 'w'); |
||
| 322 | fputs($file_contents, $out, strlen($out)); |
||
| 323 | fclose($file_contents); |
||
| 324 | } catch (Exception $e) { |
||
| 325 | $GLOBALS ['log']->fatal("Could not write $filename"); |
||
| 326 | $GLOBALS ['log']->fatal('Exception '.$e->getMessage()); |
||
| 327 | $failed_to_write = true; |
||
| 328 | } |
||
| 329 | |||
| 330 | if ($changed_mod_strings) { |
||
| 331 | if (!$failed_to_write) { |
||
| 332 | // if we have a cache to worry about, then clear it now |
||
| 333 | if ($deployedModule) { |
||
| 334 | SugarCache::cleanOpcodes(); |
||
| 335 | $GLOBALS ['log']->debug('PaserLabel->addLabels: clearing language cache'); |
||
| 336 | $cache_key = 'module_language.'.$language.$moduleName; |
||
| 337 | sugar_cache_clear($cache_key); |
||
| 338 | LanguageManager::clearLanguageCache($moduleName, $language); |
||
| 339 | } |
||
| 340 | } |
||
| 341 | } |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | return true; |
||
| 346 | } |
||
| 347 | |||
| 372 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.