Conditions | 12 |
Paths | 17 |
Total Lines | 39 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
111 | public function afterSave(ModelInterface $model, string $field, bool $rawSql): void |
||
112 | { |
||
113 | if (!$this->getProgress() && $model->hasSnapshotData() && $model->hasUpdated($field)) { |
||
114 | self::staticStart(); |
||
115 | |||
116 | $snapshot = $model->getOldSnapshotData() ?: $model->getSnapshotData(); |
||
117 | $modelPosition = $model->readAttribute($field); |
||
118 | $modelPrimaryKeys = $model->getPrimaryKeysValues(); |
||
119 | |||
120 | if (!($modelPosition instanceof RawValue)) { |
||
121 | $uField = Text::uncamelize($field); // @todo use columnMap |
||
122 | $updatePositionQuery = null; |
||
123 | |||
124 | if ($snapshot[$field] > $modelPosition) { |
||
125 | $updatePositionQuery = $rawSql |
||
126 | ? 'UPDATE `' . $model->getSource() . '` SET `' . $uField . '` = `' . $uField . '`+1 WHERE `' . $uField . '` >= :position and `' . $uField . '` < :oldPosition and `' . $idField . '` <> :id' |
||
127 | : 'UPDATE [' . get_class($model) . '] SET [' . $field . '] = [' . $field . ']+1 WHERE [' . $field . '] >= ?1 and [' . $field . '] < ?2 and [' . $idField . '] <> ?0'; |
||
128 | } |
||
129 | elseif ($snapshot[$field] < $modelPosition) { |
||
130 | $updatePositionQuery = $rawSql |
||
131 | ? 'UPDATE `' . $model->getSource() . '` SET `' . $uField . '` = `' . $uField . '`-1 WHERE `' . $uField . '` > :oldPosition and `' . $uField . '` <= :position and `' . $idField . '` <> :id' |
||
132 | : 'UPDATE [' . get_class($model) . '] SET [' . $field . '] = [' . $field . ']-1 WHERE [' . $field . '] > ?2 and [' . $field . '] <= ?1 and [' . $idField . '] <> ?0'; |
||
133 | } |
||
134 | |||
135 | if (!empty($updatePositionQuery)) { |
||
136 | if ($rawSql) { |
||
137 | $model->getWriteConnection()->query($updatePositionQuery, [ |
||
138 | 'primaryKeys' => $modelPrimaryKeys, |
||
139 | 'position' => $modelPosition, |
||
140 | 'oldPosition' => $snapshot[$field], |
||
141 | ]); |
||
142 | } |
||
143 | else { |
||
144 | $model->getModelsManager()->executeQuery($updatePositionQuery, [$modelId, $modelPosition, $snapshot[$field]]); |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | |||
149 | self::staticStop(); |
||
150 | } |
||
153 |