Completed
Push — master ( 8fbfef...c68ddf )
by Revin
02:53
created

Module   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 8
c 7
b 0
f 2
lcom 2
cbo 6
dl 0
loc 105
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 12 2
A instance() 0 4 1
A defineModelClasses() 0 8 1
A getDefaultModels() 0 8 1
A model() 0 17 3
1
<?php
2
/**
3
 * Module.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.ru
6
 */
7
8
namespace rmrevin\yii\module\Comments;
9
10
use rmrevin\yii\module\Comments\forms\CommentCreateForm;
11
use rmrevin\yii\module\Comments\models\Comment;
12
use rmrevin\yii\module\Comments\models\queries\CommentQuery;
13
use yii\helpers\ArrayHelper;
14
15
/**
16
 * Class Module
17
 * @package rmrevin\yii\module\Comments
18
 */
19
class Module extends \yii\base\Module
20
{
21
22
    /** @var string module name */
23
    public static $moduleName = 'comments';
24
25
    /** @var string|null */
26
    public $userIdentityClass = null;
27
28
    /** @var bool */
29
    public $useRbac = true;
30
31
    /**
32
     * Array that will store the models used in the package
33
     * e.g. :
34
     * [
35
     *     'Comment' => 'frontend/models/comments/CommentModel'
36
     * ]
37
     *
38
     * The classes defined here will be merged with getDefaultModels()
39
     * having he manually defined by the user preference.
40
     *
41
     * @var array
42
     */
43
    public $modelMap = [];
44
45
    public function init()
46
    {
47
        parent::init();
48
49
        if ($this->userIdentityClass === null) {
50
            $this->userIdentityClass = \Yii::$app->getUser()->identityClass;
0 ignored issues
show
Bug introduced by
The method getUser 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...
51
        }
52
53
        // Merge the default model classes
54
        // with the user defined ones.
55
        $this->defineModelClasses();
56
    }
57
58
    /**
59
     * @return static
60
     */
61
    public static function instance()
62
    {
63
        return \Yii::$app->getModule(static::$moduleName);
64
    }
65
66
    /**
67
     * Merges the default and user defined model classes
68
     * Also let's the developer to set new ones with the
69
     * parameter being those the ones with most preference.
70
     *
71
     * @param array $modelClasses
72
     */
73
    public function defineModelClasses($modelClasses = [])
74
    {
75
        $this->modelMap = ArrayHelper::merge(
76
            $this->getDefaultModels(),
77
            $this->modelMap,
78
            $modelClasses
79
        );
80
    }
81
82
    /**
83
     * Get default model classes
84
     */
85
    protected function getDefaultModels()
86
    {
87
        return [
88
            'Comment' => Comment::className(),
89
            'CommentQuery' => CommentQuery::className(),
90
            'CommentCreateForm' => CommentCreateForm::className()
91
        ];
92
    }
93
94
    /**
95
     * Get defined className of model
96
     *
97
     * Returns an string or array compatible
98
     * with the Yii::createObject method.
99
     *
100
     * @param string $name
101
     * @param array $config // You should never send an array with a key defined as "class" since this will
102
     *                      // overwrite the main className defined by the system.
103
     * @return string|array
104
     */
105
    public function model($name, $config = [])
106
    {
107
        $modelData = $this->modelMap[ucfirst($name)];
108
109
        if (!empty($config)) {
110
            if (is_string($modelData)) {
111
                $modelData = ['class' => $modelData];
112
            }
113
114
            $modelData = ArrayHelper::merge(
115
                $modelData,
116
                $config
117
            );
118
        }
119
120
        return $modelData;
121
    }
122
123
}