m160523_164512_activeuser_init   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 59
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B up() 0 43 1
A down() 0 11 2
1
<?php
2
3
4
use inblank\activeuser\migrations\Migration;
5
use yii\db\Schema;
6
7
class m160523_164512_activeuser_init extends Migration
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    public function up()
10
    {
11
        // Accounts
12
        $tab = self::tn(self::TAB_USERS);
13
        $this->createTable($tab, [
14
            'id' => Schema::TYPE_PK,
15
            'status' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 0',
16
17
            'email' => Schema::TYPE_STRING . "(200) NOT NULL DEFAULT ''",
18
            'pass_hash' => Schema::TYPE_STRING . "(60) NOT NULL DEFAULT ''",
19
20
            'name' => Schema::TYPE_STRING . "(200) NOT NULL DEFAULT ''",
21
            'gender' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 0',
22
            'birth' => Schema::TYPE_DATE . " DEFAULT NULL",
23
            'avatar' => Schema::TYPE_STRING . "(45) NOT NULL DEFAULT ''",
24
25
            'access_token' => Schema::TYPE_STRING . "(40) DEFAULT NULL",
26
            'auth_key' => Schema::TYPE_STRING . "(40) DEFAULT NULL",
27
28
            'token' => Schema::TYPE_STRING . "(40) DEFAULT NULL",
29
            'token_created_at' => Schema::TYPE_INTEGER . " NOT NULL DEFAULT 0",
30
31
            'registered_at' => Schema::TYPE_DATETIME . ' DEFAULT NULL',
32
        ], $this->tableOptions);
33
        $this->createIndex('unique_email', $tab, 'email', true);
34
        $this->createIndex('unique_access_token', $tab, 'access_token', true);
35
        $this->createIndex('unique_auth_key', $tab, 'auth_key', true);
36
        $this->createIndex('unique_token', $tab, 'token', true);
37
38
        // Accounts profiles
39
        $tab = self::tn(self::TAB_PROFILES);
40
        $this->createTable($tab, [
41
            'user_id' => Schema::TYPE_PK,
42
            'site' => Schema::TYPE_STRING . "(255) NOT NULL DEFAULT ''",
43
            'location' => Schema::TYPE_STRING . "(255) NOT NULL DEFAULT ''",
44
        ], $this->tableOptions);
45
        $this->addForeignKey(
46
            self::fk(self::TAB_PROFILES, self::TAB_USERS),
47
            $tab, 'user_id',
48
            self::tn(self::TAB_USERS), 'id',
49
            'CASCADE', 'RESTRICT'
50
        );
51
    }
52
53
    public function down()
54
    {
55
        $tables = [
56
            self::TAB_PROFILES,
57
            self::TAB_USERS,
58
        ];
59
        foreach ($tables as $table) {
60
            $this->dropTable(self::tn($table));
61
        }
62
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type of the parent method yii\db\Migration::down of type false|null.

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...
63
    }
64
65
}
66