Kohana_Jam_Behavior_Auth_User_Token   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A builder_call_valid_token() 0 6 2
A builder_call_expired() 0 4 3
1
<?php defined('SYSPATH') OR die('No direct access allowed.');
2
/**
3
 * @package    Jam
4
 * @category   Behavior
5
 * @author     Ivan Kerin
6
 * @copyright  (c) 2011-2012 Despark Ltd.
7
 * @license    http://www.opensource.org/licenses/isc-license.txt
8
 */
9
class Kohana_Jam_Behavior_Auth_User_Token extends Jam_Behavior {
10
11 5
	public function builder_call_valid_token(Database_Query $query, Jam_Event_Data $data, $token, $current_time = NULL)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
12
	{
13
		$query
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Database_Query as the method where() does only exist in the following sub-classes of Database_Query: Database_Query_Builder_Delete, Database_Query_Builder_Select, Database_Query_Builder_Update, Database_Query_Builder_Where, Jam_Query_Builder_Collection, Jam_Query_Builder_Delete, Jam_Query_Builder_Select, Jam_Query_Builder_Update, Kohana_Database_Query_Builder_Delete, Kohana_Database_Query_Builder_Select, Kohana_Database_Query_Builder_Update, Kohana_Database_Query_Builder_Where, Kohana_Jam_Query_Builder_Collection, Kohana_Jam_Query_Builder_Delete, Kohana_Jam_Query_Builder_Select, Kohana_Jam_Query_Builder_Update, Model_Collection_Test_Author. 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...
14 5
			->where('token', '=', $token)
15 5
			->where('expires', '>=', ($current_time === NULL) ? time() : $current_time);
16 5
	}
17
18 1
	public function builder_call_expired(Database_Query $query, Jam_Event_Data $data, $token = TRUE, $current_time = NULL)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
	{
20 1
		$query->where('expires', (bool) $token ? '<' : '>=', ($current_time === NULL) ? time() : $current_time);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Database_Query as the method where() does only exist in the following sub-classes of Database_Query: Database_Query_Builder_Delete, Database_Query_Builder_Select, Database_Query_Builder_Update, Database_Query_Builder_Where, Jam_Query_Builder_Collection, Jam_Query_Builder_Delete, Jam_Query_Builder_Select, Jam_Query_Builder_Update, Kohana_Database_Query_Builder_Delete, Kohana_Database_Query_Builder_Select, Kohana_Database_Query_Builder_Update, Kohana_Database_Query_Builder_Where, Kohana_Jam_Query_Builder_Collection, Kohana_Jam_Query_Builder_Delete, Kohana_Jam_Query_Builder_Select, Kohana_Jam_Query_Builder_Update, Model_Collection_Test_Author. 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...
21
	}
22
}