| Conditions | 8 |
| Paths | 32 |
| Total Lines | 26 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 12 |
| CRAP Score | 10.3696 |
| Changes | 0 | ||
| 1 | <?php |
||
| 34 | 36 | protected function getTableName($table, $isForeignKey = false) |
|
| 35 | { |
||
| 36 | 36 | $tableName = ''; |
|
| 37 | |||
| 38 | 36 | if ($table instanceof \CI_Model) { |
|
| 39 | if (method_exists($table, 'getTableName')) { |
||
| 40 | $tableName = $table->getTableName(); |
||
|
|
|||
| 41 | } elseif (property_exists($table, 'table')) { |
||
| 42 | // NOTE: To be removed in v1.0.0 |
||
| 43 | $tableName = $table->table; |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | 36 | if (! $isForeignKey && $this->table) { |
|
| 48 | 24 | $tableName = $this->table; |
|
| 49 | 24 | } |
|
| 50 | |||
| 51 | 36 | if (is_string($table)) { |
|
| 52 | 36 | $tableName = $table; |
|
| 53 | 36 | } |
|
| 54 | |||
| 55 | 36 | $tableName = ucfirst(singular($tableName)); |
|
| 56 | 36 | $array = explode('.', $tableName); |
|
| 57 | |||
| 58 | 36 | return isset($array[1]) ? $array[1] : $tableName; |
|
| 59 | } |
||
| 60 | |||
| 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: