Completed
Push — master ( 9cb597...d2d2ad )
by mw
13s
created

src/Exporter/ConceptToExpDataMapper.php (36 issues)

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 SMW\Exporter;
4
5
use SMW\ApplicationFactory;
6
use SMW\DataValueFactory;
7
use SMW\DIConcept;
8
use SMW\DIProperty;
9
use SMW\Exporter\Element\ExpResource;
10
use SMW\Query\Language\ClassDescription;
11
use SMW\Query\Language\ConceptDescription;
12
use SMW\Query\Language\Conjunction;
13
use SMW\Query\Language\Description;
14
use SMW\Query\Language\Disjunction;
15
use SMW\Query\Language\SomeProperty;
16
use SMW\Query\Language\ThingDescription;
17
use SMW\Query\Language\ValueDescription;
18
use SMWDataItem as DataItem;
19
use SMWExpData as ExpData;
20
use SMWExporter as Exporter;
21
22
/**
23
 * @license GNU GPL v2+
24
 * @since 2.4
25
 *
26
 * @author Markus Krötzsch
27
 * @author mwjames
28
 */
29
class ConceptToExpDataMapper {
30
31
	/**
32
	 * @var Exporter
33
	 */
34
	private $exporter;
35
36
	/**
37
	 * @since 2.4
38
	 *
39
	 * @param Exporter|null $exporter
40
	 */
41 18
	public function __construct( Exporter $exporter = null ) {
42 18
		$this->exporter = $exporter;
43
44 18
		if ( $this->exporter === null ) {
45 18
			$this->exporter = Exporter::getInstance();
46
		}
47 18
	}
48
49
	/**
50
	 * @since 2.4
51
	 *
52
	 * @param DataItem $dataItem
53
	 *
54
	 * @return boolean
55
	 */
56 14
	public function isMapperFor( DataItem $dataItem ) {
57 14
		return $dataItem instanceof DIConcept;
58
	}
59
60
	/**
61
	 * @since 2.4
62
	 *
63
	 * @param DIConcept $concept
64
	 *
65
	 * @return ExpData|null
66
	 */
67
	public function getElementFor( DIConcept $concept ) {
68
69
		$dataValue = DataValueFactory::getInstance()->newDataValueByItem(
70
			$concept
71
		);
72
73
		if ( !$dataValue->isValid() ) {
74
			return null;
75
		}
76
77
		$description = ApplicationFactory::getInstance()->newQueryParser()->getQueryDescription(
0 ignored issues
show
Deprecated Code introduced by
The method SMW\ApplicationFactory::newQueryParser() has been deprecated with message: since 2.5, use QueryFactory::newQueryParser

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...
78
			$dataValue->getWikiValue()
79
		);
80
81
		$exact = true;
82
		$owlDescription = $this->getExpDataFromDescription( $description, $exact );
83
84
		if ( $owlDescription === false ) {
85
			$result = new ExpData(
86
				$this->exporter->getSpecialNsResource( 'owl', 'Thing' )
0 ignored issues
show
$this->exporter->getSpec...esource('owl', 'Thing') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpResource>.

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...
87
			);
88
89
			return $result;
90
		}
91
92
		if ( $exact ) {
93
			return $owlDescription;
94
		}
95
96
		$result = new ExpData(
97
			new ExpResource( '' )
0 ignored issues
show
new \SMW\Exporter\Element\ExpResource('') is of type object<SMW\Exporter\Element\ExpResource>, but the function expects a object<SMWExpResource>.

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...
98
		);
99
100
		$result->addPropertyObjectValue(
101
			$this->exporter->getSpecialNsResource( 'rdf', 'type' ),
0 ignored issues
show
$this->exporter->getSpec...Resource('rdf', 'type') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpNsResource>.

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...
102
			new ExpData( $this->exporter->getSpecialNsResource( 'owl', 'Class' ) )
0 ignored issues
show
$this->exporter->getSpec...esource('owl', 'Class') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpResource>.

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...
103
		);
104
105
		$result->addPropertyObjectValue(
106
			$this->exporter->getSpecialNsResource( 'rdfs', 'subClassOf' ),
0 ignored issues
show
$this->exporter->getSpec...e('rdfs', 'subClassOf') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpNsResource>.

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...
107
			$owlDescription
0 ignored issues
show
It seems like $owlDescription defined by $this->getExpDataFromDes...n($description, $exact) on line 82 can be null; however, SMWExpData::addPropertyObjectValue() 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...
108
		);
109
110
		return $result;
111
	}
112
113
	/**
114
	 * @since 2.4
115
	 *
116
	 * @param Description $description
117
	 *
118
	 * @param string &$exact
119
	 *
120
	 * @return Element|false
121
	 */
122 3
	public function getExpDataFromDescription( Description $description, &$exact ) {
123
124 3
		if ( ( $description instanceof Conjunction ) || ( $description instanceof Disjunction ) ) {
125
			$result = $this->doMapConjunctionDisjunction( $description, $exact );
126
		} elseif ( $description instanceof ClassDescription ) {
127 2
			$result = $this->doMapClassDescription( $description, $exact );
128
		} elseif ( $description instanceof ConceptDescription ) {
129
			$result = $this->doMapConceptDescription( $description, $exact );
130
		} elseif ( $description instanceof SomeProperty ) {
131
			$result = $this->doMapSomeProperty( $description, $exact );
132
		} elseif ( $description instanceof ValueDescription ) {
133
			$result = $this->doMapValueDescription( $description, $exact );
134
		} elseif ( $description instanceof ThingDescription ) {
135 1
			$result = false;
136
		} else {
137
			$result = false;
138
			$exact = false;
139
		}
140
141 3
		return $result;
142
	}
143
144
	private function doMapValueDescription( ValueDescription $description, &$exact ) {
145
146
		if ( $description->getComparator() === SMW_CMP_EQ ) {
147
			$result = $this->exporter->getDataItemExpElement( $description->getDataItem() );
148
		} else {
149
			// OWL cannot represent <= and >= ...
150
			$exact = false;
151
			$result = false;
152
		}
153
154
		return $result;
155
	}
156
157
	private function doMapConceptDescription( ConceptDescription $description, &$exact ) {
0 ignored issues
show
The parameter $exact is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
158
159
		$result = new ExpData(
160
			$this->exporter->getResourceElementForWikiPage( $description->getConcept() )
0 ignored issues
show
$description->getConcept() 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...
$this->exporter->getReso...cription->getConcept()) is of type object<SMW\Exporter\Element\ExpResource>, but the function expects a object<SMWExpResource>.

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...
161
		);
162
163
		return $result;
164
	}
