m170211_140247_create_item_table   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 21 2
A down() 0 5 1
1
<?php
2
3
use yii\db\Schema;
4
5
class m170211_140247_create_item_table extends \yii\db\Migration
6
{
7
    public function up()
8
    {
9
        $tableOptions = null;
10
        if ($this->db->driverName === 'mysql') {
11
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';
12
        }
13
        $this->createTable('{{%item}}', [
14
            'id' => Schema::TYPE_PK,
15
            'item_name' => Schema::TYPE_STRING . '(125) NOT NULL COMMENT "商品名称"',
16
            "subtitle" => Schema::TYPE_STRING . '(125) NOT NULL COMMENT "副标题"',
17
            'categories' => Schema::TYPE_STRING . ' NOT NULL COMMENT "商品类别"',
18
            'market_price' => Schema::TYPE_DECIMAL . '(10,2) NOT NULL DEFAULT 0 COMMENT "市场价"',
19
            'price' => Schema::TYPE_DECIMAL . '(10,2) NOT NULL DEFAULT 0 COMMENT "价格"',
20
            'description' => Schema::TYPE_TEXT . ' NOT NULL COMMENT "商品属性"',
21
            'logo_image' => Schema::TYPE_STRING . ' NOT NULL DEFAULT "/images/icon-image.png" COMMENT "缩略图"',
22
            'status' => Schema::TYPE_BOOLEAN . ' NOT NULL DEFAULT 1 COMMENT "状态"',
23
            'created_at' => Schema::TYPE_INTEGER . ' NOT NULL COMMENT "创建时间"',
24
            'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL COMMENT "更新时间"',
25
        ], $tableOptions);
26
        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::up 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...
27
    }
28
29
    public function down()
30
    {
31
        $this->dropTable('{{%item}}');
32
        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...
33
    }
34
}
35