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 |
||
| 12 | class ClubAR extends CActiveRecord |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Returns the static model of the specified AR class. |
||
| 16 | * @param string $className active record class name. |
||
| 17 | * @return Club the static model class |
||
| 18 | */ |
||
| 19 | public static function model($className = __CLASS__) |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @return string the associated database table name |
||
| 26 | */ |
||
| 27 | public function tableName() |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @return array validation rules for model attributes. |
||
| 34 | */ |
||
| 35 | public function rules() |
||
| 49 | |||
| 50 | public function clean($attribute) |
||
| 56 | |||
| 57 | public function levelRequirement($attribute, $params) |
||
| 58 | { |
||
| 59 | View Code Duplication | if (Yii::app()->player->model->level < Yii::app()->params['clubCreateLevelRequirement']) { |
|
|
|
|||
| 60 | $this->addError($attribute, 'Saját klub indításához minimum ' . Yii::app()->params['clubCreateLevelRequirement'] . '. szintre kell fejlődnöd.'); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | public function hasOtherClub($attribute, $params) |
||
| 70 | |||
| 71 | public function nameIsFree($attribute, $params) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return array customized attribute labels (name=>label) |
||
| 85 | */ |
||
| 86 | public function attributeLabels() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Retrieves a list of models based on the current search/filter conditions. |
||
| 98 | * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. |
||
| 99 | */ |
||
| 100 | public function search() |
||
| 116 | |||
| 117 | protected function beforeSave() |
||
| 122 | |||
| 123 | protected function afterSave() |
||
| 135 | } |
||
| 136 |
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.