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 |
||
| 20 | class ContentBlock extends \yii\db\ActiveRecord |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var null|string |
||
| 25 | */ |
||
| 26 | public $newGroup = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @inheritdoc |
||
| 30 | */ |
||
| 31 | public static function tableName() |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @inheritdoc |
||
| 38 | */ |
||
| 39 | public function rules() |
||
| 40 | { |
||
| 41 | return [ |
||
| 42 | [['value'], 'string'], |
||
| 43 | [['preload', 'group_id'], 'integer'], |
||
| 44 | [['key'], 'unique'], |
||
| 45 | [['group_id'], 'default', 'value' => 1], |
||
| 46 | [['name', 'key'], 'string', 'max' => 255], |
||
| 47 | [['newGroup'], 'safe'] |
||
| 48 | ]; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @inheritdoc |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function attributeLabels() |
|
| 55 | { |
||
| 56 | return [ |
||
| 57 | 'id' => 'ID', |
||
| 58 | 'name' => Yii::t('app', 'Name'), |
||
| 59 | 'key' => Yii::t('app', 'Key'), |
||
| 60 | 'value' => Yii::t('app', 'Value'), |
||
| 61 | 'preload' => Yii::t('app', 'Preload'), |
||
| 62 | 'group_id' => Yii::t('app', 'Group Id'), |
||
| 63 | 'newGroup' => Yii::t('app', 'New Group'), |
||
| 64 | ]; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritdoc |
||
| 69 | */ |
||
| 70 | public function behaviors() |
||
| 71 | { |
||
| 72 | return [ |
||
| 73 | [ |
||
| 74 | 'class' => ActiveRecordHelper::className(), |
||
| 75 | ], |
||
| 76 | ]; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param $params |
||
| 81 | * @return ActiveDataProvider |
||
| 82 | */ |
||
| 83 | public function search($params) |
||
| 84 | { |
||
| 85 | /* @var $query \yii\db\ActiveQuery */ |
||
| 86 | $query = self::find(); |
||
| 87 | |||
| 88 | if ($this->group_id) { |
||
| 89 | $query->andWhere(['group_id' => $this->group_id]); |
||
| 90 | } |
||
| 91 | |||
| 92 | $dataProvider = new ActiveDataProvider( |
||
| 93 | [ |
||
| 94 | 'query' => $query, |
||
| 95 | 'pagination' => [ |
||
| 96 | 'pageSize' => 10, |
||
| 97 | ], |
||
| 98 | ] |
||
| 99 | ); |
||
| 100 | if (!($this->load($params))) { |
||
| 101 | return $dataProvider; |
||
| 102 | } |
||
| 103 | $query->andFilterWhere(['id' => $this->id]); |
||
| 104 | $query->andFilterWhere(['like', 'name', $this->name]); |
||
| 105 | $query->andFilterWhere(['like', 'key', $this->key]); |
||
| 106 | $query->andFilterWhere(['preload' => $this->preload]); |
||
| 107 | return $dataProvider; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @deprecated use ContentBlockHelper::getChunk($key, $params, $model) instead |
||
| 112 | * Get chunk model or value |
||
| 113 | * @param string $key |
||
| 114 | * @param bool $valueOnly |
||
| 115 | * @param mixed $defaultValue |
||
| 116 | * @return ContentBlock|string |
||
|
|
|||
| 117 | */ |
||
| 118 | public static function getChunk($key, $valueOnly = true, $defaultValue = null) |
||
| 119 | { |
||
| 120 | return ContentBlockHelper::getChunk($key); |
||
| 121 | } |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @return ContentBlockGroup[] |
||
| 126 | */ |
||
| 127 | public function getGroup() |
||
| 131 | } |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.