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

includes/dataitems/SMW_DI_Bool.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
use SMW\DataItemException;
6
7
/**
8
 * This class implements Boolean data items.
9
 *
10
 * @since 1.6
11
 *
12
 * @author Markus Krötzsch
13
 * @ingroup SMWDataItems
14
 */
15
class SMWDIBoolean 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...
16
17
	/**
18
	 * Internal value.
19
	 * @var bool
20
	 */
21
	protected $m_boolean;
22
23 18
	public function __construct( $boolean ) {
24 18
		if ( !is_bool( $boolean ) ) {
25
			throw new DataItemException( "Initialization value '$boolean' is not a boolean." );
26
		}
27
28 18
		$this->m_boolean = ( $boolean == true );
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
29 18
	}
30
31 16
	public function getDIType() {
32 16
		return SMWDataItem::TYPE_BOOLEAN;
33
	}
34
35 16
	public function getBoolean() {
36 16
		return $this->m_boolean;
37
	}
38
39 16
	public function getSerialization() {
40 16
		return $this->m_boolean ? 't' : 'f';
41
	}
42
43 2
	public function getSortKey() {
44 2
		return $this->m_boolean ? 1 : 0;
45
	}
46
47
	/**
48
	 * Create a data item from the provided serialization string and type
49
	 * ID.
50
	 * @return SMWDIBoolean
51
	 */
52 2
	public static function doUnserialize( $serialization ) {
53 2
		if ( $serialization == 't' ) {
54 1
			return new SMWDIBoolean( true );
55 1
		} elseif  ( $serialization == 'f' ) {
56 1
			return new SMWDIBoolean( false );
57
		} else {
58
			throw new DataItemException( "Boolean data item unserialised from illegal value '$serialization'" );
59
		}
60
	}
61
62 4
	public function equals( SMWDataItem $di ) {
63 4
		if ( $di->getDIType() !== SMWDataItem::TYPE_BOOLEAN ) {
64 2
			return false;
65
		}
66 2
		return $di->getBoolean() === $this->m_boolean;
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class SMWDataItem as the method getBoolean() does only exist in the following sub-classes of SMWDataItem: SMWDIBoolean. 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...
67
	}
68
}
69