Prototype::remote_function()   D
last analyzed

Complexity

Conditions 12
Paths 320

Size

Total Lines 26

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
cc 12
nc 320
nop 1
dl 26
loc 26
rs 4.6333
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * Projax
5
 *
6
 * An open source set of php helper classes for prototype and script.aculo.us.
7
 *
8
 * @package     Projax
9
 * @author      Vikas Patial
10
 * @copyright   Copyright (c) 2006, ngcoders.
11
 * @license     http://www.gnu.org/copyleft/gpl.html
12
 * @link        http://www.ngcoders.com
13
 * @since       Version 0.2
14
 * @filesource
15
 */
16 View Code Duplication
class Prototype extends JavaScript
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
17
{
18
    public $CALLBACKS = [
19
        'uninitialized',
20
        'loading',
21
        'loaded',
22
        'interactive',
23
        'complete',
24
        'failure',
25
        'success'
26
    ];
27
28
    public $AJAX_OPTIONS = [
29
        'before',
30
        'after',
31
        'condition',
32
        'url',
33
        'asynchronous',
34
        'method',
35
        'insertion',
36
        'position',
37
        'form',
38
        'with',
39
        'update',
40
        'script',
41
        'uninitialized',
42
        'loading',
43
        'loaded',
44
        'interactive',
45
        'complete',
46
        'failure',
47
        'success'
48
    ];
49
50
    /**
51
     * @return string
52
     */
53
    public function evaluate_remote_response()
54
    {
55
        return 'eval(request.responseText)';
56
    }
57
58
    /**
59
     * @param $options
60
     * @return string
61
     */
62
    public function form_remote_tag($options)
63
    {
64
        $options['form'] = true;
65
66
        return '<form action="' . $options['url'] . '" onsubmit="' . $this->remote_function($options) . '; return false;" method="' . (isset($options['method']) ? $options['method'] : 'post') . '"  >';
67
    }
68
69
    /**
70
     * @param         $name
71
     * @param  null   $options
72
     * @param  null   $html_options
73
     * @return string
74
     */
75
    public function link_to_remote($name, $options = null, $html_options = null)
76
    {
77
        return $this->link_to_function($name, $this->remote_function($options), $html_options);
78
    }
79
80
    /**
81
     * @param         $field_id
82
     * @param  null   $options
83
     * @return string
84
     */
85
    public function observe_field($field_id, $options = null)
86
    {
87
        if (isset($options['frequency']) && $options['frequency'] > 0) {
88
            return $this->_build_observer('Form.Element.Observer', $field_id, $options);
89
        } else {
90
            return $this->_build_observer('Form.Element.EventObserver', $field_id, $options);
91
        }
92
    }
93
94
    /**
95
     * @param         $form_id
96
     * @param  null   $options
97
     * @return string
98
     */
99
    public function observe_form($form_id, $options = null)
100
    {
101
        if (isset($options['frequency'])) {
102
            return $this->_build_observer('Form.Observer', $form_id, $options);
103
        } else {
104
            return $this->_build_observer('Form.EventObserver', $form_id, $options);
105
        }
106
    }
107
108
    /**
109
     * @param  null $options
110
     * @return string
111
     */
112
    public function periodically_call_remote($options = null)
113
    {
114
        $frequency = isset($options['frequency']) ? $options['frequency'] : 10;
115
        $code      = 'new PeriodicalExecuter(function() {' . $this->remote_function($options) . '},' . $frequency . ')';
116
117
        return $code;
118
    }
119
120
    /**
121
     * @param $options
122
     * @return string
123
     */
124
    public function remote_function($options)
125
    {
126
        $javascript_options = $this->_options_for_ajax($options);
127
128
        $update = '';
129
130
        if (isset($options['update']) && is_array($options['update'])) {
131
            $update = isset($options['update']['success']) ? 'success: ' . $options['update']['success'] : '';
132
            $update .= empty($update) ? '' : ',';
133
            $update .= isset($options['update']['failure']) ? 'failure: ' . $options['update']['failure'] : '';
134
        } else {
135
            $update .= isset($options['update']) ? $options['update'] : '';
136
        }
137
138
        $ajax_function = empty($update) ? 'new Ajax.Request(' : 'new Ajax.Updater(\'' . $update . '\',';
139
140
        $ajax_function .= "'" . $options['url'] . "'";
141
        $ajax_function .= ',' . $javascript_options . ')';
142
143
        $ajax_function = isset($options['before']) ? $options['before'] . ';' . $ajax_function : $ajax_function;
144
        $ajax_function = isset($options['after']) ? $ajax_function . ';' . $options['after'] : $ajax_function;
145
        $ajax_function = isset($options['condition']) ? 'if (' . $options['condition'] . ') {' . $ajax_function . '}' : $ajax_function;
146
        $ajax_function = isset($options['confirm']) ? 'if ( confirm(\'' . $options['confirm'] . '\' ) ) { ' . $ajax_function . ' } ' : $ajax_function;
147
148
        return $ajax_function;
149
    }
150
151
    /**
152
     * @param $name
153
     * @param $value
154
     * @param $options
155
     * @return string
156
     */
157
    public function submit_to_remote($name, $value, $options)
158
    {
159
        if (isset($options['with'])) {
160
            $options['with'] = 'Form.serialize(this.form)';
161
        }
162
163
        return '<input type="button" onclick="' . $this->remote_function($options) . '" name="' . $name . '" value ="' . $value . '">';
164
    }
165
166
    /**
167
     * @param      $element_id
168
     * @param null $options
169
     * @param      $block
170
     */
171
    public function update_element_function($element_id, $options = null, $block)
0 ignored issues
show
Unused Code introduced by
The parameter $element_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $block is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
    {
173
        $content = isset($options['content']) ? $options['content'] : '';
174
        $content = $this->escape($content);
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
175
    }
176
177
    /**
178
     * @param $block
179
     */
180
    public function update_page($block)
0 ignored issues
show
Unused Code introduced by
The parameter $block is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
181
    {
182
    }
183
184
    /**
185
     * @param $block
186
     * @return string
187
     */
188
    public function update_page_tag(& $block)
189
    {
190
        return $this->tag($block);
191
    }
192
193
    /////////////////////////////////////////////////////////////////////////////////////
194
    //                             Private functions
195
    /////////////////////////////////////////////////////////////////////////////////////
196
197
    /**
198
     * @param $options
199
     * @return array
200
     */
201
    public function _build_callbacks($options)
202
    {
203
        $callbacks = [];
204
        foreach ($options as $callback => $code) {
205
            if (in_array($callback, $this->CALLBACKS)) {
206
                $name             = 'on' . ucfirst($callback);
207
                $callbacks[$name] = 'function(request){' . $code . '}';
208
            }
209
        }
210
211
        return $callbacks;
212
    }
213
214
    /**
215
     * @param         $klass
216
     * @param         $name
217
     * @param  null   $options
218
     * @return string
219
     */
220
    public function _build_observer($klass, $name, $options = null)
221
    {
222
        if (isset($options['with']) && false === strpos($options['with'], '=')) {
223
            $options['with'] = '\'' . $options['with'] . '=\' + value';
224
        } elseif (isset($options['with']) && isset($options['update'])) {
225
            $options['with'] = 'value';
226
        }
227
228
        $callback = $options['function'] ?: $this->remote_function($options);
229
230
        $javascript = "new $klass('$name', ";
231
        $javascript .= isset($options['frequency']) ? $options['frequency'] . ', ' : '';
232
        $javascript .= 'function (element,value) { ';
233
        $javascript .= $callback;
234
        $javascript .= isset($options['on']) ? ', ' . $options['on'] : '';
235
        $javascript .= '})';
236
237
        return $javascript;
238
    }
239
240
    /**
241
     * @param $method
242
     * @return string
243
     */
244
    public function _method_option_to_s($method)
245
    {
246
        return false !== strpos($method, "'") ? $method : "'$method'";
247
    }
248
249
    /**
250
     * @param $options
251
     * @return string
252
     */
253
    public function _options_for_ajax($options)
254
    {
255
        $js_options = is_array($options) ? $this->_build_callbacks($options) : [];
256
257
        if (isset($options['type']) && 'synchronous' === $option['type']) {
0 ignored issues
show
Bug introduced by
The variable $option does not exist. Did you mean $options?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
258
            $js_options['asynchronous'] = 'false';
259
        }
260
261
        if (isset($options['method'])) {
262
            $js_options['method'] = $this->_method_option_to_s($options['method']);
263
        }
264
265
        if (isset($options['position'])) {
266
            $js_options['insertion'] = 'Insertion.' . ucfirst($options['position']);
267
        }
268
269
        $js_options['evalScripts'] = isset($options['script']) ? $options['script'] : 'true';
270
271
        if (isset($options['form'])) {
272
            $js_options['parameters'] = 'Form.serialize(this)';
273
        } elseif (isset($options['parameters'])) {
274
            $js_options['parameters'] = 'Form.serialize(\'' . $options['submit'] . '\')';
275
        } elseif (isset($options['with'])) {
276
            $js_options['parameters'] = $options['with'];
277
        }
278
279
        return $this->_options_for_javascript($js_options);
280
    }
281
282
    /////////////////////////////////////////////////////////////////////////////////////
283
    //                            Mergerd Javascript Generator helpers
284
    /////////////////////////////////////////////////////////////////////////////////////
285
286
    /**
287
     * @param $javascript
288
     */
289
    public function dump($javascript)
290
    {
291
        echo $javascript;
292
    }
293
294
    /**
295
     * @param         $id
296
     * @param  null   $extend
297
     * @return string
298
     */
299
    public function ID($id, $extend = null)
300
    {
301
        return "$('$id')" . (!empty($extend)) ? '.' . $extend . '()' : '';
302
    }
303
304
    /**
305
     * @param $message
306
     * @return string
307
     */
308
    public function alert($message)
309
    {
310
        return $this->call('alert', $message);
311
    }
312
313
    /**
314
     * @param $variable
315
     * @param $value
316
     * @return string
317
     */
318
    public function assign($variable, $value)
319
    {
320
        return "$variable = $value;";
321
    }
322
323
    /**
324
     * @param         $function
325
     * @param  null   $args
326
     * @return string
327
     */
328
    public function call($function, $args = null)
329
    {
330
        $arg_str = '';
331
        if (is_array($args)) {
332
            foreach ($args as $arg) {
333
                if (!empty($arg_str)) {
334
                    $arg_str .= ', ';
335
                }
336
                if (is_string($arg)) {
337
                    $arg_str .= "'$arg'";
338
                } else {
339
                    $arg_str .= $arg;
340
                }
341
            }
342
        } else {
343
            if (is_string($args)) {
344
                $arg_str .= "'$args'";
345
            } else {
346
                $arg_str .= $args;
347
            }
348
        }
349
350
        return "$function($arg_str)";
351
    }
352
353
    /**
354
     * @param  int    $seconds
355
     * @param  string $script
356
     * @return string
357
     */
358
    public function delay($seconds = 1, $script = '')
359
    {
360
        return "setTimeout( function() { $script } , " . ($seconds * 1000) . ' )';
361
    }
362
363
    /**
364
     * @param $id
365
     * @return string
366
     */
367
    public function hide($id)
368
    {
369
        return $this->call('Element.hide', $id);
370
    }
371
372
    /**
373
     * @param         $position
374
     * @param         $id
375
     * @param  null   $options_for_render
376
     * @return string
377
     */
378
    public function insert_html($position, $id, $options_for_render = null)
379
    {
380
        $args = array_merge([$id], (is_array($options_for_render) ? $options_for_render : [$options_for_render]));
381
382
        return $this->call('new Insertion.' . ucfirst($position), $args);
383
    }
384
385
    /**
386
     * @param $location
387
     * @return string
388
     */
389
    public function redirect_to($location)
390
    {
391
        return $this->assign('window.location.href', $location);
392
    }
393
394
    /**
395
     * @param $id
396
     * @return string
397
     */
398
    public function remove($id)
399
    {
400
        if (is_array($id)) {
401
            $arr_str = '';
0 ignored issues
show
Unused Code introduced by
$arr_str is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
402
            foreach ($id as $obj) {
403
                if (!empty($arg_str)) {
404
                    $arg_str .= ', ';
405
                }
406
                $arg_str .= "'$arg'";
0 ignored issues
show
Bug introduced by
The variable $arg_str does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $arg does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
407
            }
408
409
            return "$A[$arg_str].each(Element.remove)";
0 ignored issues
show
Bug introduced by
The variable $A does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
410
        } else {
411
            return "Element.remove('$id')";
412
        }
413
    }
414
415
    /**
416
     * @param $id
417
     * @param $options_for_render
418
     * @return string
419
     */
420
    public function replace($id, $options_for_render)
421
    {
422
        $args = array_merge([$id], (is_array($options_for_render) ? $options_for_render : [$options_for_render]));
423
424
        return $this->call('Element.replace', $args);
425
    }
426
427
    /**
428
     * @param $id
429
     * @param $options_for_render
430
     * @return string
431
     */
432
    public function replace_html($id, $options_for_render)
433
    {
434
        $args = array_merge([$id], (is_array($options_for_render) ? $options_for_render : [$options_for_render]));
435
436
        return $this->call('Element.update', $args);
437
    }
438
439
    /**
440
     * @param         $pattern
441
     * @param  null   $extend
442
     * @return string
443
     */
444
    public function select($pattern, $extend = null)
445
    {
446
        return "$$('$pattern')" . (!empty($extend)) ? '.' . $extend : '';
447
    }
448
449
    /**
450
     * @param $id
451
     * @return string
452
     */
453
    public function show($id)
454
    {
455
        return $this->call('Element.show', $id);
456
    }
457
458
    /**
459
     * @param $id
460
     * @return string
461
     */
462
    public function toggle($id)
463
    {
464
        return $this->call('Element.toggle', $id);
465
    }
466
}
467