SignupAction   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 8.62 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 5 26 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace yii2mod\user\actions;
4
5
use Yii;
6
use yii\web\Response;
7
use yii\widgets\ActiveForm;
8
use yii2mod\user\traits\EventTrait;
9
10
/**
11
 * Class SignupAction
12
 *
13
 * @package yii2mod\user\actions
14
 */
15
class SignupAction extends Action
16
{
17
    use EventTrait;
18
19
    /**
20
     * Event is triggered after creating SignupForm class.
21
     * Triggered with \yii2mod\user\events\FormEvent.
22
     */
23
    const EVENT_BEFORE_SIGNUP = 'beforeSignup';
24
25
    /**
26
     * Event is triggered after successful signup.
27
     * Triggered with \yii2mod\user\events\FormEvent.
28
     */
29
    const EVENT_AFTER_SIGNUP = 'afterSignup';
30
31
    /**
32
     * @var string name of the view, which should be rendered
33
     */
34
    public $view = '@vendor/yii2mod/yii2-user/views/signup';
35
36
    /**
37
     * @var string signup form class
38
     */
39
    public $modelClass = 'yii2mod\user\models\SignupForm';
40
41
    /**
42
     * Signup a user.
43
     *
44
     * @return string|\yii\web\Response
45
     */
46
    public function run()
47
    {
48
        $model = Yii::createObject($this->modelClass);
49
        $event = $this->getFormEvent($model);
50
51
        $this->trigger(self::EVENT_BEFORE_SIGNUP, $event);
52
53
        $load = $model->load(Yii::$app->request->post());
54
55 View Code Duplication
        if (Yii::$app->request->isAjax) {
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...
56
            Yii::$app->response->format = Response::FORMAT_JSON;
57
58
            return ActiveForm::validate($model);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \yii\widgets\ActiveForm::validate($model); (array) is incompatible with the return type documented by yii2mod\user\actions\SignupAction::run of type string|yii\web\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
59
        }
60
61
        if ($load && ($user = $model->signup()) !== null) {
62
            $this->trigger(self::EVENT_AFTER_SIGNUP, $event);
63
            if (Yii::$app->getUser()->login($user)) {
64
                return $this->redirectTo(Yii::$app->getUser()->getReturnUrl());
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...
65
            }
66
        }
67
68
        return $this->controller->render($this->view, [
69
            'model' => $model,
70
        ]);
71
    }
72
}
73