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 |
||
| 13 | class Page extends AliasModel |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The content of the page |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $content; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The creation date of the page |
||
| 23 | * @var TimeDate |
||
| 24 | */ |
||
| 25 | protected $created; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The date the page was last updated |
||
| 29 | * @var TimeDate |
||
| 30 | */ |
||
| 31 | protected $updated; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The ID of the author of the page |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | protected $author; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Whether the page is the home page |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected $home; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The status of the page |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $status; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The name of the database table used for queries |
||
| 53 | */ |
||
| 54 | const TABLE = "pages"; |
||
| 55 | |||
| 56 | const CREATE_PERMISSION = Permission::CREATE_PAGE; |
||
| 57 | const EDIT_PERMISSION = Permission::EDIT_PAGE; |
||
| 58 | const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_PAGE; |
||
| 59 | const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_PAGE; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | 1 | protected function assignResult($page) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | protected function assignLazyResult($page) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Get the raw content of the page |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getContent() |
||
| 88 | { |
||
| 89 | $this->lazyLoad(); |
||
| 90 | |||
| 91 | return $this->content; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get the page's submission time |
||
| 96 | * @return TimeDate |
||
| 97 | */ |
||
| 98 | public function getCreated() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the time when the page was last updated |
||
| 107 | * @return TimeDate |
||
| 108 | */ |
||
| 109 | public function getUpdated() |
||
| 110 | { |
||
| 111 | $this->lazyLoad(); |
||
| 112 | |||
| 113 | return $this->updated; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the user who created the page |
||
| 118 | * @return Player The page's author |
||
| 119 | */ |
||
| 120 | public function getAuthor() |
||
| 121 | { |
||
| 122 | return Player::get($this->author); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get the status of the page |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | 1 | public function getStatus() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Find out whether this is the homepage |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | 1 | public function isHomePage() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Set the content of the page |
||
| 145 | * |
||
| 146 | * @param string $content |
||
| 147 | * @return self |
||
| 148 | */ |
||
| 149 | public function setContent($content) |
||
| 150 | { |
||
| 151 | return $this->updateProperty($this->content, "content", $content, 's'); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set the status of the page |
||
| 156 | * |
||
| 157 | * @param string $status One of "live", "revision" or "disabled" |
||
| 158 | * @return self |
||
| 159 | */ |
||
| 160 | public function setStatus($status) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Update the last edit timestamp |
||
| 167 | * @return self |
||
| 168 | */ |
||
| 169 | public function updateEditTimestamp() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Create a new Page |
||
| 176 | * |
||
| 177 | * @param string $title The title of the page |
||
| 178 | * @param string $content The content of page |
||
| 179 | * @param int $authorID The ID of the author |
||
| 180 | * @param string $status Page status: 'live','disabled',or 'deleted' |
||
| 181 | * |
||
| 182 | * @return Page An object representing the page that was just created |
||
| 183 | */ |
||
| 184 | 1 | public static function addPage($title, $content, $authorID, $status = "live") |
|
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | 1 | public static function getRouteName($action = 'show') |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Get a list of enabled pages |
||
| 206 | * @return Page[] A list of Page IDs |
||
| 207 | */ |
||
| 208 | 1 | public static function getPages() |
|
| 209 | { |
||
| 210 | 1 | return self::arrayIdToModel( |
|
| 211 | 1 | parent::fetchIdsFrom("status", array("live"), "s") |
|
|
|
|||
| 212 | ); |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | */ |
||
| 218 | 1 | protected static function getDisallowedAliases() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | 1 | public static function getActiveStatuses() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | 1 | public static function getEagerColumns() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | public static function getLazyColumns() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get a query builder for pages |
||
| 251 | * @return QueryBuilder |
||
| 252 | */ |
||
| 253 | View Code Duplication | public static function getQueryBuilder() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Get the home page |
||
| 266 | * @return Page |
||
| 267 | */ |
||
| 268 | public static function getHomePage() |
||
| 272 | } |
||
| 273 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.