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

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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