Create   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 68.75%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 5
c 5
b 0
f 1
lcom 1
cbo 5
dl 59
loc 59
ccs 11
cts 16
cp 0.6875
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A newModel() 4 4 1
A run() 21 21 4

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * @link https://github.com/roboapp
4
 * @copyright Copyright (c) 2016 Roboapp
5
 * @license [MIT License](https://opensource.org/licenses/MIT)
6
 */
7
8
namespace roboapp\crud;
9
10
use yii\base\InvalidConfigException;
11
use yii\base\Model;
12
use yii\db\ActiveRecord;
13
use yii\web\Response;
14
use yii\widgets\ActiveForm;
15
16
/**
17
 * Action supports creation of the new model using web form.
18
 *
19
 * @author iRipVanWinkle <[email protected]>
20
 * @since 1.0
21
 */
22 View Code Duplication
class Create extends BaseAction
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
{
24
    /**
25
     * @var string the scenario to be assigned to the new model
26
     */
27
    public $scenario = Model::SCENARIO_DEFAULT;
28
29
    /**
30
     * @var string name of the view
31
     */
32
    public $view = 'create';
33
34
    /**
35
     * @var string|array|callable a PHP callable that will be called to create the new model or class name
36
     * The callable should return the new model instance.
37
     */
38
    public $newModel;
39
40
    /**
41
     * @var string the name variable model in the view template
42
     */
43
    public $nameVariableModel = 'model';
44
45
    /**
46
     * Creates new model instance.
47
     * @return ActiveRecord new model instance.
48
     * @throws InvalidConfigException on invalid configuration.
49
     */
50 18
    public function newModel()
51
    {
52 18
        return \Yii::createObject($this->newModel);
53
    }
54 18
55
    /**
56
     * Creates new record.
57
     * @return mixed response
58
     */
59 18
    public function run()
60 3
    {
61 3
        $model = $this->newModel();
62
        $model->setScenario($this->scenario);
63
        if ($model->load(\Yii::$app->getRequest()->post())) {
64 15
            if (\Yii::$app->getRequest()->isAjax) {
65
                \Yii::$app->getResponse()->format = Response::FORMAT_JSON;
66
                return ActiveForm::validate($model);
67
            }
68
            if ($model->save()) {
69
                $this->addSuccessFlash();
70 15
                return $this->redirect($model);
71
            } else {
72
                $this->addErrorFlash();
73
            }
74
        }
75
76
        return $this->controller->render($this->view, [
77 18
            $this->nameVariableModel => $model,
78
        ]);
79 18
    }
80
}
81