165
166
	private function doMapSomeProperty( SomeProperty $description, &$exact ) {
167
168
		$result = new ExpData(
169
			new ExpResource( '' )
0 ignored issues
show
new \SMW\Exporter\Element\ExpResource('') is of type object<SMW\Exporter\Element\ExpResource>, but the function expects a object<SMWExpResource>.

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...
170
		);
171
172
		$result->addPropertyObjectValue(
173
			$this->exporter->getSpecialNsResource( 'rdf', 'type' ),
0 ignored issues
show
$this->exporter->getSpec...Resource('rdf', 'type') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpNsResource>.

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...
174
			new ExpData( $this->exporter->getSpecialNsResource( 'owl', 'Restriction' ) )
0 ignored issues
show
$this->exporter->getSpec...e('owl', 'Restriction') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpResource>.

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...
175
		);
176
177
		$property = $description->getProperty();
178
179
		if ( $property->isInverse() ) {
180
			$property = new DIProperty( $property->getKey() );
181
		}
182
183
		$result->addPropertyObjectValue(
184
			$this->exporter->getSpecialNsResource( 'owl', 'onProperty' ),
0 ignored issues
show
$this->exporter->getSpec...ce('owl', 'onProperty') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpNsResource>.

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...
185
			new ExpData(
186
				$this->exporter->getResourceElementForProperty( $property )
0 ignored issues
show
$property of type object<SMW\DIProperty> is not a sub-type of object<SMWDIProperty>. It seems like you assume a child class of the class SMW\DIProperty 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...
$this->exporter->getReso...tForProperty($property) is of type object<SMW\Exporter\Element\ExpResource>, but the function expects a object<SMWExpResource>.

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...
187
			)
188
		);
189
190
		$subdata = $this->getExpDataFromDescription(
191
			$description->getDescription(),
192
			$exact
193
		);
194
195
		if ( ( $description->getDescription() instanceof ValueDescription ) &&
196
		     ( $description->getDescription()->getComparator() === SMW_CMP_EQ ) ) {
197
			$result->addPropertyObjectValue(
198
				$this->exporter->getSpecialNsResource( 'owl', 'hasValue' ),
0 ignored issues
show
$this->exporter->getSpec...urce('owl', 'hasValue') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpNsResource>.

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...
199
				$subdata
0 ignored issues
show
It seems like $subdata defined by $this->getExpDataFromDes...tDescription(), $exact) on line 190 can also be of type false or null; however, SMWExpData::addPropertyObjectValue() does only seem to accept object<SMW\Exporter\Element>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
200
			);
201
		} else {
202
			if ( $subdata === false ) {
203
204
				$owltype = $this->exporter->getOWLPropertyType(
205
					$description->getProperty()->findPropertyTypeID()
206
				);
207
208
				if ( $owltype == 'ObjectProperty' ) {
209
					$subdata = new ExpData(
210
						$this->exporter->getSpecialNsResource( 'owl', 'Thing' )
0 ignored issues
show
$this->exporter->getSpec...esource('owl', 'Thing') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpResource>.

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...
211
					);
212
				} elseif ( $owltype == 'DatatypeProperty' ) {
213
					$subdata = new ExpData(
214
						$this->exporter->getSpecialNsResource( 'rdfs', 'Literal' )
0 ignored issues
show
$this->exporter->getSpec...urce('rdfs', 'Literal') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpResource>.

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...
215
					);
216
				} else { // no restrictions at all with annotation properties ...
217
					return new ExpData(
218
						$this->exporter->getSpecialNsResource( 'owl', 'Thing' )
0 ignored issues
show
$this->exporter->getSpec...esource('owl', 'Thing') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpResource>.

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...
219
					);
220
				}
221
			}
222
223
			$result->addPropertyObjectValue(
224
				$this->exporter->getSpecialNsResource( 'owl', 'someValuesFrom' ),
0 ignored issues
show
$this->exporter->getSpec...owl', 'someValuesFrom') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpNsResource>.

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...
225
				$subdata
0 ignored issues
show
It seems like $subdata defined by $this->getExpDataFromDes...tDescription(), $exact) on line 190 can be null; however, SMWExpData::addPropertyObjectValue() 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...
226
			);
