Completed
Push — master ( 14d2bd...06e609 )
by mw
81:37 queued 59:24
created

SMWDIHandlerBoolean::getWhereConds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * @ingroup SMWDataItemsHandlers
4
 */
5
6
/**
7
 * This class implements Store access to Boolean data items.
8
 *
9
 * @since 1.8
10
 *
11
 * @author Nischay Nahata
12
 * @ingroup SMWDataItemsHandlers
13
 */
14
class SMWDIHandlerBoolean extends SMWDataItemHandler {
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...
15
16
	/**
17
	 * Method to return array of fields for a DI type
18
	 *
19
	 * @return array
20
	 */
21 26
	public function getTableFields() {
22 26
		return array( 'o_value' => 'b' );
23
	}
24
25
	/**
26
	 * @see SMWDataItemHandler::getFetchFields()
27
	 *
28
	 * @since 1.8
29
	 * @return array
30
	 */
31 5
	public function getFetchFields() {
32 5
		return array( 'o_value' => 'b' );
33
	}
34
35
	/**
36
	 * Method to return an array of fields=>values for a DataItem
37
	 *
38
	 * @return array
39
	 */
40
	public function getWhereConds( SMWDataItem $dataItem ) {
41
		return array(
42
			'o_value' => $dataItem->getBoolean() ? 1 : 0,
0 ignored issues
show
Bug introduced by
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...
43
		);
44
	}
45
46
	/**
47
	 * Method to return an array of fields=>values for a DataItem
48
	 * This array is used to perform all insert operations into the DB
49
	 * To optimize return minimum fields having indexes
50
	 *
51
	 * @return array
52
	 */
53 12
	public function getInsertValues( SMWDataItem $dataItem ) {
54
		return array(
55 12
			'o_value' => $dataItem->getBoolean() ? 1 : 0,
0 ignored issues
show
Bug introduced by
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...
56
		);
57
	}
58
59
	/**
60
	 * Method to return the field used to select this type of DataItem
61
	 * @since 1.8
62
	 * @return string
63
	 */
64 7
	public function getIndexField() {
65 7
		return 'o_value';
66
	}
67
68
	/**
69
	 * Method to return the field used to select this type of DataItem
70
	 * using the label
71
	 * @since 1.8
72
	 * @return string
73
	 */
74 5
	public function getLabelField() {
75 5
		return 'o_value';
76
	}
77
78
	/**
79
	 * Method to create a dataitem from an array of DB keys.
80
	 *
81
	 * @since 1.8
82
	 * @param array|string $dbkeys should be a string here
83
	 *
84
	 * @return SMWDataItem
85
	 */
86 3
	public function dataItemFromDBKeys( $dbkeys ) {
87 3
		global $wgDBtype;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
88
89
		//PgSQL returns as t and f and need special handling http://archives.postgresql.org/pgsql-php/2010-02/msg00005.php
90 3
		if ( $wgDBtype == 'postgres' ) {
91
			$value = ( $dbkeys == 't' );
92
		} else {
93 3
			$value = ( $dbkeys == '1' );
94
		}
95
96 3
		return new SMWDIBoolean( $value );
97
	}
98
}
99