Completed
Push — master ( 453150...86c4c5 )
by mw
03:14
created

CannedResultArray::getNextDataValue()   C

Complexity

Conditions 13
Paths 20

Size

Total Lines 77
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 19.2573

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 77
ccs 32
cts 48
cp 0.6667
rs 5.2202
cc 13
eloc 43
nc 20
nop 0
crap 19.2573

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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( $content, $diProperty );
187
188
		// Allow the DV formatter to access a specific language code
189 4
		$dataValue->setOption(
190 4
			'content.language',
191 4
			Localizer::getInstance()->getPreferredContentLanguage( $this->mResult )->getCode()
192 4
		);
193
194 4
		$dataValue->setOption(
195 4
			'user.language',
196 4
			Localizer::getInstance()->getUserLanguage()->getCode()
197 4
		);
198
199 4
		$dataValue->setContextPage(
200 4
			$this->mResult
0 ignored issues
show
Compatibility introduced by
$this->mResult of type object<SMW\DIWikiPage> is not a sub-type of object<SMWDIWikiPage>. It seems like you assume a child class of the class SMW\DIWikiPage to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
201 4
		);
202
203 4
		if ( $this->mPrintRequest->getOutputFormat() ) {
204
			$dataValue->setOutputFormat( $this->mPrintRequest->getOutputFormat() );
205
		}
206
207 4
		return $dataValue;
208
	}
209
210
	/**
211
	 * Load results of the given print request and result subject. This is only
212
	 * done when needed.
213
	 */
214 5
	protected function loadContent() {
215 5
		if ( $this->mContent !== false ) {
216 4
			return;
217
		}
218
219 5
		$this->mContent = array();
220
221 5
		switch ( $this->mPrintRequest->getMode() ) {
222 5
			case PrintRequest::PRINT_THIS:
223 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...
224 2
			break;
225 3
			case PrintRequest::PRINT_CCAT:
226 3
			case PrintRequest::PRINT_CATS:
227
228 1
				$this->mContent = $this->jsonResponseParser->getPropertyValuesFor(
229 1
					$this->mResult,
230 1
					new DIProperty( '_INST' )
231 1
				);
232
233 1
			break;
234 2
			case PrintRequest::PRINT_PROP:
235 2
				$propertyValue = $this->mPrintRequest->getData();
236
237 2
				if ( $propertyValue->isValid() ) {
238 2
					$this->mContent = $this->jsonResponseParser->getPropertyValuesFor(
239 2
						$this->mResult,
240 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...
241 2
					);
242 2
				}
243
244 2
			break;
245
			default:
246
				$this->mContent = array(); // Unknown print request.
247 5
		}
248
249 5
		reset( $this->mContent );
250 5
	}
251
252 5
	private function getMatchablePropertyFromPrintRequest() {
253
254 5
		if ( $this->mPrintRequest->getMode() !== PrintRequest::PRINT_PROP ) {
255 3
			return null;
256
		}
257
258 2
		$property = $this->mPrintRequest->getData()->getDataItem();
259
260
		// The API may not deploy the natural property key (until 0.8+) hence something
261
		// like |?Has population=Population (in K) does not return a valid result from
262
		// the parser because "Has population" cannot be connected to "Population (in K)"
263
		// without the extra "key" field therefore construct a new property to match the
264
		// label
265 2
		if ( $this->mPrintRequest->getLabel() !== '' && $this->mPrintRequest->getLabel() !== $property->getLabel() ) {
266 1
			return $this->jsonResponseParser->findPropertyFromInMemoryExternalRepositoryCache(
267 1
				DIProperty::newFromUserLabel( $this->mPrintRequest->getLabel() )
268 1
			);
269
		}
270
271 1
		return $this->jsonResponseParser->findPropertyFromInMemoryExternalRepositoryCache(
272
			$property
273 1
		);
274
	}
275
276
}
277