Completed
Push — master ( 314506...335380 )
by mw
100:54 queued 62:54
created

includes/dataitems/SMW_DI_Number.php (1 issue)

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
 * @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
It seems like you code against a specific sub-type and not the parent class SMWDataItem as the method getNumber() does only exist in the following sub-classes of SMWDataItem: SMWDINumber. 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...
72
	}
73
74
}
75