Completed
Push — master ( 631a79...21674a )
by Angus
02:45
created

Migration_Tracker_Add_MAL_id::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class Migration_Tracker_Add_MAL_id extends CI_Migration {
4
	public function __construct() {
5
		parent::__construct();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class CI_Migration as the method __construct() does only exist in the following sub-classes of CI_Migration: Migration_Auth_add_apikey, Migration_Favourites_Rework, Migration_Favourites_Rework_Revert, Migration_Install_ion_auth, Migration_Setup_Favourites, Migration_Setup_Notices, Migration_Setup_Rate_Limit, Migration_Setup_Sessions, Migration_Setup_Signup_Verification, Migration_Setup_Title_History, Migration_Setup_Tracker, Migration_Setup_User_History, Migration_Setup_User_Options, Migration_Tracker_Add_Active, Migration_Tracker_Add_MAL_id, Migration_Tracker_Add_Site_Custom, Migration_Tracker_Add_Site_Status, Migration_Tracker_Add_Title_Status, Migration_Tracker_add_category, Migration_Tracker_add_complete, Migration_Tracker_add_ignore_chapter, Migration_Tracker_add_last_checked, Migration_Tracker_add_tags, Migration_Update_Sites_20170415, Migration_Update_Sites_20170503. 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...
6
		$this->load->dbforge();
7
	}
8
9
	public function up() {
10
		$fields = array(
11
			'mal_id' => array(
12
				'type'      => 'MEDIUMINT',
13
				'constraint' => '7',
14
				'null'       => TRUE,
15
				'unsigned'   => TRUE,
16
17
				//'default'    => NULL
18
			)
19
		);
20
21
		$this->dbforge->add_column('tracker_titles', $fields, 'status');
22
		$this->dbforge->add_column('tracker_chapters', $fields, 'ignore_chapter');
23
	}
24
25
	public function down() {
26
		$this->dbforge->drop_column('tracker_titles',   'mal_id');
27
		$this->dbforge->drop_column('tracker_chapters', 'mal_id');
28
29
	}
30
}
31