Total Complexity | 50 |
Total Lines | 277 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 |
||
11 | abstract class TableModel extends Crudites |
||
12 | { |
||
13 | |||
14 | private SchemaInterface $schema; |
||
15 | private string $table; |
||
16 | |||
17 | |||
18 | //check all primary keys are set (FIXME that doesn't work unles AIPK.. nice try) |
||
19 | public function isNew(): bool |
||
20 | { |
||
21 | $match = static::table()->primaryKeysMatch(get_object_vars($this)); |
||
22 | return empty($match); |
||
23 | } |
||
24 | |||
25 | public function getId($mode = null) |
||
26 | { |
||
27 | $primary_key = $this->schema->autoIncrementedPrimaryKey(static::table()); |
||
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 |
||
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 | |||
80 | // relational mapping |
||
81 | public static function table(): string |
||
82 | { |
||
83 | $reflectionClass = new \ReflectionClass(get_called_class()); |
||
84 | $called_class = new \ReflectionClass(get_called_class()); |
||
85 | |||
86 | $table_name = $reflectionClass->getConstant('TABLE_NAME'); |
||
87 | |||
88 | if ($table_name === false) { |
||
89 | $shortName = $reflectionClass->getShortName(); |
||
90 | $table_name = defined($const_name = 'TABLE_' . strtoupper($shortName)) ? constant($const_name) : strtolower($shortName); |
||
91 | } |
||
92 | |||
93 | return $table_name; |
||
94 | $table_name = $called_class->getConstant('TABLE_NAME'); |
||
95 | if($table_name !== false) |
||
96 | return $table_name; |
||
97 | |||
98 | $class_name = $called_class->getShortName(); |
||
99 | |||
100 | if (defined($const_name = 'TABLE_' . strtoupper($class_name))) |
||
101 | return constant($const_name); |
||
102 | |||
103 | return strtolower($class_name); |
||
104 | } |
||
105 | |||
106 | public function to_table_row($operator_id = null) |
||
107 | { |
||
108 | if (!is_null($operator_id) && $this->isNew() && is_null($this->get('created_by'))) { |
||
109 | $this->set('created_by', $operator_id); |
||
110 | } |
||
111 | |||
112 | $model_data = get_object_vars($this); |
||
113 | |||
114 | if($this->isNew()){ |
||
115 | // $table_row = new Row($this, $model_data); |
||
116 | $table_row = new Row($this->schema, static::table(), $model_data); |
||
117 | } |
||
118 | else{ |
||
119 | $table_row = new Row($this->schema); |
||
120 | $table_row->load($model_data); |
||
121 | } |
||
122 | // 1. Produce OR restore a row |
||
123 | |||
124 | // 2. Apply alterations from form_model data |
||
125 | $table_row->alter($model_data); |
||
126 | |||
127 | return $table_row; |
||
128 | } |
||
129 | |||
130 | // success: return PK-indexed array of results (associative array or object) |
||
131 | public static function retrieve(SelectInterface $select): array |
||
154 | } |
||
155 | |||
156 | |||
157 | private static function actionnableParams($arg1, $arg2 = null): array |
||
192 | } |
||
193 | |||
194 | |||
195 | /** |
||
196 | * Retrieve a single instance of the model by its unique identifier(s). |
||
197 | * Throws CruditesException if the unique identifier yields no or multiple instances. |
||
198 | * |
||
199 | * @param mixed $arg1 The value of the primary key or an array of column-value pairs. |
||
200 | * @param mixed|null $arg2 The value of the primary key if $arg1 is a string, otherwise null. |
||
201 | * |
||
202 | * @return mixed The retrieved instance of the model. |
||
203 | * |
||
204 | * @throws CruditesException If the arguments are not actionable, the unique identifier is not found, or multiple instances are found. |
||
205 | * |
||
206 | * USAGE |
||
207 | * Case 1: Class::one($primary_key_value) |
||
208 | * Case 2: Class::one($unique_column, $value) |
||
209 | * Case 3: Class::one([$unique_column => $value, $unique_column2 => $value2]) |
||
210 | * |
||
211 | */ |
||
212 | public static function one($arg1, $arg2 = null): self |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Attempts to retrieve a single instance of the model by its unique identifier(s). |
||
234 | * If no instance is found, returns null. |
||
235 | */ |
||
236 | |||
237 | public static function exists($arg1, $arg2 = null): ?self |
||
238 | { |
||
239 | try { |
||
240 | return self::one($arg1, $arg2); |
||
241 | } |
||
242 | catch (CruditesException $e) { |
||
243 | return null; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | public static function any($field_exact_values=[], $options = []) |
||
248 | { |
||
249 | $select = static::filter($field_exact_values, $options); |
||
250 | return static::retrieve($select); |
||
251 | } |
||
252 | |||
253 | |||
254 | public static function filter($filters = [], $options = []): SelectInterface |
||
258 | |||
259 | // $query = static::query_retrieve($filters, $options); |
||
260 | // return static::retrieve($query); |
||
261 | } |
||
262 | |||
263 | public static function count($filters = [], $options = []): int |
||
270 | } |
||
271 | |||
272 | public static function listing($filters = [], $options = []): array |
||
273 | { |
||
274 | return static::retrieve(static::filter($filters, $options)); // listing as arrays for templates |
||
275 | } |
||
276 | |||
277 | |||
278 | |||
279 | public static function get_many_by_AIPK($aipk_values): ?array |
||
290 |
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