| Conditions | 12 |
| Paths | 48 |
| Total Lines | 89 |
| Code Lines | 45 |
| Lines | 34 |
| Ratio | 38.2 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 259 | public function getScript() |
||
| 260 | { |
||
| 261 | /* |
||
| 262 | * JQuery variables sometimes depend on the context where they are used, eg. when their value depends on $(this). |
||
| 263 | * When a confirmation question is added, the Jaxon calls are made in a different context, |
||
| 264 | * making those variables invalid. |
||
| 265 | * To avoid issues related to these context changes, the JQuery selectors values are first saved into |
||
| 266 | * local variables, which are then used in Jaxon function calls. |
||
| 267 | */ |
||
| 268 | $sVars = ''; // Javascript code defining all the variables values. |
||
| 269 | $nVarId = 1; // Position of the variables, starting from 1. |
||
| 270 | // This array will avoid declaring multiple variables with the same value. |
||
| 271 | // The array key is the variable value, while the array value is the variable name. |
||
| 272 | $aVariables = array(); // Array of local variables. |
||
| 273 | foreach($this->aParameters as &$xParameter) |
||
| 274 | { |
||
| 275 | $sParameterStr = $xParameter->getScript(); |
||
| 276 | View Code Duplication | if($xParameter instanceof \Jaxon\JQuery\Dom\Element) |
|
| 277 | { |
||
| 278 | if(!array_key_exists($sParameterStr, $aVariables)) |
||
| 279 | { |
||
| 280 | // The value is not yet defined. A new variable is created. |
||
| 281 | $sVarName = "jxnVar$nVarId"; |
||
| 282 | $aVariables[$sParameterStr] = $sVarName; |
||
| 283 | $sVars .= "$sVarName=$xParameter;"; |
||
| 284 | $nVarId++; |
||
| 285 | } |
||
| 286 | else |
||
| 287 | { |
||
| 288 | // The value is already defined. The corresponding variable is assigned. |
||
| 289 | $sVarName = $aVariables[$sParameterStr]; |
||
| 290 | } |
||
| 291 | $xParameter = new Parameter(Jaxon::JS_VALUE, $sVarName); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | $sPhrase = ''; |
||
| 296 | if(count($this->aMessageArgs) > 0) |
||
| 297 | { |
||
| 298 | $sPhrase = array_shift($this->aMessageArgs); // The first array entry is the question. |
||
| 299 | // $sPhrase = "'" . addslashes($sPhrase) . "'"; // Wrap the phrase with single quotes |
||
| 300 | if(count($this->aMessageArgs) > 0) |
||
| 301 | { |
||
| 302 | $nParamId = 1; |
||
| 303 | foreach($this->aMessageArgs as &$xParameter) |
||
| 304 | { |
||
| 305 | $sParameterStr = $xParameter->getScript(); |
||
| 306 | View Code Duplication | if($xParameter instanceof \Jaxon\JQuery\Dom\Element) |
|
| 307 | { |
||
| 308 | if(!array_key_exists($sParameterStr, $aVariables)) |
||
| 309 | { |
||
| 310 | // The value is not yet defined. A new variable is created. |
||
| 311 | $sVarName = "jxnVar$nVarId"; |
||
| 312 | $aVariables[$sParameterStr] = $sVarName; |
||
| 313 | $sVars .= "$sVarName=$xParameter;"; |
||
| 314 | $nVarId++; |
||
| 315 | } |
||
| 316 | else |
||
| 317 | { |
||
| 318 | // The value is already defined. The corresponding variable is assigned. |
||
| 319 | $sVarName = $aVariables[$sParameterStr]; |
||
| 320 | } |
||
| 321 | $xParameter = new Parameter(Jaxon::JS_VALUE, $sVarName); |
||
| 322 | } |
||
| 323 | $xParameter = "'$nParamId':" . $xParameter->getScript(); |
||
| 324 | $nParamId++; |
||
| 325 | } |
||
| 326 | $sPhrase .= '.supplant({' . implode(',', $this->aMessageArgs) . '})'; |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | $sScript = $this->getOption('core.prefix.' . $this->sType) . parent::getScript(); |
||
| 331 | if($this->sCondition == '__confirm__') |
||
| 332 | { |
||
| 333 | $xConfirm = $this->getPluginManager()->getConfirm(); |
||
| 334 | $sScript = $xConfirm->confirm($sPhrase, $sScript, ''); |
||
| 335 | } |
||
| 336 | elseif($this->sCondition !== null) |
||
| 337 | { |
||
| 338 | $sScript = 'if(' . $this->sCondition . '){' . $sScript . ';}'; |
||
| 339 | if(($sPhrase)) |
||
| 340 | { |
||
| 341 | $xAlert = $this->getPluginManager()->getAlert(); |
||
| 342 | $xAlert->setReturn(true); |
||
| 343 | $sScript .= 'else{' . $xAlert->warning($sPhrase) . ';}'; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | return $sVars . $sScript; |
||
| 347 | } |
||
| 348 | |||
| 359 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.