Completed
Pull Request — master (#59)
by
unknown
05:02
created

SemanticFormsSelectInput   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 3
dl 0
loc 225
rs 9.3999
c 0
b 0
f 0
ccs 0
cts 140
cp 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getName() 0 3 1
A getParameters() 0 4 1
A getResourceModuleNames() 0 10 1
A getHtmlText() 0 8 1
F getHTML() 0 167 28
1
<?php
2
3
namespace SFS;
4
5
use SMWQueryProcessor as QueryProcessor;
6
use Parser;
7
use PFFormInput;
8
use MWDebug;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 1.3
13
 *
14
 * @author Jason Zhang
15
 * @author Toni Hermoso Pulido
16
 * @author Alexander Gesinn
17
 */
18
class SemanticFormsSelectInput extends PFFormInput {
19
20
	/**
21
	 * Internal data container
22
	 *
23
	 * @var array
24
	 */
25
	private static $data = array();
26
27
	public function __construct( $inputNumber, $curValue, $inputName, $disabled, $otherArgs ) {
28
		parent::__construct( $inputNumber, $curValue, $inputName, $disabled, $otherArgs );
29
	}
30
31
	public static function getName() {
32
		return 'SF_Select';
33
	}
34
35
	public static function getParameters() {
36
		$params = parent::getParameters();
37
		return $params;
38
	}
39
40
	public function getResourceModuleNames() {
41
		/**
42
		 * Loading modules this way currently fails with:
43
		 * "mw.loader.state({"ext.sf_select.scriptselect":"loading"});"
44
		 */
45
46
		return array(
47
			'ext.sf_select.scriptselect'
48
		);
49
	}
50
51
	/**
52
	 * Returns the HTML code to be included in the output page for this input.
53
	 * This is currently just a wrapper for getHTML().
54
	 */
55
	public function getHtmlText() {
56
		return self::getHTML(
0 ignored issues
show
Deprecated Code introduced by
The method SFS\SemanticFormsSelectInput::getHTML() has been deprecated with message: use getHtmlText() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
57
			$this->mCurrentValue,
58
			$this->mInputName,
59
			$this->mIsMandatory,
60
			$this->mIsDisabled,
61
			$this->mOtherArgs );
62
	}
63
64
	/**
65
	 * Returns the HTML code to be included in the output page for this input.
66
	 * @deprecated use getHtmlText() instead
67
	 *
68
	 * @param $cur_value
69
	 * @param $input_name
70
	 * @param $is_mandatory
71
	 * @param $is_disabled
72
	 * @param $other_args
73
	 * @return string
74
	 */
75
	public function getHTML( $cur_value, $input_name, $is_mandatory, $is_disabled, $other_args ) {
76
		global # $wgScriptSelectCount,
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
77
		$wgScriptSelectCount, $sfgFieldNum, $wgUser, $wgParser;
78
79
		$selectField = array();
80
		$values = null;
81
		$staticvalue = false;
82
		$data = array();
83
84
		if ( array_key_exists( "query", $other_args ) ) {
85
			$query = $other_args["query"];
86
			$query = str_replace( array( "~", "(", ")" ), array( "=", "[", "]" ), $query );
87
88
			$selectField["query"] = $query;
89
90
			// unparametrized query
91
			if ( strpos( $query, '@@@@' ) === false ) {
92
				$params = explode( ";", $query );
93
94
				// there is no need to run the parser, $query has been parsed already
95
				//$params[0] = $wgParser->replaceVariables( $params[0] );
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
96
97
				$values = QueryProcessor::getResultFromFunctionParams( $params, SMW_OUTPUT_WIKI );
0 ignored issues
show
Deprecated Code introduced by
The method SMWQueryProcessor::getResultFromFunctionParams() has been deprecated with message: Will vanish after release of SMW 1.8.
See SMW_Ask.php for example code on how to get query results from
#ask function parameters.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
98
99
				$staticvalue = true;
100
			}
101
102
		} elseif ( array_key_exists( "function", $other_args ) ) {
103
			$query = $other_args["function"];
104
			$query = '{{#' . $query . '}}';
105
			$query = str_replace( array( "~", "(", ")" ), array( "=", "[", "]" ), $query );
106
107
			$selectField["function"] = $query;
108
109
			// unparametrized function
110
			if ( strpos( $query, '@@@@' ) === false ) {
111
				$f = str_replace( ";", "|", $query );
112
113
				$values = $wgParser->replaceVariables( $f );
114
115
				$staticvalue = true;
116
			}
117
		}
118
119
		if ( $staticvalue ) {
120
			$values = explode( ",", $values );
121
			$values = array_map( "trim", $values );
122
			$values = array_unique( $values );
123
		} else {
124
125
			if ( $wgScriptSelectCount == 0 ) {
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...
126
				// this has been moved to getResourceModuleNames()
127
				//Output::addModule( 'ext.sf_select.scriptselect' );
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...
128
			}
129
			// $wgScriptSelectCount ++;
130
131
			$data["selectismultiple"] = array_key_exists( "part_of_multiple", $other_args );
132
133
			$index = strpos( $input_name, "[" );
134
			$data['selecttemplate'] = substr( $input_name, 0, $index );
135
136
			// Does hit work for multiple template?
137
			$index = strrpos( $input_name, "[" );
138
			$data['selectfield'] = substr( $input_name, $index + 1, strlen( $input_name ) - $index - 2 );
139
140
			$valueField = array();
0 ignored issues
show
Unused Code introduced by
$valueField 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...
141
			$data["valuetemplate"] =
142
				array_key_exists( "sametemplate", $other_args ) ? $data['selecttemplate'] : $other_args["template"];
143
			$data["valuefield"] = $other_args["field"];
144
145
			$data['selectrm'] = array_key_exists( 'rmdiv', $other_args );
146
			$data['label'] = array_key_exists( 'label', $other_args );
147
			$data['sep'] = array_key_exists( 'sep', $other_args ) ? $other_args["sep"] : ',';
148
149
			if ( array_key_exists( "query", $selectField ) ) {
150
				$data['selectquery'] = $selectField['query'];
151
			} else {
152
				$data['selectfunction'] = $selectField['function'];
153
			}
154
155
			self::$data[] = $data;
156
		}
157
158
		$extraatt = "";
159
		$is_list = false;
160
161
		// TODO This needs clean-up
162
163
		if ( array_key_exists( 'is_list', $other_args ) && $other_args['is_list'] == true ) {
164
			$is_list = true;
165
		}
166
167
		if ( $is_list ) {
168
			$extraatt = ' multiple="multiple" ';
169
		}
170
171
		if ( array_key_exists( "size", $other_args ) ) {
172
			$extraatt .= " size=\"{$other_args['size']}\"";
173
		}
174
175
		$classes = array();
176
		if ( $is_mandatory ) {
177
			$classes[] = "mandatoryField";
178
		}
179
		if ( array_key_exists( "class", $other_args ) ) {
180
			$classes[] = $other_args['class'];
181
		}
182
		if ( $classes ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $classes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
183
			$cstr = implode( " ", $classes );
184
			$extraatt .= " class=\"$cstr\"";
185
		}
186
187
		$inname = $input_name;
188
		if ( $is_list ) {
189
			$inname .= '[]';
190
		}
191
192
		// TODO Use Html::
193
194
		$spanextra = $is_mandatory ? 'mandatoryFieldSpan' : '';
195
		$ret = "<span class=\"inputSpan $spanextra\"><select name='$inname' id='input_$sfgFieldNum' $extraatt>";
196
		$curvalues = null;
0 ignored issues
show
Unused Code introduced by
$curvalues 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...
197
		if ( $cur_value ) {
198
			if ( $cur_value === 'current user' ) {
199
				$cur_value = $wgUser->getName();
200
			}
201
			if ( is_array( $cur_value ) ) {
202
				$curvalues = $cur_value;
203
			} else {
204
				$curvalues = array_map( "trim", explode( ",", $cur_value ) );
205
			}
206
207
		} else {
208
			$curvalues = array();
209
		}
210
211
		// TODO handle empty value case.
212
		$ret .= "<option></option>";
213
214
		foreach ( $curvalues as $cur ) {
215
			$ret .= "<option selected='selected'>$cur</option>";
216
		}
217
218
		if ( $staticvalue ) {
219
			foreach ( $values as $val ) {
220
				if ( !in_array( $val, $curvalues ) ) {
221
					$ret .= "<option>$val</option>";
222
				}
223
			}
224
		}
225
226
		$ret .= "</select></span>";
227
		$ret .= "<span id=\"info_$sfgFieldNum\" class=\"errorMessage\"></span>";
228
229
		if ( $other_args["is_list"] ) {
230
			$hiddenname = $input_name . '[is_list]';
231
			$ret .= "<input type='hidden' name='$hiddenname' value='1' />";
232
		}
233
234
		if ( !$staticvalue ) {
235
			$item = Output::addToHeadItem( $data );
0 ignored issues
show
Unused Code introduced by
$item 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...
236
			//$wgOut->addJsConfigVars('sf_select', array(json_encode( $data )));
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
237
		}
238
239
		Output::commitToParserOutput();
240
		return $ret;
241
	}
242
}
243