227
		}
228
229
		return $result;
230
	}
231
232 2
	private function doMapClassDescription( ClassDescription $description, &$exact ) {
0 ignored issues
show
The parameter $exact is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
233
234 2
		if ( count( $description->getCategories() ) == 1 ) { // single category
235 1
			$categories = $description->getCategories();
236 1
			$result = new ExpData(
237 1
				$this->exporter->getResourceElementForWikiPage( end( $categories ) )
0 ignored issues
show
$this->exporter->getReso...iPage(end($categories)) is of type object<SMW\Exporter\Element\ExpResource>, but the function expects a object<SMWExpResource>.

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...
238
			);
239
		} else { // disjunction of categories
240
241 1
			$result = new ExpData(
242 1
				new ExpResource( '' )
0 ignored issues
show
new \SMW\Exporter\Element\ExpResource('') is of type object<SMW\Exporter\Element\ExpResource>, but the function expects a object<SMWExpResource>.

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...
243
			);
244
245 1
			$elements = array();
246
247 1
			foreach ( $description->getCategories() as $cat ) {
248 1
				$elements[] = new ExpData(
249 1
					$this->exporter->getResourceElementForWikiPage( $cat )
0 ignored issues
show
$this->exporter->getReso...lementForWikiPage($cat) is of type object<SMW\Exporter\Element\ExpResource>, but the function expects a object<SMWExpResource>.

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...
250
				);
251
			}
252
253 1
			$result->addPropertyObjectValue(
254 1
				$this->exporter->getSpecialNsResource( 'owl', 'unionOf' ),
0 ignored issues
show
$this->exporter->getSpec...ource('owl', 'unionOf') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpNsResource>.

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...
255 1
				ExpData::makeCollection( $elements )
256
			);
257
		}
258
259 2
		$result->addPropertyObjectValue(
260 2
			$this->exporter->getSpecialNsResource( 'rdf', 'type' ),
0 ignored issues
show
$this->exporter->getSpec...Resource('rdf', 'type') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpNsResource>.

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...
261 2
			new ExpData(
262 2
				$this->exporter->getSpecialNsResource( 'owl', 'Class' )
0 ignored issues
show
$this->exporter->getSpec...esource('owl', 'Class') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpResource>.

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...
263
			)
264
		);
265
266 2
		return $result;
267
	}
268
269
	private function doMapConjunctionDisjunction( Description $description, &$exact ) {
270
271
		$result = new ExpData(
272
			new ExpResource( '' )
0 ignored issues
show
new \SMW\Exporter\Element\ExpResource('') is of type object<SMW\Exporter\Element\ExpResource>, but the function expects a object<SMWExpResource>.

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...
273
		);
274
275
		$result->addPropertyObjectValue(
276
			$this->exporter->getSpecialNsResource( 'rdf', 'type' ),
0 ignored issues
show
$this->exporter->getSpec...Resource('rdf', 'type') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpNsResource>.

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...
277
			new ExpData(
278
				$this->exporter->getSpecialNsResource( 'owl', 'Class' )
0 ignored issues
show
$this->exporter->getSpec...esource('owl', 'Class') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpResource>.

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...
279
			)
280
		);
281
282
		$elements = array();
283
284
		foreach ( $description->getDescriptions() as $subdesc ) {
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class SMW\Query\Language\Description as the method getDescriptions() does only exist in the following sub-classes of SMW\Query\Language\Description: SMW\Query\Language\Conjunction, SMW\Query\Language\Disjunction. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
285
			$element = $this->getExpDataFromDescription( $subdesc, $exact );
286
287
			if ( $element === false ) {
288
				$element = new ExpData(
289
					$this->exporter->getSpecialNsResource( 'owl', 'Thing' )
0 ignored issues
show
$this->exporter->getSpec...esource('owl', 'Thing') is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpResource>.

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...
290
				);
291
			}
292
293
			$elements[] = $element;
294
		}
295
296
		$prop = $description instanceof Conjunction ? 'intersectionOf' : 'unionOf';
297
298
		$result->addPropertyObjectValue(
299
			$this->exporter->getSpecialNsResource( 'owl', $prop ),
0 ignored issues
show
$this->exporter->getSpec...sResource('owl', $prop) is of type object<SMW\Exporter\Element\ExpNsResource>, but the function expects a object<SMWExpNsResource>.

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...
300
			ExpData::makeCollection( $elements )
301
		);
302
303
		return $result;
304
	}
305
306
}
307