Completed
Push — master ( ccc91d...b5de45 )
by Andrey
04:35
created

ActiveRecord::scenarios()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Itstructure\AdminModule\models;
4
5
use yii\behaviors\TimestampBehavior;
6
use yii\db\Expression;
7
use Itstructure\AdminModule\interfaces\ModelInterface;
8
9
/**
10
 * Class ActiveRecord
11
 *
12
 * @package Itstructure\AdminModule\models
13
 *
14
 * @author Andrey Girnik <[email protected]>
15
 */
16
class ActiveRecord extends \yii\db\ActiveRecord
17
{
18
    /**
19
     * Connect behavior to the basic model.
20
     *
21
     * @return array
22
     */
23
    public function behaviors()
24
    {
25
        $behaviors = parent::behaviors();
26
        $behaviors[] = [
27
            'class'              => TimestampBehavior::class,
28
            'createdAtAttribute' => 'created_at',
29
            'updatedAtAttribute' => 'updated_at',
30
            'value'              => new Expression('NOW()'),
31
        ];
32
        return $behaviors;
33
    }
34
35
    /**
36
     * Scenarios.
37
     *
38
     * @return array
39
     */
40 View Code Duplication
    public function scenarios()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
41
    {
42
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(\Itstructur...> $this->attributes()); (array<*,integer[]>) is incompatible with the return type of the parent method yii\base\Model::scenarios of type array<string,array>.

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...
43
            ModelInterface::SCENARIO_CREATE => $this->attributes(),
44
            ModelInterface::SCENARIO_UPDATE => $this->attributes(),
45
            self::SCENARIO_DEFAULT => $this->attributes(),
46
        ];
47
    }
48
}
49