| Conditions | 19 |
| Paths | 54 |
| Total Lines | 129 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 66 | private function unifiedAction($id, $importMode) |
||
| 67 | { |
||
| 68 | $object = BaseObject::findById($id); |
||
| 69 | /* @var $className \app\modules\data\models\Import */ |
||
| 70 | $className = |
||
| 71 | $importMode ? |
||
| 72 | 'app\modules\data\models\Import' : |
||
| 73 | 'app\modules\data\models\Export'; |
||
| 74 | |||
| 75 | if ($object !== null) { |
||
| 76 | $model = new ImportModel(['object' => $id]); |
||
| 77 | |||
| 78 | $fields = \app\modules\data\components\Import::getFields($model->object); |
||
| 79 | |||
| 80 | |||
| 81 | if (\Yii::$app->request->isPost) { |
||
| 82 | if ( |
||
| 83 | $model->load(\Yii::$app->request->post()) && $model->validate() |
||
| 84 | ) { |
||
| 85 | $import = $className::find()->where( |
||
| 86 | [ |
||
| 87 | 'user_id' => Yii::$app->user->id, |
||
| 88 | 'object_id' => $id, |
||
| 89 | ] |
||
| 90 | )->one(); |
||
| 91 | |||
| 92 | if ($import === null) { |
||
| 93 | $import = new $className( |
||
| 94 | [ |
||
| 95 | 'user_id' => Yii::$app->user->id, |
||
| 96 | 'object_id' => $id, |
||
| 97 | ] |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | $import->filename = null; |
||
| 102 | if ($importMode === true) { |
||
| 103 | $file = UploadedFile::getInstance($model, 'file'); |
||
| 104 | $model->type = $file->extension; |
||
| 105 | $filename = $model->getFilename('Import'); |
||
| 106 | $import->filename = $filename; |
||
| 107 | $fullFilename = |
||
| 108 | Yii::$app->getModule('data')->importDir . |
||
| 109 | '/' . |
||
| 110 | $filename; |
||
| 111 | if ($file->saveAs($fullFilename) === false) { |
||
| 112 | throw new ErrorException("Unable to save file"); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $import->status = $className::STATUS_PROCESS; |
||
| 117 | |||
| 118 | $task_options = Yii::$app->request->post('Task', []); |
||
| 119 | |||
| 120 | if ($import->save()) { |
||
| 121 | BackgroundTasks::addTask([ |
||
| 122 | 'name' => |
||
| 123 | $importMode ? 'import' : 'export', |
||
| 124 | 'description' => |
||
| 125 | ($importMode ? 'import' : 'export') . |
||
| 126 | " {$model->object}", |
||
| 127 | 'action' => |
||
| 128 | 'data/file/' . |
||
| 129 | ($importMode ? 'import' : 'export'), |
||
| 130 | 'params' => |
||
| 131 | $model->serialize(), |
||
| 132 | 'init_event' => |
||
| 133 | ($importMode ? 'import' : 'export'), |
||
| 134 | ], |
||
| 135 | [ |
||
| 136 | 'create_notification' => isset($task_options['create_notification']) && 1 == $task_options['create_notification'] ? true : false, |
||
| 137 | ]); |
||
| 138 | \Yii::$app->session->setFlash( |
||
| 139 | 'info', |
||
| 140 | \Yii::t('app', 'Task is queued. Come back later.') |
||
| 141 | ); |
||
| 142 | } else { |
||
| 143 | \Yii::$app->session->setFlash( |
||
| 144 | 'error', |
||
| 145 | \Yii::t('app', 'Import Error') |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | return $this->redirect(['/data/file']); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | $availablePropertyGroups = []; |
||
| 156 | |||
| 157 | /** @var \app\modules\shop\models\Product $exampleModel - product for example */ |
||
| 158 | $exampleModel = new $object->object_class; |
||
| 159 | if ($exampleModel->hasMethod('getPropertyGroups')) { |
||
| 160 | $availablePropertyGroups = $exampleModel->getPropertyGroups( |
||
| 161 | false, |
||
| 162 | true |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | |||
| 166 | \Yii::$app->session->setFlash( |
||
| 167 | 'info', |
||
| 168 | \Yii::t('app', 'Specify fields to import and select the file') |
||
| 169 | ); |
||
| 170 | |||
| 171 | $fields['additionalFields'] = []; |
||
| 172 | if ($exampleModel instanceof ExportableInterface) { |
||
| 173 | $fields['additionalFields'] = $exampleModel::exportableAdditionalFields(); |
||
| 174 | } |
||
| 175 | |||
| 176 | if ($model->type === null) { |
||
| 177 | $model->type = Yii::$app->modules['data']->defaultType; |
||
| 178 | } |
||
| 179 | |||
| 180 | return $this->render('import-export', [ |
||
| 181 | 'model' => $model, |
||
| 182 | 'object' => $object, |
||
| 183 | 'fields' => $fields, |
||
| 184 | 'availablePropertyGroups' => $availablePropertyGroups, |
||
| 185 | 'importMode' => $importMode, |
||
| 186 | ]); |
||
| 187 | } else { |
||
| 188 | \Yii::$app->session->setFlash( |
||
| 189 | 'error', |
||
| 190 | \Yii::t('app', 'Object not found') |
||
| 191 | ); |
||
| 192 | return $this->redirect(['/data/file']); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 249 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.