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:
Complex classes like Publication 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 Publication, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Publication extends Model |
||
| 11 | { |
||
| 12 | |||
| 13 | public function getSource() |
||
| 17 | |||
| 18 | protected $translateModel = 'Publication\Model\Translate\PublicationTranslate'; // translate |
||
| 19 | |||
| 20 | public function initialize() |
||
| 21 | { |
||
| 22 | $this->hasMany('id', $this->translateModel, 'foreign_id'); // translate |
||
| 23 | |||
| 24 | $this->belongsTo('type_id', 'Publication\Model\Type', 'id', [ |
||
| 25 | 'alias' => 'type' |
||
| 26 | ]); |
||
| 27 | } |
||
| 28 | |||
| 29 | private $id; |
||
| 30 | private $type_id; |
||
| 31 | private $slug; |
||
| 32 | private $created_at; |
||
| 33 | private $updated_at; |
||
| 34 | private $date; |
||
| 35 | private $preview_src; |
||
| 36 | private $preview_inner; |
||
| 37 | |||
| 38 | protected $title; |
||
| 39 | protected $text; |
||
| 40 | protected $meta_title; |
||
| 41 | protected $meta_description; |
||
| 42 | protected $meta_keywords; |
||
| 43 | |||
| 44 | public function beforeCreate() |
||
| 48 | |||
| 49 | public function beforeUpdate() |
||
| 53 | |||
| 54 | public function afterUpdate() |
||
| 62 | |||
| 63 | public function validation() |
||
| 74 | |||
| 75 | public function afterValidation() |
||
| 81 | |||
| 82 | public function updateFields($data) |
||
| 83 | { |
||
| 84 | if (!$this->getSlug()) { |
||
| 85 | $this->setSlug(Transliterator::slugify($data['title'])); |
||
| 86 | } |
||
| 87 | if (!$this->getMetaTitle()) { |
||
| 88 | $this->setMetaTitle($data['title']); |
||
| 89 | } |
||
| 90 | $this->setPreviewInner(isset($data['preview_inner']) ? 1 : 0); |
||
| 91 | } |
||
| 92 | |||
| 93 | View Code Duplication | public static function findCachedBySlug($slug) |
|
| 102 | |||
| 103 | public static function cacheSlugKey($slug) |
||
| 108 | |||
| 109 | public function setCreatedAt($created_at) |
||
| 114 | |||
| 115 | public function getCreatedAt() |
||
| 119 | |||
| 120 | public function setId($id) |
||
| 125 | |||
| 126 | public function getId() |
||
| 130 | |||
| 131 | public function setMetaDescription($meta_description) |
||
| 136 | |||
| 137 | public function getMetaDescription() |
||
| 141 | |||
| 142 | public function setMetaKeywords($meta_keywords) |
||
| 147 | |||
| 148 | public function getMetaKeywords() |
||
| 152 | |||
| 153 | public function setMetaTitle($meta_title) |
||
| 158 | |||
| 159 | public function getMetaTitle() |
||
| 163 | |||
| 164 | public function setSlug($slug) |
||
| 169 | |||
| 170 | public function getSlug() |
||
| 174 | |||
| 175 | public function setText($text) |
||
| 180 | |||
| 181 | public function getText() |
||
| 185 | |||
| 186 | public function setTitle($title) |
||
| 191 | |||
| 192 | public function getTitle() |
||
| 196 | |||
| 197 | public function setUpdatedAt($updated_at) |
||
| 202 | |||
| 203 | public function getUpdatedAt() |
||
| 207 | |||
| 208 | public function setDate($date) |
||
| 213 | |||
| 214 | public function getDate($format = 'Y-m-d H:i:s') |
||
| 224 | |||
| 225 | public function setTypeId($type_id) |
||
| 230 | |||
| 231 | public function getTypeId() |
||
| 235 | |||
| 236 | View Code Duplication | public function getTypeTitle() |
|
| 245 | |||
| 246 | View Code Duplication | public function getTypeSlug() |
|
| 255 | |||
| 256 | public function setPreviewInner($preview_inner) |
||
| 261 | |||
| 262 | public function getPreviewInner() |
||
| 266 | |||
| 267 | public function getPreviewSrc() |
||
| 271 | |||
| 272 | public function setPreviewSrc($preview_src) |
||
| 277 | |||
| 278 | } |
||
| 279 |