| Conditions | 15 |
| Paths | 600 |
| Total Lines | 105 |
| Code Lines | 66 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 74 | public function fetchFromZoho(AbstractZohoDao $dao, $incrementalSync = true, $twoWaysSync = true) |
||
| 75 | { |
||
| 76 | $tableName = ZohoDatabaseHelper::getTableName($dao, $this->prefix); |
||
| 77 | |||
| 78 | if ($incrementalSync) { |
||
| 79 | $this->logger->info("Copying incremental data for '$tableName'"); |
||
| 80 | //We need to check $lastActivity column exist. |
||
| 81 | /* @var $existedColumns \Doctrine\DBAL\Schema\Column[] */ |
||
| 82 | $existedColumns = $this->connection->getSchemaManager()->listTableColumns($tableName); |
||
| 83 | $existLastActivityTime = false; |
||
| 84 | foreach ($existedColumns as $existedColumn) { |
||
| 85 | if($existedColumn->getName() == 'lastActivityTime'){ |
||
| 86 | $existLastActivityTime = true; |
||
| 87 | break; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | // To avoid checking this column when it is not used in a table. |
||
| 91 | if($existLastActivityTime){ |
||
| 92 | /////'SHOW COLUMNS FROM '.$tableName.' LIKE `lastActivityTime`'); |
||
| 93 | // Let's get the last modification date: |
||
| 94 | $lastActivityTime = $this->connection->fetchColumn('SELECT MAX(lastActivityTime) FROM '.$tableName); |
||
| 95 | if ($lastActivityTime !== null) { |
||
| 96 | $lastActivityTime = new \DateTime($lastActivityTime); |
||
| 97 | $this->logger->info('Last activity time: '.$lastActivityTime->format('c')); |
||
| 98 | // Let's add one second to the last activity time (otherwise, we are fetching again the last record in DB). |
||
| 99 | $lastActivityTime->add(new \DateInterval('PT1S')); |
||
| 100 | } |
||
| 101 | |||
| 102 | $records = $dao->getRecords(null, null, $lastActivityTime); |
||
| 103 | $deletedRecordIds = $dao->getDeletedRecordIds($lastActivityTime); |
||
| 104 | } |
||
| 105 | } else { |
||
| 106 | $this->logger->notice("Copying FULL data for '$tableName'"); |
||
| 107 | $records = $dao->getRecords(); |
||
| 108 | $deletedRecordIds = []; |
||
| 109 | } |
||
| 110 | $this->logger->info('Fetched '.count($records).' records'); |
||
|
|
|||
| 111 | |||
| 112 | $table = $this->connection->getSchemaManager()->createSchema()->getTable($tableName); |
||
| 113 | |||
| 114 | $flatFields = ZohoDatabaseHelper::getFlatFields($dao->getFields()); |
||
| 115 | $fieldsByName = []; |
||
| 116 | foreach ($flatFields as $field) { |
||
| 117 | $fieldsByName[$field['name']] = $field; |
||
| 118 | } |
||
| 119 | |||
| 120 | $select = $this->connection->prepare('SELECT * FROM '.$tableName.' WHERE id = :id'); |
||
| 121 | |||
| 122 | $this->connection->beginTransaction(); |
||
| 123 | |||
| 124 | foreach ($records as $record) { |
||
| 125 | $data = []; |
||
| 126 | $types = []; |
||
| 127 | foreach ($table->getColumns() as $column) { |
||
| 128 | if (in_array($column->getName(), ['id', 'uid'])) { |
||
| 129 | continue; |
||
| 130 | } else { |
||
| 131 | $field = $fieldsByName[$column->getName()]; |
||
| 132 | $getterName = $field['getter']; |
||
| 133 | $data[$column->getName()] = $record->$getterName(); |
||
| 134 | $types[$column->getName()] = $column->getType()->getName(); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | $select->execute(['id' => $record->getZohoId()]); |
||
| 139 | $result = $select->fetch(\PDO::FETCH_ASSOC); |
||
| 140 | if ($result === false) { |
||
| 141 | $this->logger->debug("Inserting record with ID '".$record->getZohoId()."'."); |
||
| 142 | |||
| 143 | $data['id'] = $record->getZohoId(); |
||
| 144 | $types['id'] = 'string'; |
||
| 145 | |||
| 146 | $this->connection->insert($tableName, $data, $types); |
||
| 147 | |||
| 148 | foreach ($this->listeners as $listener) { |
||
| 149 | $listener->onInsert($data, $dao); |
||
| 150 | } |
||
| 151 | } else { |
||
| 152 | $this->logger->debug("Updating record with ID '".$record->getZohoId()."'."); |
||
| 153 | $identifier = ['id' => $record->getZohoId()]; |
||
| 154 | $types['id'] = 'string'; |
||
| 155 | |||
| 156 | $this->connection->update($tableName, $data, $identifier, $types); |
||
| 157 | |||
| 158 | // Let's add the id for the update trigger |
||
| 159 | $data['id'] = $record->getZohoId(); |
||
| 160 | foreach ($this->listeners as $listener) { |
||
| 161 | $listener->onUpdate($data, $result, $dao); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | $sqlStatementUid = 'select uid from '.$this->connection->quoteIdentifier($tableName).' where id = :id'; |
||
| 166 | foreach ($deletedRecordIds as $id) { |
||
| 167 | $uid = $this->connection->fetchColumn($sqlStatementUid, ['id' => $id]); |
||
| 168 | $this->connection->delete($tableName, ['id' => $id]); |
||
| 169 | if ($twoWaysSync) { |
||
| 170 | // TODO: we could detect if there are changes to be updated to the server and try to warn with a log message |
||
| 171 | // Also, let's remove the newly created field (because of the trigger) to avoid looping back to Zoho |
||
| 172 | $this->connection->delete('local_delete', ['table_name' => $tableName, 'id' => $id]); |
||
| 173 | $this->connection->delete('local_update', ['table_name' => $tableName, 'uid' => $uid]); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | $this->connection->commit(); |
||
| 178 | } |
||
| 179 | } |
||
| 180 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: