JavaScript::link_to_function()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 3
dl 4
loc 4
rs 10
c 0
b 0
f 0
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 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
    /**
19
     * @param         $name
20
     * @param  null   $function
21
     * @return string
22
     */
23
    public function button_to_function($name, $function = null)
24
    {
25
        return '<input type="button" value="' . $name . '" onclick="' . $function . '">';
26
    }
27
28
    /**
29
     * @param $javascript
30
     * @return mixed|string
31
     */
32
    public function escape($javascript)
33
    {
34
        $javascript = addslashes($javascript);
35
        $javascript = str_replace(["\r\n", "\n", "\r"], ["\n"], $javascript);
36
37
        return $javascript;
38
    }
39
40
    /**
41
     * @param $content
42
     * @return string
43
     */
44
    public function tag($content)
45
    {
46
        return '<script type="text/javascript">' . $content . '</script>';
47
    }
48
49
    /**
50
     * @param         $name
51
     * @param         $function
52
     * @param  null   $html_options
53
     * @return string
54
     */
55
    public function link_to_function($name, $function, $html_options = null)
56
    {
57
        return '<a href="' . (isset($html_options['href']) ? $html_options['href'] : '#') . '" onclick="' . (isset($html_options['onclick']) ? $html_options['onclick'] . ';' : '') . $function . '; return false;">' . $name . '</a>';
58
    }
59
60
    /////////////////////////////////////////////////////////////////////////////////////
61
    //                             Private functions
62
    /////////////////////////////////////////////////////////////////////////////////////
63
64
    /**
65
     * @param $option
66
     * @return string
67
     */
68
    public function _array_or_string_for_javascript($option)
69
    {
70
        $return_val = '';
71
        if (is_array($option)) {
72
            foreach ($option as $value) {
73
                if (!empty($return_val)) {
74
                    $ret_val .= ', ';
0 ignored issues
show
Bug introduced by
The variable $ret_val 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...
75
                }
76
                $return_val .= $value;
77
            }
78
79
            return '[' . $return_val . ']';
80
        }
81
82
        return "'$option'";
83
    }
84
85
    /**
86
     * @param $options
87
     * @return string
88
     */
89
    public function _options_for_javascript($options)
90
    {
91
        $return_val = '';
92
93
        if (is_array($options)) {
94
            foreach ($options as $var => $val) {
95
                if (!empty($return_val)) {
96
                    $return_val .= ', ';
97
                }
98
                $return_val .= "$var:$val";
99
            }
100
        }
101
102
        return '{' . $return_val . '}';
103
    }
104
}
105