Passed
Push — master ( 17b30a...9c28de )
by Mihail
14:15
created

ActionApprove   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B approve() 0 27 5
1
<?php
2
3
namespace Apps\Controller\Front\User;
4
5
use Apps\ActiveRecord\User;
6
use Apps\Model\Front\User\FormLogin;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Exception\ForbiddenException;
10
use Ffcms\Core\Helper\Type\Str;
11
use Ffcms\Core\Network\Request;
12
use Ffcms\Core\Network\Response;
13
14
/**
15
 * Trait ActionApprove
16
 * @package Apps\Controller\Front\User
17
 * @property View $view
18
 * @property Request $request
19
 * @property Response $response
20
 */
21
trait ActionApprove
22
{
23
24
    /**
25
     * Approve user profile via $email and $token params
26
     * @param string $email
27
     * @param string $token
28
     * @throws ForbiddenException
29
     */
30
    public function approve($email, $token)
31
    {
32
        // validate token length and email format
33
        if (App::$User->isAuth() || Str::length($token) < 32 || !Str::isEmail($email)) {
34
            throw new ForbiddenException(__('Wrong recovery data'));
35
        }
36
37
        // lets find token&email
38
        /** @var User $user */
39
        $user = App::$User->where('approve_token', $token)
0 ignored issues
show
Bug introduced by
The method where does only exist in Apps\ActiveRecord\User, but not in Ffcms\Core\Interfaces\iUser.

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...
40
            ->where('email', '=', $email)
41
            ->first();
42
43
        // check if record is exist by token and email
44
        if (!$user) {
45
            throw new ForbiddenException();
46
        }
47
48
        // update approve_token value to confirmed
49
        $user->approve_token = '0';
50
        $user->save();
51
52
        // open session and redirect to main
53
        $loginModel = new FormLogin();
54
        $loginModel->openSession($user);
55
        $this->response->redirect('/'); // session is opened, refresh page
56
    }
57
}
58