Completed
Push — master ( 9cb597...d2d2ad )
by mw
13s
created

includes/dataitems/SMW_DI_Number.php (3 issues)

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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
18
	/**
19
	 * Internal value.
20
	 * @var float|int
21
	 */
22
	protected $m_number;
23
24 108
	public function __construct( $number ) {
25 108
		if ( !is_numeric( $number ) ) {
26
			throw new DataItemException( "Initialization value '$number' is not a number." );
27
		}
28 108
		$this->m_number = $number;
0 ignored issues
show
Documentation Bug introduced by
It seems like $number can also be of type string. However, the property $m_number is declared as type double|integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
29 108
	}
30
31 107
	public function getDIType() {
32 107
		return SMWDataItem::TYPE_NUMBER;
33
	}
34
35 107
	public function getNumber() {
36 107
		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 111
	public function getSerialization() {
52 111
		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 29
	public static function doUnserialize( $serialization ) {
63 29
		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