Completed
Push — master ( 45893c...373238 )
by Angus
02:39
created

Migration_Favourites_Rework   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A up() 0 70 2
A down() 0 3 1
1
<?php defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class Migration_Favourites_Rework 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_Install_ion_auth, Migration_Setup_Favourites, 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_Site_Status, Migration_Tracker_Add_Title_Status, Migration_Tracker_add_category, Migration_Tracker_add_complete, Migration_Tracker_add_last_checked, Migration_Tracker_add_tags. 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
		$this->dbforge->add_field(array(
11
			'id' => array(
12
				'type'           => 'MEDIUMINT',
13
				'constraint'     => '8',
14
				'unsigned'       => TRUE,
15
				'auto_increment' => TRUE
16
			),
17
			'user_id' => array(
18
				'type'       => 'MEDIUMINT',
19
				'constraint' => '8',
20
				'unsigned'   => TRUE,
21
				'null'       => FALSE
22
				//FOREIGN KEY
23
			),
24
			'title_id' => array(
25
				'type'       => 'MEDIUMINT',
26
				'constraint' => '8',
27
				'unsigned'   => TRUE,
28
				'null'       => FALSE
29
				//FOREIGN KEY
30
			),
31
			'chapter' => array(
32
				'type'       => 'VARCHAR',
33
				'constraint' => '255',
34
				'null'       => FALSE
35
			),
36
			'updated_at' => array(
37
				//Despite not actually creating the field here (it's instead done below), we still need this here so a key can be created properly.
38
				//	'type'    => 'TIMESTAMP',
39
				//	'null'    => FALSE,
40
				//	'on_update' => FALSE
41
			)
42
		));
43
		$this->dbforge->add_field('updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'); //CI is annoying and auto-appends ON UPDATE which we don't want.
0 ignored issues
show
Documentation introduced by
'updated_at TIMESTAMP NO...AULT CURRENT_TIMESTAMP' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
		$this->dbforge->add_key('id', TRUE);
45
		$this->dbforge->add_key('user_id');
46
		$this->dbforge->add_key('title_id');
47
		$this->dbforge->add_key('updated_at');
48
		$this->dbforge->create_table('tracker_favourites_new');
49
50
		//We need to add data from the old favourites table
51
		$query = $this->db->select('tf.id AS id, tc.user_id As user_id, tc.title_id AS title_id, tf.chapter AS chapter, tf.updated_at AS updated_at')
52
		                  ->from('tracker_favourites AS tf')
53
		                  ->join('tracker_chapters AS tc', 'tf.chapter_id = tc.id', 'left')
54
		                  ->order_by('tf.id ASC')
55
		                  ->get();
56
		if($data = $query->result_array()) {
57
			$this->db->insert_batch('tracker_favourites_new', $data);
58
		}
59
60
		//Rename the old DB
61
		$this->dbforge->rename_table('tracker_favourites', 'tracker_favourites_old');
62
63
		//Rename the new one to old name
64
		$this->dbforge->rename_table('tracker_favourites_new', 'tracker_favourites');
65
66
		/*** Unique/Foreign Keys ***/
67
		//For whatever reason, dbforge lacks a unique/foreign key function.
68
		$this->db->query('ALTER TABLE `tracker_favourites` ADD UNIQUE(`user_id`, `title_id`, `chapter`);');
69
70
		$this->db->query('
71
				ALTER TABLE `tracker_favourites`
72
				ADD CONSTRAINT `FK_tracker_favourites_auth_users` FOREIGN KEY (`user_id`) REFERENCES `auth_users`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
73
				ADD CONSTRAINT `FK_tracker_favourites_tracker_titles` FOREIGN KEY (`title_id`) REFERENCES `tracker_titles` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION;'
74
		);
75
76
		//Remove old DB
77
		$this->dbforge->drop_table('tracker_favourites_old');
78
	}
79
80
	public function down() {
81
		//Reverting is a nope.
82
	}
83
}
84