| Conditions | 2 |
| Paths | 2 |
| Total Lines | 18 |
| Code Lines | 9 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 9 |
| CRAP Score | 2 |
| Changes | 0 | ||
| 1 | <?php |
||
| 67 | 39 | public function setDatabase($database = null) |
|
| 68 | { |
||
| 69 | 39 | $ci = Instance::create(); |
|
| 70 | |||
| 71 | 39 | $ci->load->helper('inflector'); |
|
| 72 | |||
| 73 | 39 | if (empty($database)) { |
|
| 74 | 3 | $ci->load->database(); |
|
| 75 | |||
| 76 | 3 | $this->db = $ci->db; |
|
| 77 | |||
| 78 | 3 | return $this; |
|
| 79 | } |
||
| 80 | |||
| 81 | 39 | $this->db = $database; |
|
| 82 | |||
| 83 | 39 | return $this; |
|
| 84 | } |
||
| 85 | |||
| 99 |
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: