These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * @ingroup SMWDataItems |
||
4 | */ |
||
5 | |||
6 | use SMW\DataItemException; |
||
7 | |||
8 | /** |
||
9 | * This class implements number data items. |
||
10 | * |
||
11 | * @since 1.6 |
||
12 | * |
||
13 | * @author Markus Krötzsch |
||
14 | * @ingroup SMWDataItems |
||
15 | */ |
||
16 | class SMWDINumber extends SMWDataItem { |
||
17 | |||
18 | /** |
||
19 | * Internal value. |
||
20 | * @var float|int |
||
21 | */ |
||
22 | protected $m_number; |
||
23 | |||
24 | 112 | public function __construct( $number ) { |
|
25 | 112 | if ( !is_numeric( $number ) ) { |
|
26 | throw new DataItemException( "Initialization value '$number' is not a number." ); |
||
27 | } |
||
28 | 112 | $this->m_number = $number; |
|
29 | 112 | } |
|
30 | |||
31 | 111 | public function getDIType() { |
|
32 | 111 | return SMWDataItem::TYPE_NUMBER; |
|
33 | } |
||
34 | |||
35 | 111 | public function getNumber() { |
|
36 | 111 | return $this->m_number; |
|
37 | } |
||
38 | |||
39 | 15 | public function getSortKey() { |
|
40 | 15 | return $this->m_number; |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * @see SMWDataItem::getSortKeyDataItem() |
||
45 | * @return SMWDataItem |
||
46 | */ |
||
47 | public function getSortKeyDataItem() { |
||
48 | return $this; |
||
49 | } |
||
50 | |||
51 | 115 | public function getSerialization() { |
|
52 | 115 | return strval( $this->m_number ); |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Create a data item from the provided serialization string and type |
||
57 | * ID. |
||
58 | * @note PHP can convert any string to some number, so we do not do |
||
59 | * validation here (because this would require less efficient parsing). |
||
60 | * @return SMWDINumber |
||
61 | */ |
||
62 | 32 | public static function doUnserialize( $serialization ) { |
|
63 | 32 | return new SMWDINumber( floatval( $serialization ) ); |
|
64 | } |
||
65 | |||
66 | 5 | public function equals( SMWDataItem $di ) { |
|
67 | 5 | if ( $di->getDIType() !== SMWDataItem::TYPE_NUMBER ) { |
|
68 | 2 | return false; |
|
69 | } |
||
70 | |||
71 | 3 | return $di->getNumber() === $this->m_number; |
|
0 ignored issues
–
show
|
|||
72 | } |
||
73 | |||
74 | } |
||
75 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: