Intraface_modules_cms_HTML_Editor::get()   F
last analyzed

Complexity

Conditions 29
Paths > 20000

Size

Total Lines 141
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 870

Importance

Changes 0
Metric Value
cc 29
eloc 77
nc 276542
nop 3
dl 0
loc 141
rs 2
c 0
b 0
f 0
ccs 0
cts 99
cp 0
crap 870

How to fix   Long Method    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
 * Remember to include the javascript as well
4
 *
5
 * @package Intraface_CMS
6
 */
7
class Intraface_modules_cms_HTML_Editor
8
{
9
    public $allowed_tags;
10
    public $implemented_editors = array(
11
        'none', 'tinymce', 'wiki'
12
    );
13
    public $editor;
14
    public $options;
15
16
    /**
17
     * Constructor
18
     */
19
    function __construct($allowed_tags = '')
20
    {
21
        $this->allowed_tags = $allowed_tags;
22
    }
23
24
    function setEditor($editor)
25
    {
26
        if (!in_array($editor, $this->implemented_editors)) {
27
            throw new Exception($editor . 'editor not implemented');
28
        }
29
30
        $this->editor = $editor;
31
    }
32
33
    function get($textarea_attributes, $initial_value = '', $editor_attributes = array())
34
    {
35
        $output = '';
0 ignored issues
show
Unused Code introduced by
$output 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...
36
        switch ($this->editor) {
37
            case 'tinymce':
38
                // return tinymce textarea
39
40
                $blockformat = array();
41
                $button = array();
42
43
44
                if (!empty($editor_attributes['plugins']) and is_array($editor_attributes['plugins'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
45
                    if (in_array('save', $editor_attributes['plugins'])) {
46
                        $button[] = 'save';
47
                    }
48
                }
49
50
                $button[] = 'undo';
51
                $button[] = 'redo';
52
53
                if (!empty($this->allowed_tags) and is_array($this->allowed_tags)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
54
                    if (in_array('p', $this->allowed_tags)) {
55
                        $blockformat[] = 'p';
56
                    }
57
                    if (in_array('h1', $this->allowed_tags)) {
58
                        $blockformat[] = 'h1';
59
                    }
60
61
                    if (in_array('h2', $this->allowed_tags)) {
62
                        $blockformat[] = 'h2';
63
                    }
64
65
                    if (in_array('h3', $this->allowed_tags)) {
66
                        $blockformat[] = 'h3';
67
                    }
68
69
                    if (in_array('h4', $this->allowed_tags)) {
70
                        $blockformat[] = 'h4';
71
                    }
72
73
                    if (in_array('blockquote', $this->allowed_tags)) {
74
                        $blockformat[] = 'blockquote';
75
                    }
76
77
                    if (in_array('strong', $this->allowed_tags)) {
78
                        $button[] = 'bold';
79
                    } elseif (in_array('b', $this->allowed_tags)) {
80
                        $button[] = 'bold';
81
                    }
82
83
                    if (in_array('em', $this->allowed_tags)) {
84
                        $button[] = 'italic';
85
                    } elseif (in_array('i', $this->allowed_tags)) {
86
                        $button[] = 'italic';
87
                    }
88
89
                    if (in_array('a', $this->allowed_tags)) {
90
                        $button[]= 'link';
91
                        $button[]= 'unlink';
92
                    }
93
94
                    if (in_array('ul', $this->allowed_tags)) {
95
                        $button[] = 'bullist';
96
                    }
97
                    if (in_array('ol', $this->allowed_tags)) {
98
                        $button[] = 'numlist';
99
                    }
100
                }
101
102
                if (!empty($blockformat)) {
103
                    $button[] = 'formatselect';
104
                }
105
106
107
108
                // link, unlink - hvis link er tilladte
109
                // bold, italic - hvis de er tilladte
110
                // formatselect - if blockformats !empty
111
                // bullist, numlist - if lists are availabel
112
                // spellchecker - if it is turned on
113
114
                // original list of buttons1: , bold, italic, formatselect, separator, bullist,numlist,separator,undo,redo,separator,link,unlink,separator,sub,sup,separator, tablecontrols, separator,charmap,separator,cleanup,code,spellchecker,separator,help,pasteword
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
115
116
                if (!empty($editor_attributes['plugins']) and is_array($editor_attributes['plugins'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
117
                    if (in_array('spellchecker', $editor_attributes['plugins'])) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
118
                        // $button[] = 'spellchecker';
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
119
                    }
120
121
                    if (in_array('table', $editor_attributes['plugins']) and in_array('table', $this->allowed_tags)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
122
                        $button[] = 'tablecontrols';
123
                    }
124
                }
125
126
                $button[] = 'code';
127
                $button[] = 'pasteword';
128
129
                $output = '<textarea'.$this->_parseTextareaAttributes($textarea_attributes).'>'.htmlentities(utf8_decode($initial_value)).'</textarea>'."\n";
130
                $output .= '<script language="javascript" type="text/javascript">'."\n";
131
                $output .= 'tinyMCE.init({'."\n";
132
                $output .= '    mode : "exact",'."\n";
133
                $output .= '    elements : "'.$textarea_attributes['id'].'",'."\n";
134
                $output .= '    theme : "advanced",'."\n";
135
                if (!empty($editor_attributes['plugins']) and is_array($editor_attributes['plugins'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
136
                    $output .= '    plugins : "'.implode($editor_attributes['plugins'], ',').'",'."\n";
137
                }
138
                $output .= '    theme_advanced_buttons1 : "'.implode($button, ',').'",'."\n";
139
                $output .= '    theme_advanced_buttons2 : "",'."\n";
140
                $output .= '    theme_advanced_buttons3 : "",'."\n";
141
                $output .= '    theme_advanced_blockformats : "'.implode($blockformat, ',').'",'."\n";
142
                $output .= '    theme_advanced_toolbar_location : "top",'."\n";
143
                $output .= '    theme_advanced_toolbar_align : "left",'."\n";
144
                $output .= '    cleanup : true,'."\n";
145
                $output .= '    clean_on_startup : true,'."\n";
146
                $output .= '    verify_html : true,'."\n";
147
                $output .= '    apply_source_formatting : true,'."\n";
148
                $output .= '    relative_urls : false,'."\n";
149
                $output .= '    convert_urls : false,'."\n";
150
                $output .= '    entity_encoding : "raw",'."\n";
151
                $output .= '    remove_linebreaks : true'."\n";
152
                //$output .= '  spellchecker_languages : "+Danish=da, English=en"';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
153
154
                $output .= '});'."\n";
155
                $output .= '</script>'."\n";
156
157
                break;
158
                /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
159
            case 'widgeditor':
160
                if (!empty($texarea_attributes['class'])) $texarea_attributes['class'] .= ' widgeditor';
161
                else $texarea_attributes['class'] .= 'widgeditor';
162
                $output = '<textarea'.$this->_parseTextareaAttributes($textarea_attributes).'>'.htmlentities($initial_value).'</textarea>';
163
            break;
164
            */
165
            case 'wiki':
166
                // fall through
167
            default:
168
                    // return ordinary textarea
169
                    $output = '<textarea'.$this->_parseTextareaAttributes($textarea_attributes).'>'.htmlspecialchars($initial_value).'</textarea>';
170
                break;
171
        }
172
        return $output;
173
    }
174
175
    function _parseTextareaAttributes($textarea_attributes)
176
    {
177
        $output = '';
178
        if (array_key_exists('id', $textarea_attributes)) {
179
            $output .= ' id="'.$textarea_attributes['id'].'"';
180
        }
181
        if (array_key_exists('name', $textarea_attributes)) {
182
            $output .= ' name="'.$textarea_attributes['name'].'"';
183
        }
184
        if (array_key_exists('cols', $textarea_attributes)) {
185
            $output .= ' cols="'.$textarea_attributes['cols'].'"';
186
        }
187
        if (array_key_exists('rows', $textarea_attributes)) {
188
            $output .= ' rows="'.$textarea_attributes['rows'].'"';
189
        }
190
        if (array_key_exists('class', $textarea_attributes)) {
191
            $output .= ' class="'.$textarea_attributes['class'].'"';
192
        }
193
        return $output;
194
    }
195
}
196