PasswordResetAction::run()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 21

Duplication

Lines 6
Ratio 28.57 %

Importance

Changes 0
Metric Value
dl 6
loc 21
rs 9.2728
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
3
namespace yii2mod\user\actions;
4
5
use Yii;
6
use yii\base\InvalidParamException;
7
use yii\web\BadRequestHttpException;
8
use yii2mod\user\traits\EventTrait;
9
10
/**
11
 * Class PasswordResetAction
12
 *
13
 * @package yii2mod\user\actions
14
 */
15
class PasswordResetAction extends Action
16
{
17
    use EventTrait;
18
19
    /**
20
     * Event is triggered before resetting password.
21
     * Triggered with \yii2mod\user\events\FormEvent.
22
     */
23
    const EVENT_BEFORE_RESET = 'beforeReset';
24
25
    /**
26
     * Event is triggered after resetting password.
27
     * Triggered with \yii2mod\user\events\FormEvent.
28
     */
29
    const EVENT_AFTER_RESET = 'afterReset';
30
31
    /**
32
     * @var string name of the view, which should be rendered
33
     */
34
    public $view = '@vendor/yii2mod/yii2-user/views/resetPassword';
35
36
    /**
37
     * @var string reset password model class
38
     */
39
    public $modelClass = 'yii2mod\user\models\ResetPasswordForm';
40
41
    /**
42
     * @var string message to be set on success
43
     */
44
    public $successMessage = 'New password was saved.';
45
46
    /**
47
     * Reset password for a user.
48
     *
49
     * @param $token
50
     *
51
     * @return string|\yii\web\Response
52
     *
53
     * @throws \yii\web\BadRequestHttpException
54
     */
55
    public function run($token)
56
    {
57
        try {
58
            $model = Yii::createObject($this->modelClass, [$token]);
59
            $event = $this->getFormEvent($model);
60
            $this->trigger(self::EVENT_BEFORE_RESET, $event);
61
        } catch (InvalidParamException $e) {
62
            throw new BadRequestHttpException($e->getMessage());
63
        }
64
65 View Code Duplication
        if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
            $this->trigger(self::EVENT_AFTER_RESET, $event);
67
            Yii::$app->getSession()->setFlash('success', $this->successMessage);
0 ignored issues
show
Bug introduced by
The method getSession 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...
68
69
            return $this->redirectTo(Yii::$app->getHomeUrl());
0 ignored issues
show
Bug introduced by
The method getHomeUrl 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...
70
        }
71
72
        return $this->controller->render($this->view, [
73
            'model' => $model,
74
        ]);
75
    }
76
}
77