Conditions | 13 |
Paths | 111 |
Total Lines | 68 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 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 |
||
73 | public function writeItem(array $item) |
||
74 | { |
||
75 | if ($this->index == 0) { |
||
76 | if (!$this->options['columns']) { |
||
77 | $this->cols = array_keys($item); |
||
78 | } else { |
||
79 | $this->cols = $this->options['columns']; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | //raise exception on extra columns |
||
84 | if ($this->options['exception_on_extra_columns']) { |
||
85 | $extraColumns = []; |
||
86 | foreach (array_keys($item) as $itemKey) { |
||
87 | if (!in_array($itemKey, $this->cols)) { |
||
88 | $extraColumns[] = $itemKey; |
||
89 | } |
||
90 | } |
||
91 | if ($extraColumns) { |
||
92 | throw new InvalidArgumentException(sprintf( |
||
93 | 'Row %s had extra columns %s. (Defined columns: %s)', |
||
94 | $this->index + 1, |
||
95 | '"' . implode('", "', $extraColumns) . '"', |
||
96 | '"' . implode('", "', $this->cols) . '"' |
||
97 | )); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | //Fill array maintain defined order |
||
102 | foreach ($this->cols as $key => $colName) { |
||
103 | if (!array_key_exists($colName, $item)) { |
||
104 | $item[$colName] = null; |
||
105 | } |
||
106 | } |
||
107 | |||
108 | $itemOrdered = []; |
||
109 | $colOrder = array_flip($this->cols); |
||
110 | foreach ($colOrder as $colKey => $colValue) { |
||
111 | $itemOrdered[$colKey] = $item[$colKey]; |
||
112 | } |
||
113 | $item = $itemOrdered; |
||
114 | |||
115 | if ($this->index == 0) { |
||
116 | if ($this->options['drop_data']) { |
||
117 | $this->appendRow(sprintf("delete from %s\n", $this->tableName)); |
||
118 | } |
||
119 | $this->appendRow(sprintf("insert into %s (%s) values\n", $this->tableName, implode(', ', array_keys($item)))); |
||
120 | } else { |
||
121 | $this->appendRow(",\n"); |
||
122 | } |
||
123 | |||
124 | $item = array_reduce($item, function ($outCell, $currentCell) { |
||
125 | if (is_null($currentCell)) { |
||
126 | $outCell[] = 'null'; |
||
127 | |||
128 | return $outCell; |
||
129 | } |
||
130 | //escape sql string |
||
131 | $search = ['\\', "\x00", "\n", "\r", "'", '"', "\x1a"]; |
||
132 | $replace = ['\\\\','\\0','\\n', '\\r', "\'", '\"', '\\Z']; |
||
133 | $outCell[] = '"' . str_replace($search, $replace, $currentCell) . '"'; |
||
134 | |||
135 | return $outCell; |
||
136 | }, []); |
||
137 | |||
138 | $rowString = sprintf('(%s)', implode(', ', array_values($item))); |
||
139 | $this->appendRow($rowString); |
||
140 | $this->index++; |
||
141 | } |
||
170 |