Complex classes like Categories often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Categories, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Categories extends CiiModel |
||
|
|
|||
| 21 | {
|
||
| 22 | public $pageSize = 15; |
||
| 23 | |||
| 24 | public $description = NULL; |
||
| 25 | |||
| 26 | public $keywords = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Returns the static model of the specified AR class. |
||
| 30 | * @param string $className active record class name. |
||
| 31 | * @return Categories the static model class |
||
| 32 | */ |
||
| 33 | public static function model($className=__CLASS__) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @return string the associated database table name |
||
| 40 | */ |
||
| 41 | public function tableName() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @return array validation rules for model attributes. |
||
| 48 | */ |
||
| 49 | public function rules() |
||
| 50 | {
|
||
| 51 | // NOTE: you should only define rules for those attributes that |
||
| 52 | // will receive user inputs. |
||
| 53 | return array( |
||
| 54 | array('parent_id, name', 'required'),
|
||
| 55 | array('parent_id', 'numerical', 'integerOnly'=>true),
|
||
| 56 | array('name, slug', 'length', 'max'=>150),
|
||
| 57 | // The following rule is used by search(). |
||
| 58 | // Please remove those attributes that should not be searched. |
||
| 59 | array('id, parent_id, name, slug', 'safe', 'on'=>'search'),
|
||
| 60 | ); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return array relational rules. |
||
| 65 | */ |
||
| 66 | public function relations() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return array customized attribute labels (name=>label) |
||
| 79 | */ |
||
| 80 | public function attributeLabels() |
||
| 92 | |||
| 93 | public function getDescription() |
||
| 94 | {
|
||
| 95 | $this->description = CategoriesMetadata::model()->findByAttributes(array('category_id' => $this->id, 'key' => 'description'));
|
||
| 96 | if ($this->description == null || $this->description == false) |
||
| 97 | return NULL; |
||
| 98 | |||
| 99 | $this->description = $this->description->value; |
||
| 100 | return $this->description; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Retrieves a list of models based on the current search/filter conditions. |
||
| 105 | * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. |
||
| 106 | */ |
||
| 107 | public function search() |
||
| 108 | {
|
||
| 109 | $criteria=new CDbCriteria; |
||
| 110 | |||
| 111 | $criteria->compare('id',$this->id);
|
||
| 112 | $criteria->compare('parent_id',$this->parent_id);
|
||
| 113 | $criteria->compare('name',$this->name,true);
|
||
| 114 | $criteria->compare('slug',$this->slug,true);
|
||
| 115 | $criteria->compare('created',$this->created,true);
|
||
| 116 | $criteria->compare('updated',$this->updated,true);
|
||
| 117 | $criteria->order = "id DESC"; |
||
| 118 | |||
| 119 | return new CActiveDataProvider($this, array( |
||
| 120 | 'criteria' => $criteria, |
||
| 121 | 'pagination' => array( |
||
| 122 | 'pageSize' => $this->pageSize |
||
| 123 | ) |
||
| 124 | )); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Gets keyword tags for this entry |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | public function getKeywords() |
||
| 132 | {
|
||
| 133 | $tags = CategoriesMetadata::model()->findByAttributes(array('category_id' => $this->id, 'key' => 'keywords'));
|
||
| 134 | return $tags === NULL ? array() : CJSON::decode($tags->value); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Adds a tag to the model |
||
| 139 | * @param string $tag The tag to add |
||
| 140 | * @return bool If the insert was successful or not |
||
| 141 | */ |
||
| 142 | public function addKeyword($tag) |
||
| 143 | {
|
||
| 144 | $tags = $this->keywords; |
||
| 145 | if (in_array($tag, $tags) || $tag == "") |
||
| 146 | return false; |
||
| 147 | |||
| 148 | $tags[] = $tag; |
||
| 149 | $tags = CJSON::encode($tags); |
||
| 150 | $metaTag = CategoriesMetadata::model()->findByAttributes(array('category_id' => $this->id, 'key' => 'keywords'));
|
||
| 151 | if ($metaTag == false || $metaTag == NULL) |
||
| 152 | {
|
||
| 153 | $metaTag = new CategoriestMetadata; |
||
| 154 | $metaTag->content_id = $this->id; |
||
| 155 | $metaTag->key = 'keywords'; |
||
| 156 | } |
||
| 157 | |||
| 158 | $metaTag->value = $tags; |
||
| 159 | return $metaTag->save(); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Removes a tag from the model |
||
| 164 | * @param string $tag The tag to remove |
||
| 165 | * @return bool If the removal was successful |
||
| 166 | */ |
||
| 167 | public function removeKeyword($tag) |
||
| 168 | {
|
||
| 169 | $tags = $this->keywords; |
||
| 170 | if (!in_array($tag, $tags) || $tag == "") |
||
| 171 | return false; |
||
| 172 | |||
| 173 | $key = array_search($tag, $tags); |
||
| 174 | unset($tags[$key]); |
||
| 175 | $tags = CJSON::encode($tags); |
||
| 176 | |||
| 177 | $metaTag = CategoryMetadata::model()->findByAttributes(array('category_id' => $this->id, 'key' => 'keywords'));
|
||
| 178 | $metaTag->value = $tags; |
||
| 179 | return $metaTag->save(); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Verifies the slug before validating the model |
||
| 184 | */ |
||
| 185 | public function beforeValidate() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Flushes URL data from the cache before the model is updated |
||
| 194 | */ |
||
| 195 | public function afterSave() |
||
| 196 | {
|
||
| 197 | $meta = CategoriesMetadata::model()->findByAttributes(array('category_id' => $this->id, 'key' => 'description'));
|
||
| 213 | |||
| 214 | /** |
||
| 215 | * Automatically corrects parent tree issues that arise when a parent category node |
||
| 216 | * is deleted. |
||
| 217 | * @return boolean |
||
| 218 | */ |
||
| 219 | public function beforeDelete() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Retrieves the parent categories for a given category_id |
||
| 260 | * @param int $id |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | public function getParentCategories($id) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Recursive callback for retrieving parent categories |
||
| 278 | * @param int $id The category we're seeking |
||
| 279 | * @param array $stack A stack to hold the entire tree |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | private function _getParentCategories($all_categories, $id, array $stack = array()) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * checkSlug - Recursive method to verify that the slug can be used |
||
| 299 | * This method is purposfuly declared here to so that Content::findByPk is used instead of CiiModel::findByPk |
||
| 300 | * @param string $slug - the slug to be checked |
||
| 301 | * @param int $id - the numeric id to be appended to the slug if a conflict exists |
||
| 302 | * @return string $slug - the final slug to be used |
||
| 303 | */ |
||
| 304 | public function checkSlug($slug, $id=NULL) |
||
| 337 | } |
||
| 338 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.