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