| 1 | <?php |
||
| 15 | trait DatabaseTrait |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var \CI_DB |
||
| 19 | */ |
||
| 20 | protected $db; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var \CI_DB_result |
||
| 24 | */ |
||
| 25 | protected $query; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Parses the table name from Describe class. |
||
| 29 | * |
||
| 30 | * @param string|\CI_Model $table |
||
| 31 | * @param boolean $isForeignKey |
||
| 32 | * @return string |
||
| 33 | */ |
||
| 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 | |||
| 61 | /** |
||
| 62 | * Sets the database class. |
||
| 63 | * |
||
| 64 | * @param \CI_DB|null $database |
||
| 65 | * @return self |
||
| 66 | */ |
||
| 67 | 39 | public function setDatabase($database = null) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Sets the query result. |
||
| 88 | * |
||
| 89 | * @param \CI_DB_result $query |
||
| 90 | * @return self |
||
| 91 | */ |
||
| 92 | 3 | public function setQuery($query) |
|
| 98 | } |
||
| 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: