Completed
Push — master ( c3e068...3f9a02 )
by
unknown
02:14
created

CannedResultArray   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 80.73%

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 6
dl 0
loc 250
ccs 88
cts 109
cp 0.8073
rs 9.6
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getResultSubject() 0 3 1
A getContent() 0 15 4
A getPrintRequest() 0 3 1
A getNextDataItem() 0 8 2
A reset() 0 7 2
C getNextDataValue() 0 69 13
B loadContent() 0 37 7
A getMatchablePropertyFromPrintRequest() 0 23 4
1
<?php
2
3
namespace SEQL\ByHttpRequest;
4
5
use SMW\DataValueFactory;
6
use SMW\DIProperty;
7
use SMW\DIWikiPage;
8
use SMW\Localizer;
9
use SMW\Query\PrintRequest;
10
use SMWDataValue as DataValue;
11
use SMWResultArray as ResultArray;
12
13
/**
14
 * @license GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author mwjames
18
 */
19
class CannedResultArray extends ResultArray {
20
21
	/**
22
	 * @var PrintRequest
23
	 */
24
	private $mPrintRequest;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
25
26
	/**
27
	 * @var DIWikiPage
28
	 */
29
	private $mResult;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
30
31
	/**
32
	 * @var JsonResponseParser
33
	 */
34
	private $jsonResponseParser;
35
36
	/**
37
	 * @var DataValue[]|false
38
	 */
39
	private $mContent;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
40
41
	/**
42
	 * @since 1.0
43
	 *
44
	 * @param DIWikiPage $resultPage
45
	 * @param PrintRequest $printRequest
46
	 * @param JsonResponseParser $jsonResponseParser
47
	 */
48 7
	public function __construct( DIWikiPage $resultPage, PrintRequest $printRequest, JsonResponseParser $jsonResponseParser ) {
49 7
		$this->mResult = $resultPage;
50 7
		$this->mPrintRequest = $printRequest;
51 7
		$this->jsonResponseParser = $jsonResponseParser;
52 7
		$this->mContent = false;
53 7
	}
54
55
	/**
56
	 * @see ResultArray::getResultSubject
57
	 *
58
	 * @return DIWikiPage
59
	 */
60 1
	public function getResultSubject() {
61 1
		return $this->mResult;
62
	}
63
64
	/**
65
	 * @see ResultArray::getContent
66
	 *
67
	 * @return SMWDataItem[]|false
68
	 */
69 4
	public function getContent() {
70 4
		$this->loadContent();
71
72 4
		if ( !$this->mContent ) {
73
			return $this->mContent;
74
		}
75
76 4
		$content = array();
77
78 4
		foreach ( $this->mContent as $value ) {
79 4
			$content[] = $value instanceof DataValue ? $value->getDataItem() : $value;
80 4
		}
81
82 4
		return $content;
83
	}
84
85
	/**
86
	 * @see ResultArray::getPrintRequest
87
	 *
88
	 * @return PrintRequest
89
	 */
90
	public function getPrintRequest() {
91
		return $this->mPrintRequest;
92
	}
93
94
	/**
95
	 * @see ResultArray::getNextDataItem
96
	 *
97
	 * @since 1.6
98
	 *
99
	 * @return SMWDataItem|false
100
	 */
101 4
	public function getNextDataItem() {
102
103 4
		$this->loadContent();
104 4
		$result = current( $this->mContent );
105 4
		next( $this->mContent );
106
107 4
		return $result instanceof DataValue ? $result->getDataItem() : $result;
108
	}
109
110
	/**
111
	 * @see ResultArray::reset
112
	 *
113
	 * @since 1.7.1
114
	 *
115
	 * @return SMWDataItem|false
116
	 */
117 4
	public function reset() {
118
119 4
		$this->loadContent();
120 4
		$result = reset( $this->mContent );
121
122 4
		return $result instanceof DataValue ? $result->getDataItem() : $result;
123
	}
124
125
	/**
126
	 * @see ResultArray::getNextDataValue
127
	 *
128
	 * @since 1.6
129
	 *
130
	 * @return SMWDataValue|false
131
	 */
132 5
	public function getNextDataValue() {
133
134 5
		$this->loadContent();
135 5
		$content = current( $this->mContent );
136 5
		next( $this->mContent );
137
138 5
		if ( $content === false ) {
139
			return false;
140
		}
141
142 5
		$property = $this->getMatchablePropertyFromPrintRequest();
143
144
		// Units of measurement can not be assumed to be declared on a wiki
145
		// therefore don't try to recreate a DataValue and use the DV created
146
		// from the raw API response
147 5
		if ( $this->mPrintRequest->getMode() === PrintRequest::PRINT_PROP &&
148 5
		    $property->findPropertyTypeId() === '_qty' ) {
149 1
			return $content;
150
		}
151
152 4
		if ( $this->mPrintRequest->getMode() === PrintRequest::PRINT_PROP &&
153 4
		    strpos( $property->findPropertyTypeId(), '_rec' ) !== false ) {
154
155
			if ( $this->mPrintRequest->getParameter( 'index' ) === false ) {
156
				return $content;
157
			}
158
159
			$pos = $this->mPrintRequest->getParameter( 'index' ) - 1;
160
			$dataItems = $content->getDataItems();
0 ignored issues
show
Bug introduced by
The method getDataItems() does not exist on SMWDataValue. Did you maybe mean getDataItem()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
161
162
			if ( !array_key_exists( $pos, $dataItems ) ) {
163
				return $content;
164
			}
165
166
			$diProperties = $content->getPropertyDataItems();
0 ignored issues
show
Bug introduced by
The method getPropertyDataItems() does not exist on SMWDataValue. Did you maybe mean getProperty()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
167
			$content = $dataItems[$pos];
168
169
			if ( array_key_exists( $pos, $diProperties ) &&
170
				!is_null( $diProperties[$pos] ) ) {
171
				$diProperty = $diProperties[$pos];
172
			} else {
173
				$diProperty = null;
174
			}
175
176 4
		} elseif ( $this->mPrintRequest->getMode() == PrintRequest::PRINT_PROP ) {
177 1
			$diProperty = $property;
178 1
		} else {
179 3
			$diProperty = null;
180
		}
181
182 4
		if ( $content instanceof DataValue ) {
183 1
			$content = $content->getDataItem();
184 1
		}
185
186 4
		$dataValue = DataValueFactory::getInstance()->newDataItemValue(
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DataValueFactory::newDataItemValue() has been deprecated with message: since 2.4, use DataValueFactory::newDataValueByItem

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...
187 4
			$content,
188
			$diProperty
189 4
		);
190
191 4
		$dataValue->setContextPage(
192 4
			$this->mResult
0 ignored issues
show
Documentation introduced by
$this->mResult is of type object<SMW\DIWikiPage>, but the function expects a null|object<SMWDIWikiPage>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
193 4
		);
194
195 4
		if ( $this->mPrintRequest->getOutputFormat() ) {
196
			$dataValue->setOutputFormat( $this->mPrintRequest->getOutputFormat() );
197
		}
198
199 4
		return $dataValue;
200
	}
201
202
	/**
203
	 * Load results of the given print request and result subject. This is only
204
	 * done when needed.
205
	 */
206 5
	protected function loadContent() {
207 5
		if ( $this->mContent !== false ) {
208 4
			return;
209
		}
210
211 5
		$this->mContent = array();
212
213 5
		switch ( $this->mPrintRequest->getMode() ) {
214 5
			case PrintRequest::PRINT_THIS:
215 2
				$this->mContent = array( $this->mResult );
0 ignored issues
show
Documentation Bug introduced by
It seems like array($this->mResult) of type array<integer,object<SMW...ect<SMW\\DIWikiPage>"}> is incompatible with the declared type array<integer,object<SMWDataValue>>|false of property $mContent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
216 2
			break;
217 3
			case PrintRequest::PRINT_CCAT:
218 3
			case PrintRequest::PRINT_CATS:
219
220 1
				$this->mContent = $this->jsonResponseParser->getPropertyValuesFor(
221 1
					$this->mResult,
222 1
					new DIProperty( '_INST' )
223 1
				);
224
225 1
			break;
226 2
			case PrintRequest::PRINT_PROP:
227 2
				$propertyValue = $this->mPrintRequest->getData();
228
229 2
				if ( $propertyValue->isValid() ) {
230 2
					$this->mContent = $this->jsonResponseParser->getPropertyValuesFor(
231 2
						$this->mResult,
232 2
						$this->getMatchablePropertyFromPrintRequest()
0 ignored issues
show
Bug introduced by
It seems like $this->getMatchablePropertyFromPrintRequest() can be null; however, getPropertyValuesFor() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
233 2
					);
234 2
				}
235
236 2
			break;
237
			default:
238
				$this->mContent = array(); // Unknown print request.
239 5
		}
240
241 5
		reset( $this->mContent );
242 5
	}
243
244 5
	private function getMatchablePropertyFromPrintRequest() {
245
246 5
		if ( $this->mPrintRequest->getMode() !== PrintRequest::PRINT_PROP ) {
247 3
			return null;
248
		}
249
250 2
		$property = $this->mPrintRequest->getData()->getDataItem();
251
252
		// The API may not deploy the natural property key (until 0.8+) hence something
253
		// like |?Has population=Population (in K) does not return a valid result from
254
		// the parser because "Has population" cannot be connected to "Population (in K)"
255
		// without the extra "key" field therefore construct a new property to match the
256
		// label
257 2
		if ( $this->mPrintRequest->getLabel() !== '' && $this->mPrintRequest->getLabel() !== $property->getLabel() ) {
258 1
			return $this->jsonResponseParser->findPropertyFromInMemoryExternalRepositoryCache(
259 1
				DIProperty::newFromUserLabel( $this->mPrintRequest->getLabel() )
260 1
			);
261
		}
262
263 1
		return $this->jsonResponseParser->findPropertyFromInMemoryExternalRepositoryCache(
264
			$property
265 1
		);
266
	}
267
268
}
269