Completed
Push — master ( 2472c5...a814e8 )
by mw
35:03
created

src/DataValues/AbstractMultiValue.php (2 issues)

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 SMW\DataValues;
4
5
use SMWDataValue as DataValue;
6
7
/**
8
 * @private
9
 *
10
 * @license GNU GPL v2+
11
 * @since 2.5
12
 *
13
 * @author mwjames
14
 */
15
abstract class AbstractMultiValue extends DataValue {
16
17
	/**
18
	 * @since 2.5
19
	 *
20
	 * @param string $userValue
21
	 *
22
	 * @return array
23
	 */
24
	abstract public function getValuesFromString( $userValue );
25
26
	/**
27
	 * @since 2.5
28
	 *
29
	 * @param DIProperty[] $properties
30
	 *
31
	 * @return DIProperty[]|null
32
	 */
33
	abstract public function setFieldProperties( array $properties );
34
35
	/**
36
	 * @since 2.5
37
	 *
38
	 * @return DIProperty[]|null
39
	 */
40
	abstract public function getProperties();
41
42
	/**
43
	 * Return the array (list) of properties that the individual entries of
44
	 * this datatype consist of.
45
	 *
46
	 * @since 2.5
47
	 *
48
	 * @return DIProperty[]|null
49
	 */
50
	abstract public function getPropertyDataItems();
51
52
	/**
53
	 * Create a list (array with numeric keys) containing the datavalue
54
	 * objects that this SMWRecordValue object holds. Values that are not
55
	 * present are set to null. Note that the first index in the array is
56
	 * 0, not 1.
57
	 *
58
	 * @since 2.5
59
	 *
60
	 * @return DataItem[]|null
61
	 */
62 9
	public function getDataItems() {
63
64 9
		if ( !$this->isValid() ) {
65 6
			return array();
66
		}
67
68 9
		$dataItems = array();
69 9
		$index = 0;
70
71 9
		foreach ( $this->getPropertyDataItems() as $diProperty ) {
0 ignored issues
show
The expression $this->getPropertyDataItems() of type array<integer,object<SMW...alues\DIProperty>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
72 9
			$values = $this->getDataItem()->getSemanticData()->getPropertyValues( $diProperty );
73 9
			$dataItems[$index] = count( $values ) > 0 ? reset( $values ) : null;
74 9
			$index++;
75
		}
76
77 9
		return $dataItems;
78
	}
79
80
	/**
81
	 * @note called by SMWResultArray::loadContent for matching an index as denoted
82
	 * in |?Foo=Bar|+index=1 OR |?Foo=Bar|+index=Bar
83
	 *
84
	 * @see https://www.semantic-mediawiki.org/wiki/Help:Type_Record#Semantic_search
85
	 *
86
	 * @since 2.5
87
	 *
88
	 * @param string|integer $index
89
	 *
90
	 * @return DataItem[]|null
91
	 */
92 5
	public function getDataItemByIndex( $index ) {
93
94 5
		if ( is_numeric( $index ) ) {
95 3
			$pos = $index - 1;
96 3
			$dataItems = $this->getDataItems();
97 3
			return isset( $dataItems[$pos] ) ? $dataItems[$pos] : null;
98
		}
99
100 2
		if ( ( $property = $this->getPropertyDataItemByIndex( $index ) ) !== null ) {
101 2
			$values = $this->getDataItem()->getSemanticData()->getPropertyValues( $property );
102 2
			return reset( $values );
103
		}
104
105
		return null;
106
	}
107
108
	/**
109
	 * @note called by SMWResultArray::getNextDataValue to match an index
110
	 * that has been denoted using |?Foo=Bar|+index=1 OR |?Foo=Bar|+index=Bar
111
	 *
112
	 * @since 2.5
113
	 *
114
	 * @param string|integer $index
115
	 *
116
	 * @return DIProperty|null
117
	 */
118 6
	public function getPropertyDataItemByIndex( $index ) {
119
120 6
		$properties = $this->getPropertyDataItems();
121
122 6
		if ( is_numeric( $index ) ) {
123 4
			$pos = $index - 1;
124 4
			return isset( $properties[$pos] ) ? $properties[$pos] : null;
125
		}
126
127 2
		foreach ( $properties as $property ) {
0 ignored issues
show
The expression $properties of type array<integer,object<SMW...alues\DIProperty>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
128 2
			if ( $property->getLabel() === $index ) {
129 2
				return $property;
130
			}
131
		}
132
133
		return null;
134
	}
135
136
}
137