CommentCreateForm::save()   C
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 38
ccs 0
cts 26
cp 0
rs 5.3846
cc 8
eloc 22
nc 7
nop 0
crap 72
1
<?php
2
/**
3
 * CommentCreateForm.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.ru
6
 */
7
8
namespace rmrevin\yii\module\Comments\forms;
9
10
use rmrevin\yii\module\Comments;
11
12
/**
13
 * Class CommentCreateForm
14
 * @package rmrevin\yii\module\Comments\forms
15
 */
16
class CommentCreateForm extends \yii\base\Model
17
{
18
19
    public $id;
20
    public $entity;
21
    public $from;
22
    public $text;
23
24
    /** @var Comments\models\Comment */
25
    public $Comment;
26
27
    public function init()
28
    {
29
        $Comment = $this->Comment;
30
31
        if (false === $this->Comment->isNewRecord) {
32
            $this->id = $Comment->id;
33
            $this->entity = $Comment->entity;
34
            $this->from = $Comment->from;
35
            $this->text = $Comment->text;
36
        } elseif (!\Yii::$app->getUser()->getIsGuest()) {
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...
37
            $User = \Yii::$app->getUser()->getIdentity();
38
39
            $this->from = $User instanceof Comments\interfaces\CommentatorInterface
40
                ? $User->getCommentatorName()
41
                : null;
42
        }
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function rules()
49
    {
50
        $CommentModelClassName = Comments\Module::instance()->model('comment');
51
52
        return [
53
            [['entity', 'text'], 'required'],
54
            [['entity', 'from', 'text'], 'string'],
55
            [['id'], 'integer'],
56
            [['id'], 'exist', 'targetClass' => $CommentModelClassName, 'targetAttribute' => 'id'],
57
        ];
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function attributeLabels()
64
    {
65
        return [
66
            'entity' => \Yii::t('app', 'Entity'),
67
            'from' => \Yii::t('app', 'Your name'),
68
            'text' => \Yii::t('app', 'Text'),
69
        ];
70
    }
71
72
    /**
73
     * @return bool
74
     * @throws \yii\web\NotFoundHttpException
75
     */
76
    public function save()
77
    {
78
        $Comment = $this->Comment;
79
80
        $CommentModelClassName = Comments\Module::instance()->model('comment');
81
82
        if (empty($this->id)) {
83
            $Comment = \Yii::createObject($CommentModelClassName);
84
        } elseif ($this->id > 0 && $Comment->id !== $this->id) {
85
            /** @var Comments\models\Comment $CommentModel */
86
            $CommentModel = \Yii::createObject($CommentModelClassName);
87
            $Comment = $CommentModel::find()
88
                ->byId($this->id)
89
                ->one();
90
91
            if (!($Comment instanceof Comments\models\Comment)) {
92
                throw new \yii\web\NotFoundHttpException;
93
            }
94
        }
95
96
        $Comment->entity = $this->entity;
97
        $Comment->from = $this->from;
98
        $Comment->text = $this->text;
99
100
        $result = $Comment->save();
101
102
        if ($Comment->hasErrors()) {
103
            foreach ($Comment->getErrors() as $attribute => $messages) {
104
                foreach ($messages as $mes) {
105
                    $this->addError($attribute, $mes);
106
                }
107
            }
108
        }
109
110
        $this->Comment = $Comment;
111
112
        return $result;
113
    }
114
}
115