FavoriteController::actionDelete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 27.02.2018
6
 */
7
8
namespace app\modules\admin\controllers;
9
10
11
use app\models\Favorite;
12
use Yii;
13
use yii\filters\VerbFilter;
14
use yii\helpers\ArrayHelper;
15
use yii\web\Controller;
16
17
class FavoriteController extends Controller
18
{
19
    public function behaviors()
20
    {
21
        return ArrayHelper::merge(parent::behaviors(), [
22
            'verb' => [
23
                'class' => VerbFilter::class,
24
                'actions' => [
25
                    'delete' => ['POST'],
26
                    'create' => ['POST'],
27
                ],
28
            ],
29
        ]);
30
    }
31
32
    public function actionCreate()
33
    {
34
        $request = Yii::$app->request;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->request can also be of type yii\web\Request. However, the property $request is declared as type yii\console\Request. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
35
        $model = new Favorite();
36
        $model->load($request->post(), $request->isAjax ? '' : null);
37
        $model->user_id = Yii::$app->user->id;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->user->id can also be of type string. However, the property $user_id is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
38
        $model->label = $request->post('prefix', '') . $model->label;
0 ignored issues
show
Bug introduced by
Are you sure $request->post('prefix', '') of type array|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        $model->label = /** @scrutinizer ignore-type */ $request->post('prefix', '') . $model->label;
Loading history...
39
40
        if ($model->save()) {
41
            Yii::$app->session->setFlash('success', 'OK!');
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            Yii::$app->session->/** @scrutinizer ignore-call */ 
42
                                setFlash('success', 'OK!');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
        } else {
43
44
            Yii::$app->session->setFlash('error', 'ERROR!');
45
        }
46
47
        if ($request->isAjax) {
48
            return $model->url;
49
        }
50
51
        return $this->redirect($model->url);
52
    }
53
54
    public function actionDelete($id)
55
    {
56
        Favorite::deleteAll([
57
            'user_id' => Yii::$app->user->id,
58
            'id' => $id,
59
        ]);
60
    }
61
}