SettingsAction   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 46
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 22 6
1
<?php
2
namespace pheme\settings;
3
4
use Yii;
5
use yii\base\Action;
6
7
class SettingsAction extends Action
8
{
9
    /**
10
     * @var string class name of the model which will be used to validate the attributes.
11
     * The class should have a scenario matching the `scenario` variable.
12
     * The model class must implement [[Model]].
13
     * This property must be set.
14
     */
15
    public $modelClass;
16
17
    /**
18
     * @var string The scenario this model should use to make validation
19
     */
20
    public $scenario;
21
22
    /**
23
     * @var string the name of the view to generate the form. Defaults to 'settings'.
24
     */
25
    public $viewName = 'settings';
26
27
    /**
28
     * Render the settings form.
29
     */
30
    public function run()
31
    {
32
        /* @var $model \yii\db\ActiveRecord */
33
        $model = new $this->modelClass();
34
        if ($this->scenario) {
35
            $model->setScenario($this->scenario);
36
        }
37
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
38
            foreach ($model->toArray() as $key => $value) {
39
                Yii::$app->settings->set($key, $value, $model->formName());
40
            }
41
            Yii::$app->getSession()->addFlash('success',
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
42
                Module::t('settings', 'Successfully saved settings on {section}',
43
                    ['section' => $model->formName()]
44
                )
45
            );
46
        }
47
        foreach ($model->attributes() as $key) {
48
            $model->{$key} = Yii::$app->settings->get($key, $model->formName());
49
        }
50
        return $this->controller->render($this->viewName, ['model' => $model]);
51
    }
52
}
53