We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php |
||
5 | trait AutoSet |
||
6 | { |
||
7 | // ------------------------------------------------------ |
||
8 | // AUTO-SET-FIELDS-AND-COLUMNS FUNCTIONALITY |
||
9 | // ------------------------------------------------------ |
||
10 | |||
11 | /** |
||
12 | * For a simple CRUD Panel, there should be no need to add/define the fields. |
||
13 | * The public columns in the database will be converted to be fields. |
||
14 | */ |
||
15 | 2 | public function setFromDb() |
|
16 | { |
||
17 | 2 | $this->setDoctrineTypesMapping(); |
|
18 | 2 | $this->getDbColumnTypes(); |
|
19 | |||
20 | 2 | array_map(function ($field) { |
|
21 | // $this->labels[$field] = $this->makeLabel($field); |
||
|
|||
22 | |||
23 | $new_field = [ |
||
24 | 2 | 'name' => $field, |
|
25 | 2 | 'label' => ucfirst($field), |
|
26 | 'value' => null, |
||
27 | 2 | 'default' => isset($this->db_column_types[$field]['default']) ? $this->db_column_types[$field]['default'] : null, |
|
28 | 2 | 'type' => $this->getFieldTypeFromDbColumnType($field), |
|
29 | 'values' => [], |
||
30 | 'attributes' => [], |
||
31 | 'autoset' => true, |
||
32 | ]; |
||
33 | 2 | if (! isset($this->create_fields[$field])) { |
|
34 | 2 | $this->create_fields[$field] = $new_field; |
|
35 | } |
||
36 | 2 | if (! isset($this->update_fields[$field])) { |
|
37 | 2 | $this->update_fields[$field] = $new_field; |
|
38 | } |
||
39 | |||
40 | 2 | if (! in_array($field, $this->model->getHidden()) && ! isset($this->columns[$field])) { |
|
41 | 2 | $this->addColumn([ |
|
42 | 2 | 'name' => $field, |
|
43 | 2 | 'label' => ucfirst($field), |
|
44 | 2 | 'type' => $this->getFieldTypeFromDbColumnType($field), |
|
45 | 'autoset' => true, |
||
46 | ]); |
||
47 | } |
||
48 | 2 | }, $this->getDbColumnsNames()); |
|
49 | 2 | } |
|
50 | |||
51 | /** |
||
52 | * Get all columns from the database for that table. |
||
53 | * |
||
54 | * @return [array] |
||
55 | */ |
||
56 | 3 | public function getDbColumnTypes() |
|
70 | |||
71 | /** |
||
72 | * Intuit a field type, judging from the database column type. |
||
73 | * |
||
74 | * @param [string] Field name. |
||
75 | * |
||
76 | * @return [string] Fielt type. |
||
77 | */ |
||
78 | 65 | public function getFieldTypeFromDbColumnType($field) |
|
79 | { |
||
80 | 65 | if (! array_key_exists($field, $this->db_column_types)) { |
|
81 | 63 | return 'text'; |
|
82 | } |
||
83 | |||
84 | 2 | if ($field == 'password') { |
|
85 | return 'password'; |
||
86 | } |
||
87 | |||
88 | 2 | if ($field == 'email') { |
|
89 | return 'email'; |
||
90 | } |
||
91 | |||
92 | 2 | switch ($this->db_column_types[$field]['type']) { |
|
93 | case 'int': |
||
94 | case 'smallint': |
||
95 | case 'mediumint': |
||
96 | case 'longint': |
||
97 | return 'number'; |
||
98 | break; |
||
99 | |||
100 | case 'string': |
||
101 | case 'varchar': |
||
102 | case 'set': |
||
103 | 2 | return 'text'; |
|
104 | break; |
||
105 | |||
106 | // case 'enum': |
||
107 | // return 'enum'; |
||
108 | // break; |
||
109 | |||
110 | case 'tinyint': |
||
111 | return 'active'; |
||
112 | break; |
||
113 | |||
114 | case 'text': |
||
115 | 2 | return 'textarea'; |
|
116 | break; |
||
117 | |||
118 | case 'mediumtext': |
||
119 | case 'longtext': |
||
120 | return 'textarea'; |
||
121 | break; |
||
122 | |||
123 | case 'date': |
||
124 | 2 | return 'date'; |
|
125 | break; |
||
126 | |||
127 | case 'datetime': |
||
128 | case 'timestamp': |
||
129 | 2 | return 'datetime'; |
|
130 | break; |
||
131 | case 'time': |
||
132 | 2 | return 'time'; |
|
133 | break; |
||
134 | |||
135 | default: |
||
136 | 2 | return 'text'; |
|
137 | break; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | // Fix for DBAL not supporting enum |
||
142 | 2 | public function setDoctrineTypesMapping() |
|
152 | |||
153 | /** |
||
154 | * Turn a database column name or PHP variable into a pretty label to be shown to the user. |
||
155 | * |
||
156 | * @param [string] |
||
157 | * |
||
158 | * @return [string] |
||
159 | */ |
||
160 | 8 | public function makeLabel($value) |
|
164 | |||
165 | /** |
||
166 | * Get the database column names, in order to figure out what fields/columns to show in the auto-fields-and-columns functionality. |
||
167 | * |
||
168 | * @return [array] Database column names as an array. |
||
169 | */ |
||
170 | 3 | public function getDbColumnsNames() |
|
183 | } |
||
184 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.