Completed
Push — master ( 9eae06...a41cb5 )
by mw
16s
created

AbstractMultiValue::getPropertyDataItemByIndex()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 17
ccs 8
cts 8
cp 1
crap 5
rs 8.8571
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
	 * Create a list (array with numeric keys) containing the datavalue
44
	 * objects that this SMWRecordValue object holds. Values that are not
45
	 * present are set to null. Note that the first index in the array is
46
	 * 0, not 1.
47
	 *
48
	 * @since 2.5
49
	 *
50
	 * @return DataItem[]|null
51
	 */
52
	abstract public function getDataItems();
53
54
	/**
55
	 * Return the array (list) of properties that the individual entries of
56
	 * this datatype consist of.
57
	 *
58
	 * @since 2.5
59
	 *
60
	 * @return DIProperty[]|null
61
	 */
62
	abstract public function getPropertyDataItems();
63
64
	/**
65
	 * @note called by SMWResultArray::loadContent for matching an index as denoted
66
	 * in |?Foo=Bar|+index=1 OR |?Foo=Bar|+index=Bar
67
	 *
68
	 * @see https://www.semantic-mediawiki.org/wiki/Help:Type_Record#Semantic_search
69
	 *
70
	 * @since 2.5
71
	 *
72
	 * @param string|integer $index
73
	 *
74
	 * @return DataItem[]|null
75
	 */
76 4
	public function getDataItemByIndex( $index ) {
77
78 4
		if ( is_numeric( $index ) ) {
79 3
			$pos = $index - 1;
80 3
			$dataItems = $this->getDataItems();
81 3
			return isset( $dataItems[$pos] ) ? $dataItems[$pos] : null;
0 ignored issues
show
Bug Compatibility introduced by
The expression isset($dataItems[$pos]) ? $dataItems[$pos] : null; of type SMW\DataValues\DataItem|null adds the type SMW\DataValues\DataItem to the return on line 81 which is incompatible with the return type documented by SMW\DataValues\AbstractM...lue::getDataItemByIndex of type SMW\DataValues\DataItem[]|null.
Loading history...
82
		}
83
84 1
		if ( ( $property = $this->getPropertyDataItemByIndex( $index ) ) !== null ) {
85 1
			$values = $this->getDataItem()->getSemanticData()->getPropertyValues( $property );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataItem as the method getSemanticData() does only exist in the following sub-classes of SMWDataItem: SMWDIContainer. 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...
86 1
			return reset( $values );
87
		}
88
89
		return null;
90
	}
91
92
	/**
93
	 * @note called by SMWResultArray::getNextDataValue to match an index
94
	 * that has been denoted using |?Foo=Bar|+index=1 OR |?Foo=Bar|+index=Bar
95
	 *
96
	 * @since 2.5
97
	 *
98
	 * @param string|integer $index
99
	 *
100
	 * @return DIProperty|null
101
	 */
102 4
	public function getPropertyDataItemByIndex( $index ) {
103
104 4
		$properties = $this->getPropertyDataItems();
105
106 4
		if ( is_numeric( $index ) ) {
107 3
			$pos = $index - 1;
108 3
			return isset( $properties[$pos] ) ? $properties[$pos] : null;
109
		}
110
111 1
		foreach ( $properties as $property ) {
0 ignored issues
show
Bug introduced by
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...
112 1
			if ( $property->getLabel() === $index ) {
113 1
				return $property;
114
			}
115
		}
116
117
		return null;
118
	}
119
120
}
121