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
|
|||
35 | $model = new Favorite(); |
||
36 | $model->load($request->post(), $request->isAjax ? '' : null); |
||
37 | $model->user_id = Yii::$app->user->id; |
||
0 ignored issues
–
show
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 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;
}
![]() |
|||
38 | $model->label = $request->post('prefix', '') . $model->label; |
||
39 | |||
40 | if ($model->save()) { |
||
41 | Yii::$app->session->setFlash('success', 'OK!'); |
||
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 | } |
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 theid
property of an instance of theAccount
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.