Conditions | 12 |
Paths | 48 |
Total Lines | 88 |
Lines | 34 |
Ratio | 38.64 % |
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 |
||
135 | public function getScript() |
||
136 | { |
||
137 | /* |
||
138 | * JQuery variables sometimes depend on the context where they are used, eg. when their value depends on $(this). |
||
139 | * When a confirmation question is added, the Jaxon calls are made in a different context, |
||
140 | * making those variables invalid. |
||
141 | * To avoid issues related to these context changes, the JQuery selectors values are first saved into |
||
142 | * local variables, which are then used in Jaxon function calls. |
||
143 | */ |
||
144 | $sVars = ''; // Javascript code defining all the variables values. |
||
145 | $nVarId = 1; // Position of the variables, starting from 1. |
||
146 | // This array will avoid declaring multiple variables with the same value. |
||
147 | // The array key is the variable value, while the array value is the variable name. |
||
148 | $aVariables = []; // Array of local variables. |
||
149 | foreach($this->aParameters as &$xParameter) |
||
150 | { |
||
151 | $sParameterStr = $xParameter->getScript(); |
||
152 | View Code Duplication | if($xParameter instanceof \Jaxon\Response\Plugin\JQuery\Dom\Element) |
|
|
|||
153 | { |
||
154 | if(!array_key_exists($sParameterStr, $aVariables)) |
||
155 | { |
||
156 | // The value is not yet defined. A new variable is created. |
||
157 | $sVarName = "jxnVar$nVarId"; |
||
158 | $aVariables[$sParameterStr] = $sVarName; |
||
159 | $sVars .= "$sVarName=$xParameter;"; |
||
160 | $nVarId++; |
||
161 | } |
||
162 | else |
||
163 | { |
||
164 | // The value is already defined. The corresponding variable is assigned. |
||
165 | $sVarName = $aVariables[$sParameterStr]; |
||
166 | } |
||
167 | $xParameter = new Parameter(Jaxon::JS_VALUE, $sVarName); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | $sPhrase = ''; |
||
172 | if(count($this->aMessageArgs) > 0) |
||
173 | { |
||
174 | $sPhrase = array_shift($this->aMessageArgs); // The first array entry is the question. |
||
175 | // $sPhrase = "'" . addslashes($sPhrase) . "'"; // Wrap the phrase with single quotes |
||
176 | if(count($this->aMessageArgs) > 0) |
||
177 | { |
||
178 | $nParamId = 1; |
||
179 | foreach($this->aMessageArgs as &$xParameter) |
||
180 | { |
||
181 | $sParameterStr = $xParameter->getScript(); |
||
182 | View Code Duplication | if($xParameter instanceof \Jaxon\Response\Plugin\JQuery\Dom\Element) |
|
183 | { |
||
184 | if(!array_key_exists($sParameterStr, $aVariables)) |
||
185 | { |
||
186 | // The value is not yet defined. A new variable is created. |
||
187 | $sVarName = "jxnVar$nVarId"; |
||
188 | $aVariables[$sParameterStr] = $sVarName; |
||
189 | $sVars .= "$sVarName=$xParameter;"; |
||
190 | $nVarId++; |
||
191 | } |
||
192 | else |
||
193 | { |
||
194 | // The value is already defined. The corresponding variable is assigned. |
||
195 | $sVarName = $aVariables[$sParameterStr]; |
||
196 | } |
||
197 | $xParameter = new Parameter(Jaxon::JS_VALUE, $sVarName); |
||
198 | } |
||
199 | $xParameter = "'$nParamId':" . $xParameter->getScript(); |
||
200 | $nParamId++; |
||
201 | } |
||
202 | $sPhrase .= '.supplant({' . implode(',', $this->aMessageArgs) . '})'; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | $sScript = parent::getScript(); |
||
207 | $xDialog = jaxon()->dialog(); |
||
208 | if($this->sCondition == '__confirm__') |
||
209 | { |
||
210 | $sScript = $xDialog->confirm($sPhrase, $sScript, ''); |
||
211 | } |
||
212 | elseif($this->sCondition !== null) |
||
213 | { |
||
214 | $sScript = 'if(' . $this->sCondition . '){' . $sScript . ';}'; |
||
215 | if(($sPhrase)) |
||
216 | { |
||
217 | $xDialog->getAlert()->setReturn(true); |
||
218 | $sScript .= 'else{' . $xDialog->warning($sPhrase) . ';}'; |
||
219 | } |
||
220 | } |
||
221 | return $sVars . $sScript; |
||
222 | } |
||
223 | |||
234 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.