| Conditions | 10 |
| Paths | 60 |
| Total Lines | 69 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | 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 |
||
| 186 | private function copyData(AbstractZohoDao $dao, $incrementalSync = true) { |
||
| 187 | $tableName = $this->getTableName($dao); |
||
| 188 | |||
| 189 | if ($incrementalSync) { |
||
| 190 | // Let's get the last modification date: |
||
| 191 | $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(lastActivityTime) FROM '.$tableName); |
||
| 192 | if ($lastActivityTime !== null) { |
||
| 193 | $lastActivityTime = new \DateTime($lastActivityTime); |
||
| 194 | } |
||
| 195 | $records = $dao->getRecords(null, null, $lastActivityTime); |
||
| 196 | } else { |
||
| 197 | $records = $dao->getRecords(); |
||
| 198 | } |
||
| 199 | |||
| 200 | |||
| 201 | $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName); |
||
| 202 | |||
| 203 | |||
| 204 | $flatFields = $this->getFlatFields($dao->getFields()); |
||
| 205 | $fieldsByName = []; |
||
| 206 | foreach ($flatFields as $field) { |
||
| 207 | $fieldsByName[$field['name']] = $field; |
||
| 208 | } |
||
| 209 | |||
| 210 | $select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id'); |
||
| 211 | |||
| 212 | $this->connection->beginTransaction(); |
||
| 213 | |||
| 214 | foreach ($records as $record) { |
||
| 215 | $data = []; |
||
| 216 | $types = []; |
||
| 217 | foreach ($table->getColumns() as $column) { |
||
| 218 | if ($column->getName() === 'id') { |
||
| 219 | continue; |
||
| 220 | } else { |
||
| 221 | $field = $fieldsByName[$column->getName()]; |
||
| 222 | $getterName = $field['getter']; |
||
| 223 | $data[$column->getName()] = $record->$getterName(); |
||
| 224 | $types[$column->getName()] = $column->getType()->getName(); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | $select->execute([ 'id' => $record->getZohoId() ]); |
||
| 229 | $result = $select->fetch(\PDO::FETCH_ASSOC); |
||
| 230 | if ($result === false) { |
||
| 231 | $data['id'] = $record->getZohoId(); |
||
| 232 | $types['id'] = 'string'; |
||
| 233 | |||
| 234 | $this->connection->insert($tableName, $data, $types); |
||
| 235 | |||
| 236 | foreach ($this->listeners as $listener) { |
||
| 237 | $listener->onInsert($data, $dao); |
||
| 238 | } |
||
| 239 | } else { |
||
| 240 | $identifier = ['id' => $record->getZohoId() ]; |
||
| 241 | $types['id'] = 'string'; |
||
| 242 | |||
| 243 | $this->connection->update($tableName, $data, $identifier, $types); |
||
| 244 | |||
| 245 | // Let's add the id for the update trigger |
||
| 246 | $data['id'] = $record->getZohoId(); |
||
| 247 | foreach ($this->listeners as $listener) { |
||
| 248 | $listener->onUpdate($data, $result, $dao); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | $this->connection->commit(); |
||
| 254 | } |
||
| 255 | |||
| 277 |
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.