Conditions | 22 |
Paths | 448 |
Total Lines | 131 |
Code Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
83 | public function __construct(array $params = []) |
||
84 | { |
||
85 | $f3 = \Base::instance(); |
||
86 | |||
87 | $this->db = \Registry::get('db'); |
||
88 | |||
89 | // guess the table name from the class name if not specified as a class member |
||
90 | $class = strrchr(get_class($this), '\\'); |
||
91 | $class = \UTF::instance()->substr($class,1); |
||
92 | if (empty($this->table)) { |
||
93 | $table = $f3->snakecase($class); |
||
94 | } else { |
||
95 | $table = $this->table; |
||
96 | } |
||
97 | $this->table = $table; // this gets quoted |
||
98 | $this->mapperName = $table; // this doesn't |
||
99 | |||
100 | parent::__construct($this->db, $table); |
||
101 | |||
102 | foreach ($params as $k => $v) { |
||
103 | $this->$k = $v; |
||
104 | } |
||
105 | |||
106 | // work out default validation rules from schema and cache them |
||
107 | $validationRules = []; |
||
108 | foreach ($this->schema() as $field => $metadata) { |
||
109 | if ('id' == $field) { |
||
110 | continue; |
||
111 | } |
||
112 | |||
113 | $validationRules[$field] = ''; |
||
114 | $rules = []; |
||
115 | |||
116 | if (empty($metadata['nullable']) || !empty($metadata['pkey'])) { |
||
117 | // special case, id for internal use so we don't interfere with this |
||
118 | $rules[] = 'required'; |
||
119 | } |
||
120 | |||
121 | if (preg_match('/^(?<type>[^(]+)\(?(?<length>[^)]+)?/i', $metadata['type'], $matches)) { |
||
122 | switch ($matches['type']) { |
||
123 | case 'char': |
||
124 | case 'varchar': |
||
125 | $rules[] = 'max_len,' . $matches['length']; |
||
126 | break; |
||
127 | |||
128 | case 'text': |
||
129 | $rules[] = 'max_len,65535'; |
||
130 | break; |
||
131 | |||
132 | case 'int': |
||
133 | $rules[] = 'integer|min_numeric,0'; |
||
134 | break; |
||
135 | |||
136 | case 'datetime': |
||
137 | $rules[] = 'date|min_len,0|max_len,19'; |
||
138 | break; |
||
139 | |||
140 | default: |
||
141 | break; |
||
142 | } |
||
143 | $validationRules[$field] = empty($rules) ? '' : join('|', $rules); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | // set default validation rules |
||
148 | foreach ($this->validationRules as $field => $rule) { |
||
149 | if (!empty($rule)) { |
||
150 | if (empty($validationRules[$field])) { |
||
151 | $validationRules[$field] = $rule; |
||
152 | } else { |
||
153 | $validationRules[$field] .= '|' . $rule; |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | |||
158 | // save default validation rules and filter rules in-case we add rules |
||
159 | $this->validationRulesDefault = $validationRules; |
||
160 | $this->validationRules = $validationRules; |
||
161 | $this->filterRulesDefault = $this->filterRules; |
||
162 | |||
163 | // set original data when object loaded |
||
164 | $this->onload(function($mapper){ |
||
165 | $mapper->originalData = $mapper->cast(); |
||
166 | }); |
||
167 | |||
168 | // filter data, set UUID and date created before insert |
||
169 | $this->beforeinsert(function($mapper){ |
||
170 | $mapper->setUUID($mapper->uuidField); |
||
171 | $mapper->copyFrom($mapper->filter()); |
||
172 | if (in_array('created', $mapper->fields()) && empty($mapper->created)) { |
||
173 | $mapper->created = Helpers\Time::database(); |
||
174 | } |
||
175 | return $mapper->validate(); |
||
176 | }); |
||
177 | |||
178 | // filter data, set updated field if present before update |
||
179 | $this->beforeupdate(function($mapper){ |
||
180 | $mapper->copyFrom($mapper->filter()); |
||
181 | return $mapper->validate(); |
||
182 | }); |
||
183 | |||
184 | // write audit data after save |
||
185 | $this->aftersave(function($mapper){ |
||
186 | if ('audit' == $mapper->mapperName) { |
||
187 | return; |
||
188 | } |
||
189 | $data = array_merge([ |
||
190 | 'event' => (empty($mapper->originalData) ? 'created-' : 'updated-') . $mapper->mapperName, |
||
191 | 'old' => $mapper->originalData, |
||
192 | 'new' => $mapper->cast() |
||
193 | ], $this->auditData); |
||
194 | Models\Audit::instance()->write($data); |
||
195 | $mapper->originalData = $data['new']; |
||
196 | $mapper->auditData = []; |
||
197 | }); |
||
198 | |||
199 | // write audit data after erase |
||
200 | $this->aftererase(function($mapper){ |
||
201 | if ('audit' == $mapper->mapperName) { |
||
202 | return; |
||
203 | } |
||
204 | $data = array_merge([ |
||
205 | 'event' => 'deleted-' . $mapper->mapperName, |
||
206 | 'old' => $mapper->originalData, |
||
207 | 'new' => $mapper->cast() |
||
208 | ], $this->auditData); |
||
209 | Models\Audit::instance()->write($data); |
||
210 | $mapper->originalData = $mapper->auditData = []; |
||
211 | }); |
||
212 | |||
213 | } |
||
214 | |||
379 |