Completed
Push — master ( dd8aad...6526e1 )
by mw
04:06
created

src/ByAskApiHttpRequest/CannedResultArray.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
152 1
			return $content;
153
		}
154
155 3
		if ( $this->mPrintRequest->getMode() === PrintRequest::PRINT_PROP &&
156 3
		     $property->findPropertyTypeId() === '_rec' ) {
157
158
			if ( $this->mPrintRequest->getParameter( 'index' ) === false ) {
159
				return $content;
160
			}
161
162
			$pos = $this->mPrintRequest->getParameter( 'index' ) - 1;
163
			$dataItems = $content->getDataItems();
164
165
			if ( !array_key_exists( $pos, $dataItems ) ) {
166
				return $content;
167
			}
168
169
			$diProperties = $content->getPropertyDataItems();
170
			$content = $dataItems[$pos];
171
172
			if ( array_key_exists( $pos, $diProperties ) &&
173
				!is_null( $diProperties[$pos] ) ) {
174
				$diProperty = $diProperties[$pos];
175
			} else {
176
				$diProperty = null;
177
			}
178
179 3
		} elseif ( $this->mPrintRequest->getMode() == PrintRequest::PRINT_PROP ) {
180 1
			$diProperty = $property;
181 1
		} else {
182 2
			$diProperty = null;
183
		}
184
185 3
		if ( $content instanceof DataValue ) {
186 1
			$content = $content->getDataItem();
187 1
		}
188
189 3
		$dataValue = DataValueFactory::getInstance()->newDataItemValue( $content, $diProperty );
190
191 3
		if ( $this->mPrintRequest->getOutputFormat() ) {
192
			$dataValue->setOutputFormat( $this->mPrintRequest->getOutputFormat() );
193
		}
194
195 3
		return $dataValue;
196
	}
197
198
	/**
199
	 * Load results of the given print request and result subject. This is only
200
	 * done when needed.
201
	 */
202 4
	protected function loadContent() {
203 4
		if ( $this->mContent !== false ) {
204 4
			return;
205
		}
206
207 4
		$this->mContent = array();
208
209 4
		switch ( $this->mPrintRequest->getMode() ) {
210 4
			case PrintRequest::PRINT_THIS:
211 1
				$this->mContent = array( $this->mResult );
212 1
			break;
213 3
			case PrintRequest::PRINT_CCAT:
214 3
			case PrintRequest::PRINT_CATS:
215
216 1
				$this->mContent = $this->jsonResponseParser->getPropertyValuesFor(
217 1
					$this->mResult,
218 1
					new DIProperty( '_INST' )
219 1
				);
220
221 1
			break;
222 2
			case PrintRequest::PRINT_PROP:
223 2
				$propertyValue = $this->mPrintRequest->getData();
224
225 2
				if ( $propertyValue->isValid() ) {
226 2
					$this->mContent = $this->jsonResponseParser->getPropertyValuesFor(
227 2
						$this->mResult,
228 2
						$propertyValue->getDataItem()
229 2
					);
230 2
				}	
231
232 2
			break;
233
			default:
234
				$this->mContent = array(); // Unknown print request.
235 4
		}
236
237 4
		reset( $this->mContent );
238 4
	}
239
240
}
241