Completed
Push — master ( 55e0f0...d0f882 )
by Alexey
11:22
created

LastVisitBehavior   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 24
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 6 1
A afterAction() 0 8 2
1
<?php
2
namespace modules\users\behavior;
3
4
use yii\base\Behavior;
5
use yii\console\Controller;
6
7
/**
8
 * Class LastVisitBehavior
9
 * @package modules\users\behavior
10
 */
11
class LastVisitBehavior extends Behavior
12
{
13
    /**
14
     * @return array
15
     */
16
    public function events()
17
    {
18
        return [
19
            Controller::EVENT_AFTER_ACTION => 'afterAction'
20
        ];
21
    }
22
23
    /**
24
     * @return bool
25
     */
26
    public function afterAction()
27
    {
28
        if (!\Yii::$app->user->isGuest) {
29
            $model = \Yii::$app->getUser()->getIdentity();
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...
30
            $model->touch('last_visit');
31
        }
32
        return true;
33
    }
34
}
35