Complex classes like Content 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 Content, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Content extends CiiModel |
||
|
|
|||
| 33 | {
|
||
| 34 | public $pageSize = 9; |
||
| 35 | |||
| 36 | public $viewFile = 'blog'; |
||
| 37 | |||
| 38 | public $layoutFile = 'blog'; |
||
| 39 | |||
| 40 | public $autosavedata = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Returns the static model of the specified AR class. |
||
| 44 | * @param string $className active record class name. |
||
| 45 | * @return Content the static model class |
||
| 46 | */ |
||
| 47 | public static function model($className=__CLASS__) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return string the associated database table name |
||
| 54 | */ |
||
| 55 | public function tableName() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return string[] primary key of the table |
||
| 62 | **/ |
||
| 63 | public function primaryKey() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return array validation rules for model attributes. |
||
| 70 | */ |
||
| 71 | public function rules() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return array relational rules. |
||
| 86 | */ |
||
| 87 | public function relations() |
||
| 88 | {
|
||
| 89 | // NOTE: you may need to adjust the relation name and the related |
||
| 90 | // class name for the relations automatically generated below. |
||
| 91 | return array( |
||
| 92 | 'comments' => array(self::HAS_MANY, 'Comments', 'content_id'), |
||
| 93 | 'author' => array(self::BELONGS_TO, 'Users', 'author_id'), |
||
| 94 | 'category' => array(self::BELONGS_TO, 'Categories', 'category_id'), |
||
| 95 | 'metadata' => array(self::HAS_MANY, 'ContentMetadata', 'content_id'), |
||
| 96 | ); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return array customized attribute labels (name=>label) |
||
| 101 | */ |
||
| 102 | public function attributeLabels() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns a safe output to the theme |
||
| 127 | * This includes setting nofollow tags on links, forcing them to open in new windows, and safely encoding the text |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getSafeOutput() |
||
| 149 | |||
| 150 | public function getExtract() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Correctly retrieves the number of likes for a particular post. |
||
| 158 | * |
||
| 159 | * This was added to address an issue with the like count changing if an article was updated |
||
| 160 | * @return int The number of likes for this post |
||
| 161 | */ |
||
| 162 | |||
| 163 | public function getLikeCount() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Gets keyword tags for this entry |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | public function getTags() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Adds a tag to the model |
||
| 184 | * @param string $tag The tag to add |
||
| 185 | * @return bool If the insert was successful or not |
||
| 186 | */ |
||
| 187 | public function addTag($tag) |
||
| 188 | {
|
||
| 189 | $tags = $this->tags; |
||
| 190 | if (in_array($tag, $tags) || $tag == "") |
||
| 191 | return false; |
||
| 192 | |||
| 193 | $tags[] = $tag; |
||
| 194 | $tags = json_encode($tags); |
||
| 195 | $meta = $this->getPrototype('ContentMetadata', array('content_id' => $this->id, 'key' => 'keywords'));
|
||
| 196 | |||
| 197 | $meta->value = $tags; |
||
| 198 | return $meta->save(); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Removes a tag from the model |
||
| 203 | * @param string $tag The tag to remove |
||
| 204 | * @return bool If the removal was successful |
||
| 205 | */ |
||
| 206 | public function removeTag($tag) |
||
| 207 | {
|
||
| 208 | $tags = $this->tags; |
||
| 209 | if (!in_array($tag, $tags) || $tag == "") |
||
| 210 | return false; |
||
| 211 | |||
| 212 | $key = array_search($tag, $tags); |
||
| 213 | unset($tags[$key]); |
||
| 214 | $tags = json_encode($tags); |
||
| 215 | |||
| 216 | $meta = $this->getPrototype('ContentMetadata', array('content_id' => $this->id, 'key' => 'keywords'));
|
||
| 217 | $meta->value = $tags; |
||
| 218 | return $meta->save(); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Provides a base criteria for status, uniqueness, and published states |
||
| 223 | * @return CDBCriteria |
||
| 224 | */ |
||
| 225 | public function getBaseCriteria() |
||
| 226 | {
|
||
| 227 | $criteria = new CDbCriteria(); |
||
| 228 | return $criteria->addCondition("vid=(SELECT MAX(vid) FROM content WHERE id=t.id)")
|
||
| 229 | ->addCondition('status = 1')
|
||
| 230 | ->addCondition('UNIX_TIMESTAMP() >= published');
|
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns the appropriate status' depending up the user's role |
||
| 235 | * @return string[] |
||
| 236 | */ |
||
| 237 | public function getStatuses() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Determines if an article is published or not |
||
| 252 | * @return boolean |
||
| 253 | */ |
||
| 254 | public function isPublished() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Determines if a given articled is scheduled or not |
||
| 261 | * @return boolean |
||
| 262 | */ |
||
| 263 | public function isScheduled() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Gets a flattened list of keyword tags for jQuery.tag.js |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public function getTagsFlat() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Retrieves the layout used from Metadata |
||
| 279 | * We cache this to speed up the viewfile |
||
| 280 | */ |
||
| 281 | public function getLayout() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Sets the layout |
||
| 289 | * @param string $data the layout file |
||
| 290 | * @return boolean |
||
| 291 | */ |
||
| 292 | public function setLayout($data) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Sets the view |
||
| 301 | * @param string $data The view file |
||
| 302 | * @return boolean |
||
| 303 | */ |
||
| 304 | public function setView($data) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Retrieves the viewfile used from Metadata |
||
| 313 | * We cache this to speed up the viewfile |
||
| 314 | */ |
||
| 315 | public function getView() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Updates the like_count after finding new data |
||
| 323 | */ |
||
| 324 | protected function afterFind() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Retrieves a list of models based on the current search/filter conditions. |
||
| 332 | * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. |
||
| 333 | */ |
||
| 334 | public function search() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Finds all active records with the specified primary keys. |
||
| 375 | * Overloaded to support composite primary keys. For our content, we want to find the latest version of that primary key, defined as MAX(vid) WHERE id = pk |
||
| 376 | * See {@link find()} for detailed explanation about $condition and $params.
|
||
| 377 | * @param mixed $pk primary key value(s). Use array for multiple primary keys. For composite key, each key value must be an array (column name=>column value). |
||
| 378 | * @param mixed $condition query condition or criteria. |
||
| 379 | * @param array $params parameters to be bound to an SQL statement. |
||
| 380 | * @return array the records found. An empty array is returned if none is found. |
||
| 381 | */ |
||
| 382 | public function findByPk($pk, $condition='', $params=array()) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Lists all revisions in the database for a givenid |
||
| 404 | * @param int $id [description] |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | public function findRevisions($id) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * BeforeValidate |
||
| 424 | * @see CActiveRecord::beforeValidate |
||
| 425 | */ |
||
| 426 | public function beforeValidate() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Saves a prototype copy of the model so that we can get an id back to work with |
||
| 440 | * @return boolean $model->save(false) without any validation rules |
||
| 441 | */ |
||
| 442 | public function savePrototype($author_id) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * BeforeSave |
||
| 472 | * Clears caches for rebuilding, creates the end slug that we are going to use |
||
| 473 | * @see CActiveRecord::beforeSave(); |
||
| 474 | */ |
||
| 475 | public function beforeSave() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * AfterSave |
||
| 489 | * Updates the layout and view if necessary |
||
| 490 | * @see CActiveRecord::afterSave() |
||
| 491 | */ |
||
| 492 | public function afterSave() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * BeforeDelete |
||
| 507 | * Clears caches for rebuilding |
||
| 508 | * @see CActiveRecord::beforeDelete |
||
| 509 | */ |
||
| 510 | public function beforeDelete() |
||
| 519 | |||
| 520 | |||
| 521 | /** |
||
| 522 | * Retrieves the available view files under the current theme |
||
| 523 | * @return array A list of files by name |
||
| 524 | */ |
||
| 525 | public function getViewFiles($theme=null) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Retrieves the available layouts under the current theme |
||
| 532 | * @return array A list of files by name |
||
| 533 | */ |
||
| 534 | public function getLayoutFiles($theme=null) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Retrieves view files for a particular path |
||
| 541 | * @param string $theme The theme to reference |
||
| 542 | * @param string $type The view type to lookup |
||
| 543 | * @return array $files An array of files |
||
| 544 | */ |
||
| 545 | private function getFiles($theme=null, $type='views') |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Fancy truncate function to help clean up our strings for the excerpt |
||
| 585 | * @param string $string The string we want to apply the text to |
||
| 586 | * @param int $limit How many characters we want to break into |
||
| 587 | * @param string $break Characters we want to break on if possible |
||
| 588 | * @param string $pad The padding we want to apply |
||
| 589 | * @return string Truncated string |
||
| 590 | */ |
||
| 591 | private function myTruncate($string, $limit, $break=".", $pad="...") |
||
| 608 | |||
| 609 | /** |
||
| 610 | * checkSlug - Recursive method to verify that the slug can be used |
||
| 611 | * This method is purposfuly declared here to so that Content::findByPk is used instead of CiiModel::findByPk |
||
| 612 | * @param string $slug - the slug to be checked |
||
| 613 | * @param int $id - the numeric id to be appended to the slug if a conflict exists |
||
| 614 | * @return string $slug - the final slug to be used |
||
| 615 | */ |
||
| 616 | public function checkSlug($slug, $id=NULL) |
||
| 651 | } |
||
| 652 |
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.