| Conditions | 17 |
| Paths | 72 |
| Total Lines | 55 |
| 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 |
||
| 42 | public function importDataToDB($reader, $model, $columns, $key, $status, $notNullColumnNames) { |
||
| 43 | |||
| 44 | $rows = $reader->toArray(); |
||
| 45 | $newData = array(); |
||
| 46 | $updatedData = array(); |
||
| 47 | |||
| 48 | // Check validation of values |
||
| 49 | foreach ($rows as $i => $row) { |
||
| 50 | foreach ($notNullColumnNames as $notNullColumn) { |
||
| 51 | if (!isset($row[$notNullColumn])) { |
||
| 52 | unset($rows[$i]); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | if (!$this->failed) { |
||
| 58 | if ($status == 1) { |
||
| 59 | $model->truncate(); |
||
| 60 | } |
||
| 61 | foreach ($rows as $row) { |
||
| 62 | if (!empty($row[$key])) { |
||
| 63 | $exists = $model->where($key, '=', $row[$key])->count(); |
||
| 64 | if (!$exists) { |
||
| 65 | $values = array(); |
||
| 66 | foreach ($columns as $col) { |
||
| 67 | if ($col != $key) { |
||
| 68 | $values[$col] = $row[$col]; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | $newData[] = $values; |
||
| 72 | } else if ($status == 2 && $exists) { |
||
| 73 | $values = array(); |
||
| 74 | foreach ($columns as $col) { |
||
| 75 | $values[$col] = $row[$col]; |
||
| 76 | } |
||
| 77 | $updatedData[] = $values; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | // insert data into table |
||
| 84 | if (!empty($newData)) { |
||
| 85 | $model->insert($newData); |
||
| 86 | } |
||
| 87 | |||
| 88 | // update available data |
||
| 89 | if (!empty($updatedData)) { |
||
| 90 | foreach ($updatedData as $data) { |
||
| 91 | $keyValue = $data[$key]; |
||
| 92 | unset($data[$key]); |
||
| 93 | $model->where($key, $keyValue)->update($data); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: