Completed
Push — master ( d2d28e...1c2760 )
by mw
35:37
created

MandatoryTypePropertyAnnotator.php (1 issue)

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\PropertyAnnotator;
4
5
use SMW\DataTypeRegistry;
6
use SMW\DataValueFactory;
7
use SMW\DIProperty;
8
use SMW\PropertyAnnotator;
9
use SMW\Store;
10
11
/**
12
 * @license GNU GPL v2+
13
 * @since 2.2
14
 *
15
 * @author mwjames
16
 */
17
class MandatoryTypePropertyAnnotator extends PropertyAnnotatorDecorator {
18
19 187
	protected function addPropertyValues() {
20
21 187
		$subject = $this->getSemanticData()->getSubject();
22
23 187
		if ( $subject->getNamespace() !== SMW_NS_PROPERTY ) {
24 177
			return;
25
		}
26
27 146
		$property = DIProperty::newFromUserLabel(
28 146
			str_replace( '_', ' ', $subject->getDBKey() )
29
		);
30
31 146
		if ( !$property->isUserDefined() ) {
32 1
			return;
33
		}
34
35 145
		$this->findMandatoryTypeForImportVocabulary();
36 145
	}
37
38 145
	private function findMandatoryTypeForImportVocabulary() {
39
40 145
		$property = new DIProperty( '_IMPO' );
41
42 145
		$dataItems = $this->getSemanticData()->getPropertyValues(
43
			$property
44
		);
45
46 145
		if ( $dataItems === null || $dataItems === array() ) {
47 138
			return;
48
		}
49
50 10
		$this->addTypeFromImportVocabulary( $property, current( $dataItems ) );
51 10
	}
52
53 10
	private function addTypeFromImportVocabulary( $property, $dataItem ) {
54
55 10
		$importValue = DataValueFactory::getInstance()->newDataValueByItem(
56
			$dataItem,
57
			$property
58
		);
59
60 10
		if ( strpos( $importValue->getTermType(), ':' ) === false ) {
61 1
			return;
62
		}
63
64 9
		$property = new DIProperty( '_TYPE' );
65
66 9
		list( $ns, $type ) = explode( ':', $importValue->getTermType(), 2 );
0 ignored issues
show
The assignment to $ns is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
67
68 9
		$typeId = DataTypeRegistry::getInstance()->findTypeId( $type );
69
70 9
		if ( $typeId === '' ) {
71 1
			return;
72
		}
73
74 8
		$dataValue = DataValueFactory::getInstance()->newDataValueByProperty(
75
			$property,
76
			$typeId
77
		);
78
79 8
		$this->replaceAnyTypeByImportType( $property, $dataValue );
80 8
	}
81
82 8
	private function replaceAnyTypeByImportType( DIProperty $property, $dataValue ) {
83
84 8
		foreach ( $this->getSemanticData()->getPropertyValues( $property ) as $dataItem ) {
85 2
			$this->getSemanticData()->removePropertyObjectValue(
86
				$property,
87
				$dataItem
88
			);
89
		}
90
91 8
		$this->getSemanticData()->addDataValue( $dataValue );
92 8
	}
93
94
}
95