Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
53 | class InsertUpdateTable extends DbRecord implements TargetInterface |
||
54 | { |
||
55 | /** |
||
56 | * @var int |
||
57 | */ |
||
58 | protected $identifierField; |
||
59 | |||
60 | /** |
||
61 | * @param Strategy $strategy |
||
62 | */ |
||
63 | public function start(Strategy $strategy) |
||
76 | |||
77 | /** |
||
78 | * Process every entry in the .csv |
||
79 | * |
||
80 | * @param array $entry |
||
81 | * |
||
82 | * @return int|void |
||
83 | */ |
||
84 | public function processEntry(array $entry) |
||
110 | |||
111 | /** |
||
112 | * |
||
113 | * Fetch all records from the target table, where the PID equals the PID specified |
||
114 | * in the target section of the strategy |
||
115 | * |
||
116 | * @param $selectFields |
||
117 | * @return array |
||
118 | */ |
||
119 | protected function getRecords($selectFields) |
||
137 | |||
138 | /** |
||
139 | * Insert record into the target table which you have specified in the target section of the strategy |
||
140 | * |
||
141 | * @param array $entry |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | protected function insertRecord(array $entry) |
||
146 | { |
||
147 | $field_values = []; |
||
148 | $into_table = $this->getConfiguration()['target_table']; |
||
149 | |||
150 | foreach ($this->getConfiguration()["mapping"] as $key => $value) { |
||
151 | $field_values[$value] = $entry[$key]; |
||
152 | } |
||
153 | |||
154 | View Code Duplication | if ($this->getConfiguration()["salt_password"] == 1) { |
|
155 | $field_values["password"] = $this->saltPassword($field_values["password"]); |
||
156 | } |
||
157 | |||
158 | $field_values['pid'] = $this->getConfiguration()['pid']; |
||
159 | $time = time(); |
||
160 | $field_values['tstamp'] = $time; |
||
161 | $field_values['crdate'] = $time; |
||
162 | |||
163 | Utility::getDatabaseConnection()->exec_INSERTquery($into_table, $field_values); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Update a record in the target table which you have specified in the |
||
168 | * target section of the strategy (don't update the password) |
||
169 | * |
||
170 | * @param array $entry |
||
171 | * |
||
172 | * @return void |
||
173 | */ |
||
174 | protected function updateRecord(array $entry) |
||
175 | { |
||
176 | $into_table = $this->getConfiguration()['target_table']; |
||
177 | $fieldName = $this->getConfiguration()['mapping'][$this->identifierField]; |
||
178 | $whereStatement = "pid = '" . $this->getConfiguration()['pid'] . "' AND " . $fieldName . " = '" . $entry[$this->identifierField] . "'"; |
||
179 | |||
180 | $tmp_arr = []; |
||
181 | |||
182 | foreach ($this->getConfiguration()["mapping"] as $key => $value) { |
||
183 | $tmp_arr[$value] = $entry[$key]; |
||
184 | } |
||
185 | |||
186 | View Code Duplication | if ($this->getConfiguration()["salt_password"] == 1) { |
|
187 | $tmp_arr['password'] = $this->saltPassword($tmp_arr['password']); |
||
188 | } |
||
189 | |||
190 | $field_values = $this->duplicateArray($tmp_arr, $this->getConfiguration()['exclude_from_update']); |
||
191 | $field_values['tstamp'] = time(); |
||
192 | |||
193 | Utility::getDatabaseConnection()->exec_UPDATEquery($into_table, $whereStatement, $field_values); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * This function creates a duplicate of a associative array and optionally removes |
||
198 | * any entries which are also elements of a second array |
||
199 | * |
||
200 | * @param array $arr |
||
201 | * @param array $exclude_arr |
||
202 | * |
||
203 | * @return array |
||
204 | */ |
||
205 | protected function duplicateArray(array $arr, array $exclude_arr = null) |
||
219 | |||
220 | /** |
||
221 | * This function takes a password as argument, salts it and returns the new password. |
||
222 | * |
||
223 | * @param string $password |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | protected function saltPassword($password) |
||
228 | { |
||
229 | if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('saltedpasswords')) { |
||
230 | $saltedpasswordsInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(null, 'FE'); |
||
231 | $password = $saltedpasswordsInstance->getHashedPassword($password); |
||
232 | |||
233 | if ($this->isValidMd5($password)) { |
||
234 | $password = 'M' . $password; |
||
235 | } |
||
236 | } |
||
237 | |||
238 | return $password; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * This function checks if a password is in md5 format. |
||
243 | * |
||
244 | * @param string $md5 |
||
245 | * |
||
246 | * @return int |
||
247 | */ |
||
248 | protected function isValidMd5($md5 = '') |
||
252 | |||
253 | /** |
||
254 | * |
||
255 | */ |
||
256 | public function end() |
||
260 | } |
||
261 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.