Completed
Push — master ( f2f6e8...470987 )
by mw
33:59
created

SMWContainerSemanticData::hasAnonymousSubject()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.0729

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 2
nop 0
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 5.0729
rs 8.8571
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A SMWDIContainer::getSerialization() 0 3 1
1
<?php
2
/**
3
 * @ingroup SMWDataItems
4
 */
5
6
use SMW\Exception\DataItemException;
7
use SMW\DIProperty;
8
use SMWDIBlob as DIBlob;
9
10
/**
11
 * This class implements container data items that can store SMWSemanticData
12
 * objects. Containers are not dataitems in the proper sense: they do not
13
 * represent a single, opaque value that can be assigned to a property. Rather,
14
 * a container represents a "subobject" with a number of property-value
15
 * assignments. When a container is stored, these individual data assignments
16
 * are stored -- the data managed by SMW never contains any "container", just
17
 * individual property assignments for the subobject. Likewise, when a container
18
 * is used in search, it is interpreted as a patterns of possible property
19
 * assignments, and this pattern is searched for.
20
 *
21
 * The data encapsulated in a container data item is essentially an
22
 * SMWSemanticData object of class SMWContainerSemanticData. This class allows
23
 * the subject to be kept anonymous if not known (if no context page is
24
 * available for finding a suitable subobject name). See the repsective
25
 * documentation for details.
26
 *
27
 * Being a mere placeholder/template for other data, an SMWDIContainer is not
28
 * immutable as the other basic data items. New property-value pairs can always
29
 * be added to the internal SMWContainerSemanticData.
30
 *
31
 * @since 1.6
32
 *
33
 * @author Markus Krötzsch
34
 * @ingroup SMWDataItems
35
 */
36
class SMWDIContainer extends SMWDataItem {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
37
38
	/**
39
	 * Internal value.
40
	 *
41
	 * @var SMWSemanticData
42
	 */
43
	protected $m_semanticData;
44 3
45 3
	/**
46 3
	 * Constructor. The given SMWContainerSemanticData object will be owned
47
	 * by the constructed object afterwards, and in particular will not
48 3
	 * allow further changes.
49
	 *
50
	 * @param $semanticData SMWContainerSemanticData
51
	 */
52 3
	public function __construct( SMWContainerSemanticData $semanticData ) {
53
		$this->m_semanticData = $semanticData;
0 ignored issues
show
Documentation Bug introduced by
It seems like $semanticData of type object<SMWContainerSemanticData> is incompatible with the declared type object<SMWSemanticData> of property $m_semanticData.

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...
54
	}
55
56
	public function getDIType() {
57
		return SMWDataItem::TYPE_CONTAINER;
58
	}
59
60
	public function getSemanticData() {
61
		return $this->m_semanticData;
62
	}
63
64
	public function getSortKey() {
65
		return '';
66 3
	}
67 3
68 3
	/**
69
	 * @since 2.5
70
	 *
71
	 * @param string $sortKey
72
	 */
73
	public function addCompositeSortKey( $sortKey ) {
74
		$this->m_semanticData->addPropertyObjectValue(
75
			new DIProperty( '_SKEY' ),
76 164
			new DIBlob( $this->m_semanticData->getSubject()->getSortKey() . '#' . $sortKey )
77 164
		);
78 164
	}
79 164
80 164
	public function getSerialization() {
81
		return serialize( $this->m_semanticData );
82
	}
83 164
84
	/**
85
	 * Get a hash string for this data item.
86
	 *
87
	 * @return string
88
	 */
89
	public function getHash() {
90
91
		$hash = $this->getValueHash( $this->m_semanticData );
92
		sort( $hash );
93
94 164
		return md5( implode( '#', $hash ) );
95
96 164
		// We want a value hash, not an entity hash!!
97
		// return $this->m_semanticData->getHash();
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
	}
99
100 164
	private function getValueHash( $semanticData ) {
101
102
		$hash = array();
103
104
		foreach ( $semanticData->getProperties() as $property ) {
105
			$hash[] = $property->getKey();
106
107
			foreach ( $semanticData->getPropertyValues( $property ) as $di ) {
108
				$hash[] = $di->getHash();
109
			}
110
		}
111
112
		foreach ( $semanticData->getSubSemanticData() as $data ) {
113 21
			$hash[] = $this->getValueHash( $data );
114 21
		}
115 21
116 21
		return $hash;
117 21
	}
118 21
119
	/**
120 21
	 * Create a data item from the provided serialization string and type
121 21
	 * ID.
122 21
	 *
123 21
	 * @return SMWDIContainer
124
	 */
125
	public static function doUnserialize( $serialization ) {
126
		/// TODO May issue an E_NOTICE when problems occur; catch this
127
		$data = unserialize( $serialization );
128
		if ( !( $data instanceof SMWContainerSemanticData ) ) {
0 ignored issues
show
Bug introduced by
The class SMWContainerSemanticData does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
129
			throw new DataItemException( "Could not unserialize SMWDIContainer from the given string." );
130
		}
131
		return new SMWDIContainer( $data );
132
	}
133
134
	public function equals( SMWDataItem $di ) {
135
		if ( $di->getDIType() !== SMWDataItem::TYPE_CONTAINER ) {
136
			return false;
137
		}
138
139
		return $di->getSerialization() === $this->getSerialization();
140
	}
141
}
142