Completed
Pull Request — master (#71)
by Toni Hermoso
22:26
created

SelectField   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 1
dl 0
loc 172
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A processParameters() 0 7 3
A getData() 0 3 1
A setQuery() 0 20 2
A setFunction() 0 20 2
A setSelectIsMultiple() 0 4 1
A setSelectTemplate() 0 5 1
A setSelectField() 0 5 1
A setValueTemplate() 0 5 2
A setValueField() 0 5 1
A setSelectRemove() 0 4 1
A setLabel() 0 4 1
A setDelimiter() 0 15 3
A getDelimiter() 0 3 1
A getValues() 0 3 1
A setValues() 0 6 1
A hasStaticValues() 0 3 1
A setHasStaticValues() 0 3 1
1
<?php
2
3
/**
4
 * Represents a Select Field.
5
 * @license GNU GPL v2+
6
 * @since 3.0.0
7
 * @author: Alexander Gesinn
8
 */
9
10
namespace SFS;
11
12
use SMWQueryProcessor as QueryProcessor;
13
use Parser;
14
use MWDebug;
15
16
class SelectField {
17
18
	private $mParser = null;
19
20
	//private $mSelectField = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
21
	private $mValues = null;
22
	private $mHasStaticValues = false;
23
24
	private $mData = [];    # array with all parameters
25
	private $mQuery = "";
26
	private $mFunction = "";
27
	private $mSelectIsMultiple = false;
28
	private $mSelectTemplate = "";
29
	private $mSelectField = "";
30
	private $mValueTemplate = "";
31
	private $mValueField = "";
32
	private $mSelectRemove = false;
33
	private $mLabel = false;
34
	private $mDelimiter = ",";
35
36
	public function __construct( & $parser ) {
37
		$this->mParser = $parser;
38
	}
39
40
	/**
41
	 * Convenience function to process all parameters at once
42
	 */
43
	public function processParameters( $input_name = "", $other_args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $input_name 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...
44
		if ( array_key_exists( "query", $other_args ) ) {
45
			$this->setQuery( $other_args );
46
		} elseif ( array_key_exists( "function", $other_args ) ) {
47
			$this->setFunction( $other_args );
48
		}
49
	}
50
51
	/**
52
	 * getData
53
	 *
54
	 * @return array Array with all parameters
55
	 */
56
	public function getData() {
57
		return $this->mData;
58
	}
59
60
	public function setQuery( $other_args ) {
61
		$query = $other_args["query"];
62
		$query = str_replace( [ "~", "(", ")" ], [ "=", "[", "]" ], $query );
63
64
		//$this->mSelectField["query"] = $query;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
65
		$this->mQuery = $query;
66
		$this->mData['selectquery'] = $query;
67
68
		// unparametrized query
69
		if ( strpos( $query, '@@@@' ) === false ) {
70
			$params = explode( ";", $query );
71
72
			// there is no need to run the parser, $query has been parsed already
73
			//$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...
74
75
			$this->mValues = 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...
76
77
			$this->setHasStaticValues( true );
78
		}
79
	}
80
81
	public function setFunction( $other_args ) {
82
		#global $wgParser;
83
84
		$function = $other_args["function"];
85
		$function = '{{#' . $function . '}}';
86
		$function = str_replace( [ "~", "(", ")" ], [ "=", "[", "]" ], $function );
87
88
		//$this->mSelectField["function"] = $function;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
89
		$this->mFunction = $function;
90
		$this->mData['selectfunction'] = $function;
91
92
		// unparametrized function
93
		if ( strpos( $function, '@@@@' ) === false ) {
94
			$f = str_replace( ";", "|", $function );
95
96
			$this->setValues( $this->mParser->replaceVariables( $f ) );
97
98
			$this->setHasStaticValues( true );
99
		}
100
	}
101
102
	public function setSelectIsMultiple( Array $other_args ) {
103
		$this->mSelectIsMultiple = array_key_exists( "part_of_multiple", $other_args );
104
		$this->mData["selectismultiple"] = $this->mSelectIsMultiple;
105
	}
106
107
	public function setSelectTemplate( $input_name = "" ) {
108
		$index = strpos( $input_name, "[" );
109
		$this->mSelectTemplate = substr( $input_name, 0, $index );
110
		$this->mData['selecttemplate'] = $this->mSelectTemplate;
111
	}
112
113
	public function setSelectField( $input_name = "" ) {
114
		$index = strrpos( $input_name, "[" );
115
		$this->mSelectField = substr( $input_name, $index + 1, strlen( $input_name ) - $index - 2 );
116
		$this->mData['selectfield'] = $this->mSelectField;
117
	}
118
119
	public function setValueTemplate( Array $other_args ) {
120
		$this->mValueTemplate =
121
			array_key_exists( "sametemplate", $other_args ) ? $this->mSelectTemplate : $other_args["template"];
122
		$this->mData["valuetemplate"] = $this->mValueTemplate;
123
	}
124
125
	public function setValueField( Array $other_args ) {
126
		$this->mValueField = $other_args["field"];
127
		$this->mData["valuefield"] = $this->mValueField;
128
129
	}
130
131
	public function setSelectRemove( Array $other_args ) {
132
		$this->mSelectRemove = array_key_exists( 'rmdiv', $other_args );
133
		$this->mData['selectrm'] = $this->mSelectRemove;
134
	}
135
136
	public function setLabel( Array $other_args ) {
137
		$this->mLabel = array_key_exists( 'label', $other_args );
138
		$this->mData['label'] = $this->mLabel;
139
	}
140
141
	/**
142
	 * setDelimiter
143
	 * @param array $other_args
144
	 */
145
	public function setDelimiter( Array $other_args ) {
0 ignored issues
show
Coding Style introduced by
setDelimiter uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
146
		
147
		$this->mDelimiter = $GLOBALS['wgPageFormsListSeparator'];
148
		
149
		if ( array_key_exists( 'sep', $other_args ) ) {
150
			$this->mDelimiter = $other_args['sep'];
151
		} else {
152
			// Adding Backcompatibility
153
			if ( array_key_exists( 'delimiter', $other_args ) ) {
154
				$this->mDelimiter = $other_args['delimiter'];
155
			}
156
		}
157
158
		$this->mData['sep'] = $this->mDelimiter;
159
	}
160
161
	public function getDelimiter() {
162
		return $this->mDelimiter;
163
	}
164
165
	public function getValues() {
166
		return $this->mValues;
167
	}
168
169
	/**
170
	 * setValues
171
	 * @param string $values (comma separated, fully parsed list of values)
172
	 */
173
	private function setValues( $values ) {
174
		$values = explode( $this->mDelimiter, $values );
175
		$values = array_map( "trim", $values );
176
		$values = array_unique( $values );
177
		$this->mValues = $values;
178
	}
179
180
	public function hasStaticValues() {
181
		return $this->mHasStaticValues;
182
	}
183
184
	private function setHasStaticValues( $StaticValues ) {
185
		$this->mHasStaticValues = $StaticValues;
186
	}
187
}
188