| Conditions | 1 |
| Paths | 1 |
| Total Lines | 52 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php defined('BASEPATH') OR exit('No direct script access allowed'); |
||
| 9 | public function up() { |
||
| 10 | //Notice table |
||
| 11 | $this->dbforge->add_field(array( |
||
| 12 | 'id' => array( |
||
| 13 | 'type' => 'MEDIUMINT', |
||
| 14 | 'constraint' => '8', |
||
| 15 | 'unsigned' => TRUE, |
||
| 16 | 'auto_increment' => TRUE |
||
| 17 | ), |
||
| 18 | 'notice' => array( |
||
| 19 | 'type' => 'VARCHAR', |
||
| 20 | 'constraint' => '255' |
||
| 21 | //FOREIGN KEY |
||
| 22 | ), |
||
| 23 | 'created_at' => array( |
||
| 24 | //Despite not actually creating the field here (it's instead done below), we still need this here so a key can be created properly. |
||
| 25 | // 'type' => 'TIMESTAMP', |
||
| 26 | // 'null' => FALSE, |
||
| 27 | // 'on_update' => FALSE |
||
| 28 | ) |
||
| 29 | )); |
||
| 30 | $this->dbforge->add_field('created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'); //CI is annoying and auto-appends ON UPDATE which we don't want. |
||
| 31 | $this->dbforge->add_key('id', TRUE); |
||
| 32 | $this->dbforge->add_key('created_at'); |
||
| 33 | $this->dbforge->create_table('tracker_notices'); |
||
| 34 | |||
| 35 | //User notice hidden table |
||
| 36 | $this->dbforge->add_field(array( |
||
| 37 | 'user_id' => array( |
||
| 38 | 'type' => 'MEDIUMINT', |
||
| 39 | 'constraint' => '8', |
||
| 40 | 'unsigned' => TRUE, |
||
| 41 | //FOREIGN KEY |
||
| 42 | ), |
||
| 43 | 'hidden_notice_id' => array( |
||
| 44 | 'type' => 'MEDIUMINT', |
||
| 45 | 'constraint' => '8', |
||
| 46 | 'unsigned' => TRUE |
||
| 47 | //FOREIGN KEY |
||
| 48 | ) |
||
| 49 | )); |
||
| 50 | $this->dbforge->add_key('user_id', TRUE); |
||
| 51 | $this->dbforge->create_table('tracker_user_notices'); |
||
| 52 | |||
| 53 | /*** Unique/Foreign Keys ***/ |
||
| 54 | //For whatever reason, dbforge lacks a unique/foreign key function. |
||
| 55 | $this->db->query(' |
||
| 56 | ALTER TABLE `tracker_user_notices` |
||
| 57 | ADD CONSTRAINT `FK_tracker_user_notices_auth_users` FOREIGN KEY (`user_id`) REFERENCES `auth_users`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION, |
||
| 58 | ADD CONSTRAINT `FK_tracker_user_notices_tracker_notices` FOREIGN KEY (`hidden_notice_id`) REFERENCES `tracker_notices` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION;' |
||
| 59 | ); |
||
| 60 | } |
||
| 61 | |||
| 67 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: