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

doProcessFunctionFor()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
ccs 0
cts 19
cp 0
cc 2
eloc 14
nc 2
nop 2
crap 6
1
<?php
2
3
namespace SFS;
4
5
use Parser;
6
use SMWQueryProcessor as QueryProcessor;
7
use InvalidArgumentException;
8
9
/**
10
 * @license GNU GPL v2+
11
 * @since 1.3
12
 *
13
 * @author Jason Zhang
14
 * @author Toni Hermoso Pulido
15
 * @author mwjames
16
 */
17
18
class ApiSemanticFormsSelectRequestProcessor {
19
20
	/**
21
	 * @var Parser
22
	 */
23
	private $parser;
24
25
	/**
26
	 * @var boolean
27
	 */
28
	private $debugFlag = false;
29
30
	/**
31
	 * @since 1.3
32
	 *
33
	 * @param Parser $parser
34
	 */
35
	public function __construct( Parser $parser ) {
36
		$this->parser = $parser;
37
	}
38
39
	/**
40
	 * @since 1.3
41
	 *
42
	 * @param boolean $debugFlag
43
	 */
44
	public function setDebugFlag( $debugFlag ) {
45
		$this->debugFlag = $debugFlag;
46
	}
47
48
	/**
49
	 * @since 1.3
50
	 *
51
	 * @param array $parameters
52
	 *
53
	 * @return string
54
	 */
55
	public function getJsonDecodedResultValuesForRequestParameters( array $parameters ) {
56
57
		if ( !isset( $parameters['query'] ) || !isset( $parameters['sep'] ) ) {
58
			throw new InvalidArgumentException( 'Missing an query parameter' );
59
		}
60
61
		$this->parser->firstCallInit();
62
		$json = array();
0 ignored issues
show
Unused Code introduced by
$json 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...
63
64
		if ( isset( $parameters['approach'] ) && $parameters['approach'] == 'smw' ) {
65
			$json = $this->doProcessQueryFor( $parameters['query'], $parameters['sep'] );
66
		} else {
67
			$json = $this->doProcessFunctionFor( $parameters['query'], $parameters['sep'] );
68
		}
69
70
		// I have no idea why we first encode and and then decode here??
71
72
		return json_decode( $json );
73
	}
74
75
	private function doProcessQueryFor( $query, $sep = "," ) {
76
77
		$query = str_replace(
78
			array( "&lt;", "&gt;", "sep=;" ),
79
			array( "<", ">", "sep={$sep};" ),
80
			$query
81
		);
82
83
		$params = explode( ";", $query );
84
		$f = str_replace( ";", "|", $params[0] );
85
86
		$params[0] = $this->parser->replaceVariables( $f );
87
88
		if ( $this->debugFlag ) {
89
			error_log( implode( "|", $params ) );
90
		}
91
92
		$values = $this->getFormattedValuesFrom(
93
			$sep,
94
			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...
95
		);
96
97
		return json_encode( array(
98
			"values" => $values,
99
			"count"  => count( $values )
100
		) );
101
	}
102
103
	private function doProcessFunctionFor( $query, $sep = "," ) {
104
105
		$query = str_replace(
106
			array( "&lt;", "&gt;", "sep=;" ),
107
			array( "<", ">", "sep={$sep};" ),
108
			$query
109
		);
110
111
		$f = str_replace( ";", "|", $query );
112
113
		if ( $this->debugFlag ) {
114
			error_log( $f );
115
		}
116
117
		$values = $this->getFormattedValuesFrom(
118
			$sep,
119
			$this->parser->replaceVariables( $f )
120
		);
121
122
		return json_encode( array(
123
			"values" => $values,
124
			"count"  => count( $values )
125
		) );
126
	}
127
128
	private function getFormattedValuesFrom( $sep, $values ) {
129
130
		if ( strpos( $values, $sep ) === false ) {
131
			return array( $values );
132
		}
133
134
		$values = explode( $sep, $values );
135
		$values = array_map( "trim", $values );
136
		$values = array_unique( $values );
137
138
		sort( $values );
139
		array_unshift( $values, "" );
140
141
		return $values;
142
	}
143
144
}
145