| Conditions | 3 |
| Paths | 18 |
| Total Lines | 84 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 32 | public function up() |
||
| 33 | { |
||
| 34 | $tableOptions = null; |
||
| 35 | if ($this->db->driverName === 'mysql') { |
||
| 36 | $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'; |
||
| 37 | } |
||
| 38 | |||
| 39 | $now = $this->mysql('CURRENT_TIMESTAMP', "'now'"); |
||
| 40 | $on_update_now = $this->mysql("ON UPDATE $now"); |
||
| 41 | |||
| 42 | $transaction = $this->db->beginTransaction(); |
||
| 43 | try { |
||
| 44 | $this->createTable('{{%oauth_clients}}', [ |
||
| 45 | 'client_name' => Schema::TYPE_STRING . '(36) NOT NULL COMMENT "客户端名称"', |
||
| 46 | 'client_icon' => $this->string()->comment('图标'), |
||
| 47 | 'client_id' => Schema::TYPE_STRING . '(36) NOT NULL COMMENT "客户端ID"', |
||
| 48 | 'client_secret' => Schema::TYPE_STRING . '(32) DEFAULT NULL COMMENT "客户端密钥"', |
||
| 49 | 'redirect_uri' => Schema::TYPE_STRING . '(1000) NOT NULL DEFAULT "" COMMENT "回跳地址"', |
||
| 50 | 'grant_types' => Schema::TYPE_STRING . '(100) NOT NULL COMMENT "授权类型"', |
||
| 51 | 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "权限范围"', |
||
| 52 | 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL COMMENT "用户"', |
||
| 53 | $this->primaryKey('client_id'), |
||
| 54 | ], $tableOptions); |
||
| 55 | |||
| 56 | $this->createTable('{{%oauth_access_tokens}}', [ |
||
| 57 | 'access_token' => Schema::TYPE_STRING . '(40) NOT NULL COMMENT "访问令牌"', |
||
| 58 | 'client_id' => Schema::TYPE_STRING . '(36) NOT NULL COMMENT "客户端ID"', |
||
| 59 | 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL COMMENT "用户"', |
||
| 60 | 'expires' => Schema::TYPE_TIMESTAMP . " NOT NULL DEFAULT $now $on_update_now COMMENT '期限'", |
||
| 61 | 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "权限范围"', |
||
| 62 | $this->primaryKey('access_token'), |
||
| 63 | $this->foreignKey('client_id', '{{%oauth_clients}}', 'client_id', 'CASCADE', 'CASCADE'), |
||
| 64 | ], $tableOptions); |
||
| 65 | |||
| 66 | $this->createTable('{{%oauth_refresh_tokens}}', [ |
||
| 67 | 'refresh_token' => Schema::TYPE_STRING . '(40) NOT NULL COMMENT "刷新令牌"', |
||
| 68 | 'client_id' => Schema::TYPE_STRING . '(36) NOT NULL COMMENT "客户端ID"', |
||
| 69 | 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL COMMENT "用户"', |
||
| 70 | 'expires' => Schema::TYPE_TIMESTAMP . " NOT NULL DEFAULT $now $on_update_now COMMENT '期限'", |
||
| 71 | 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "权限范围"', |
||
| 72 | $this->primaryKey('refresh_token'), |
||
| 73 | $this->foreignKey('client_id', '{{%oauth_clients}}', 'client_id', 'CASCADE', 'CASCADE'), |
||
| 74 | ], $tableOptions); |
||
| 75 | |||
| 76 | $this->createTable('{{%oauth_authorization_codes}}', [ |
||
| 77 | 'authorization_code' => Schema::TYPE_STRING . '(40) NOT NULL COMMENT "授权码"', |
||
| 78 | 'client_id' => Schema::TYPE_STRING . '(36) NOT NULL COMMENT "客户端ID"', |
||
| 79 | 'user_id' => Schema::TYPE_INTEGER . ' DEFAULT NULL COMMENT "用户"', |
||
| 80 | 'redirect_uri' => Schema::TYPE_STRING . '(1000) NOT NULL COMMENT "回跳地址"', |
||
| 81 | 'expires' => Schema::TYPE_TIMESTAMP . " NOT NULL DEFAULT $now $on_update_now COMMENT '期限'", |
||
| 82 | 'scope' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "权限范围"', |
||
| 83 | $this->primaryKey('authorization_code'), |
||
| 84 | $this->foreignKey('client_id', '{{%oauth_clients}}', 'client_id', 'CASCADE', 'CASCADE'), |
||
| 85 | ], $tableOptions); |
||
| 86 | |||
| 87 | $this->createTable('{{%oauth_scopes}}', [ |
||
| 88 | 'scope' => Schema::TYPE_STRING . '(2000) NOT NULL COMMENT "权限范围"', |
||
| 89 | 'is_default' => Schema::TYPE_BOOLEAN . ' NOT NULL COMMENT "默认"', |
||
| 90 | ], $tableOptions); |
||
| 91 | |||
| 92 | $this->createTable('{{%oauth_jwt}}', [ |
||
| 93 | 'client_id' => Schema::TYPE_STRING . '(36) NOT NULL COMMENT "客户端ID"', |
||
| 94 | 'subject' => Schema::TYPE_STRING . '(80) DEFAULT NULL COMMENT "接收者"', |
||
| 95 | 'public_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "公钥"', |
||
| 96 | $this->primaryKey('client_id'), |
||
| 97 | ], $tableOptions); |
||
| 98 | |||
| 99 | $this->createTable('{{%oauth_public_keys}}', [ |
||
| 100 | 'client_id' => Schema::TYPE_STRING . '(255) NOT NULL COMMENT "客户端ID"', |
||
| 101 | 'public_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "公钥"', |
||
| 102 | 'private_key' => Schema::TYPE_STRING . '(2000) DEFAULT NULL COMMENT "私钥"', |
||
| 103 | 'encryption_algorithm' => Schema::TYPE_STRING . '(100) DEFAULT \'RS256\' COMMENT "加密算法"', |
||
| 104 | ], $tableOptions); |
||
| 105 | |||
| 106 | $transaction->commit(); |
||
| 107 | } catch (Exception $e) { |
||
| 108 | echo 'Exception: ' . $e->getMessage() . '\n'; |
||
| 109 | $transaction->rollback(); |
||
| 110 | |||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | return true; |
||
| 115 | } |
||
| 116 | |||
| 143 |
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:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.