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 | * 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
Loading history...
|
|||
82 | } |
||
83 | |||
84 | 1 | if ( ( $property = $this->getPropertyDataItemByIndex( $index ) ) !== null ) { |
|
85 | 1 | $values = $this->getDataItem()->getSemanticData()->getPropertyValues( $property ); |
|
0 ignored issues
–
show
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
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
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
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.
Loading history...
|
|||
112 | 1 | if ( $property->getLabel() === $index ) { |
|
113 | 1 | return $property; |
|
114 | } |
||
115 | } |
||
116 | |||
117 | return null; |
||
118 | } |
||
119 | |||
120 | } |
||
121 |