| Total Complexity | 51 |
| Total Lines | 284 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Complex classes like TableModel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TableModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | abstract class TableModel extends Crudites |
||
| 11 | { |
||
| 12 | |||
| 13 | protected static TableInterface $table; |
||
| 14 | |||
| 15 | |||
| 16 | //check all primary keys are set (FIXME that doesn't work unles AIPK.. nice try) |
||
| 17 | public function isNew(): bool |
||
| 18 | { |
||
| 19 | $match = static::table()->primaryKeysMatch(get_object_vars($this)); |
||
| 20 | return empty($match); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function getId($mode = null) |
||
| 24 | { |
||
| 25 | trigger_error('getId() is deprecated and should not be used anymore. Please use the id() or pk() instead.', E_USER_DEPRECATED); |
||
| 26 | |||
| 27 | $primary_key = static::table()->autoIncrementedPrimaryKey(); |
||
| 28 | if (is_null($primary_key) && count($pks = static::table()->primaryKeys()) == 1) { |
||
| 29 | $primary_key = current($pks); |
||
| 30 | } |
||
| 31 | |||
| 32 | return $mode === 'name' ? $primary_key->name() : $this->get($primary_key->name()); |
||
| 33 | } |
||
| 34 | |||
| 35 | public function pk() |
||
| 36 | { |
||
| 37 | $primary_key = static::table()->autoIncrementedPrimaryKey(); |
||
| 38 | if (is_null($primary_key) && count($pks = static::table()->primaryKeys()) == 1) { |
||
| 39 | $primary_key = current($pks); |
||
| 40 | } |
||
| 41 | |||
| 42 | return $this->get($primary_key->name()); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function id() |
||
| 46 | { |
||
| 47 | return $this->get('id'); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function get($prop_name) |
||
| 51 | { |
||
| 52 | if (property_exists($this, $prop_name)) { |
||
| 53 | return $this->$prop_name; |
||
| 54 | } |
||
| 55 | |||
| 56 | return null; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function set($prop_name, $value): void |
||
| 60 | { |
||
| 61 | $this->$prop_name = $value; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function import($assoc_data): self |
||
| 65 | { |
||
| 66 | if (!is_array($assoc_data)) { |
||
| 67 | throw new \Exception(__FUNCTION__ . '(assoc_data) parm is not an array'); |
||
| 68 | } |
||
| 69 | |||
| 70 | // shove it all up in model, god will sort them out |
||
| 71 | foreach ($assoc_data as $field => $value) { |
||
| 72 | $this->set($field, $value); |
||
| 73 | } |
||
| 74 | |||
| 75 | return $this; |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 79 | public static function table(): TableInterface |
||
| 80 | { |
||
| 81 | return static::$database->table(static::relationalMappingName()); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The relationalMappingName() function returns a string, the the name of |
||
| 86 | * the table in the database that corresponds to the current model |
||
| 87 | * |
||
| 88 | * The function returns one of the following: |
||
| 89 | * - The value of the TABLE_NAME constant, if it is defined in the model class |
||
| 90 | * - The value of a constant named TABLE_<ClassName>, if it is defined() |
||
| 91 | * - The lowercase name of the model class if neither constants are defined |
||
| 92 | */ |
||
| 93 | public static function relationalMappingName(): string |
||
| 94 | { |
||
| 95 | $reflectionClass = new \ReflectionClass(get_called_class()); |
||
| 96 | $called_class = new \ReflectionClass(get_called_class()); |
||
| 97 | |||
| 98 | $table_name = $reflectionClass->getConstant('TABLE_NAME'); |
||
| 99 | |||
| 100 | if ($table_name === false) { |
||
| 101 | $shortName = $reflectionClass->getShortName(); |
||
| 102 | $table_name = defined($const_name = 'TABLE_' . strtoupper($shortName)) ? constant($const_name) : strtolower($shortName); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $table_name; |
||
| 106 | $table_name = $called_class->getConstant('TABLE_NAME'); |
||
| 107 | if($table_name !== false) |
||
| 108 | return $table_name; |
||
| 109 | |||
| 110 | $class_name = $called_class->getShortName(); |
||
| 111 | |||
| 112 | if (defined($const_name = 'TABLE_' . strtoupper($class_name))) |
||
| 113 | return constant($const_name); |
||
| 114 | |||
| 115 | return strtolower($class_name); |
||
| 116 | } |
||
| 117 | |||
| 118 | |||
| 119 | public function to_table_row($operator_id = null) |
||
| 120 | { |
||
| 121 | if (!is_null($operator_id) && $this->isNew() && is_null($this->get('created_by'))) { |
||
| 122 | $this->set('created_by', $operator_id); |
||
| 123 | } |
||
| 124 | |||
| 125 | $model_data = get_object_vars($this); |
||
| 126 | |||
| 127 | // 1. Produce OR restore a row |
||
| 128 | $table_row = $this->isNew() ? static::table()->produce($model_data) : static::table()->restore($model_data); |
||
| 129 | |||
| 130 | // 2. Apply alterations from form_model data |
||
| 131 | $table_row->alter($model_data); |
||
| 132 | |||
| 133 | return $table_row; |
||
| 134 | } |
||
| 135 | |||
| 136 | // success: return PK-indexed array of results (associative array or object) |
||
| 137 | public static function retrieve(SelectInterface $select): array |
||
| 160 | } |
||
| 161 | |||
| 162 | |||
| 163 | private static function actionnableParams($arg1, $arg2 = null): array |
||
| 198 | } |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * Retrieve a single instance of the model by its unique identifier(s). |
||
| 203 | * Throws CruditesException if the unique identifier yields no or multiple instances. |
||
| 204 | * |
||
| 205 | * @param mixed $arg1 The value of the primary key or an array of column-value pairs. |
||
| 206 | * @param mixed|null $arg2 The value of the primary key if $arg1 is a string, otherwise null. |
||
| 207 | * |
||
| 208 | * @return mixed The retrieved instance of the model. |
||
| 209 | * |
||
| 210 | * @throws CruditesException If the arguments are not actionable, the unique identifier is not found, or multiple instances are found. |
||
| 211 | * |
||
| 212 | * USAGE |
||
| 213 | * Case 1: Class::one($primary_key_value) |
||
| 214 | * Case 2: Class::one($unique_column, $value) |
||
| 215 | * Case 3: Class::one([$unique_column => $value, $unique_column2 => $value2]) |
||
| 216 | * |
||
| 217 | */ |
||
| 218 | public static function one($arg1, $arg2 = null): self |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Attempts to retrieve a single instance of the model by its unique identifier(s). |
||
| 240 | * If no instance is found, returns null. |
||
| 241 | */ |
||
| 242 | |||
| 243 | public static function exists($arg1, $arg2 = null): ?self |
||
| 244 | { |
||
| 245 | try { |
||
| 246 | return self::one($arg1, $arg2); |
||
| 247 | } |
||
| 248 | catch (CruditesException $e) { |
||
| 249 | return null; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | public static function any($field_exact_values=[], $options = []) |
||
| 254 | { |
||
| 255 | $select = static::filter($field_exact_values, $options); |
||
| 256 | return static::retrieve($select); |
||
| 257 | } |
||
| 258 | |||
| 259 | |||
| 260 | public static function filter($filters = [], $options = []): SelectInterface |
||
| 264 | |||
| 265 | // $query = static::query_retrieve($filters, $options); |
||
| 266 | // return static::retrieve($query); |
||
| 267 | } |
||
| 268 | |||
| 269 | public static function count($filters = [], $options = []): int |
||
| 276 | } |
||
| 277 | |||
| 278 | public static function listing($filters = [], $options = []): array |
||
| 279 | { |
||
| 280 | return static::retrieve(static::filter($filters, $options)); // listing as arrays for templates |
||
| 281 | } |
||
| 282 | |||
| 283 | |||
| 284 | |||
| 285 | public static function get_many_by_AIPK($aipk_values): ?array |
||
| 286 | { |
||
| 296 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths