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 |
||
| 11 | class Page extends Model |
||
| 12 | { |
||
| 13 | |||
| 14 | public function getSource() |
||
| 18 | |||
| 19 | private $id; |
||
| 20 | private $slug; |
||
| 21 | private $created_at; |
||
| 22 | private $updated_at; |
||
| 23 | |||
| 24 | protected $title; // translate |
||
| 25 | protected $text; // translate |
||
| 26 | protected $meta_title; // translate |
||
| 27 | protected $meta_description; // translate |
||
| 28 | protected $meta_keywords; // translate |
||
| 29 | |||
| 30 | protected $translateModel = 'Page\Model\Translate\PageTranslate'; // translate |
||
| 31 | protected $translateFields = [ |
||
| 32 | 'title', |
||
| 33 | 'meta_title', |
||
| 34 | 'meta_description', |
||
| 35 | 'meta_keywords', |
||
| 36 | 'text' |
||
| 37 | ]; |
||
| 38 | |||
| 39 | public function initialize() |
||
| 43 | |||
| 44 | public function beforeCreate() |
||
| 48 | |||
| 49 | public function beforeUpdate() |
||
| 53 | |||
| 54 | public function afterUpdate() |
||
| 62 | |||
| 63 | public function updateFields($data) |
||
| 72 | |||
| 73 | View Code Duplication | public function validation() |
|
| 84 | |||
| 85 | View Code Duplication | public static function findCachedBySlug($slug) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param mixed $created_at |
||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | public function setCreatedAt($created_at) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return mixed |
||
| 105 | */ |
||
| 106 | public function getCreatedAt() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param mixed $id |
||
| 113 | * @return $this |
||
| 114 | */ |
||
| 115 | public function setId($id) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return mixed |
||
| 123 | */ |
||
| 124 | public function getId() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param mixed $meta_description |
||
| 131 | * @return $this |
||
| 132 | */ |
||
| 133 | public function setMetaDescription($meta_description) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return mixed |
||
| 141 | */ |
||
| 142 | public function getMetaDescription() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param mixed $meta_keywords |
||
| 149 | * @return $this |
||
| 150 | */ |
||
| 151 | public function setMetaKeywords($meta_keywords) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return mixed |
||
| 159 | */ |
||
| 160 | public function getMetaKeywords() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param mixed $meta_title |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | public function setMetaTitle($meta_title) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return mixed |
||
| 177 | */ |
||
| 178 | public function getMetaTitle() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param mixed $slug |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | public function setSlug($slug) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return mixed |
||
| 195 | */ |
||
| 196 | public function getSlug() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param mixed $text |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | public function setText($text) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return mixed |
||
| 213 | */ |
||
| 214 | public function getText() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param mixed $title |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | public function setTitle($title) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return mixed |
||
| 231 | */ |
||
| 232 | public function getTitle() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param $updated_at |
||
| 239 | * @return $this |
||
| 240 | */ |
||
| 241 | public function setUpdatedAt($updated_at) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return mixed |
||
| 249 | */ |
||
| 250 | public function getUpdatedAt() |
||
| 254 | |||
| 255 | } |
||
| 256 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.