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 getNew 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 getNew, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class getNew |
||
| 8 | { |
||
| 9 | private $userCountry; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @return mixed |
||
| 13 | */ |
||
| 14 | public function get_userCountry() |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param $userCountry |
||
| 21 | */ |
||
| 22 | public function set_userCountry($userCountry): void |
||
| 26 | |||
| 27 | /** |
||
| 28 | * constructor |
||
| 29 | * creates the object |
||
| 30 | * |
||
| 31 | * @param string $userCountry country of the loggedin user as parameter for the sql statements |
||
| 32 | */ |
||
| 33 | public function __construct($userCountry) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * rsForSmarty creates the result from database to use with smarty assign-rs method |
||
| 40 | * based on $this->type |
||
| 41 | * |
||
| 42 | * @param string $type type of the "new"-information, i.e. cache, event, rating, etc |
||
| 43 | * @param array $args numeric array containing the parameter for "sql_slave" |
||
| 44 | * @return object mysql result used by smarty assign_rs |
||
| 45 | */ |
||
| 46 | public function rsForSmarty($type, $args = null) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * feedForSmarty creates a HTML string to use with smarty assign method |
||
| 63 | * based on $this->type by using RSSParser class |
||
| 64 | * |
||
| 65 | * @param string $type type of the "new"-information, i.e. cache, event, rating, etc |
||
| 66 | * @param int $items number of feeditems to parse from feed (RSSParser) |
||
| 67 | * @param string $url url of the feed to parse (RSSParser) |
||
| 68 | * @param int $timeout maximum seconds to wait for the requested page |
||
| 69 | * @param null|mixed $includeText |
||
| 70 | * @return string HTML string used for smarty assign method |
||
| 71 | * @internal param bool $includetext ???following??? add table-tag? |
||
| 72 | */ |
||
| 73 | public function feedForSmarty($type, $items = null, $url = null, $timeout = null, $includeText = null) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * cacheRs executes the database statements for type "cache" |
||
| 90 | * |
||
| 91 | * @param array $args numeric array containing the parameter for "sql_slave" |
||
| 92 | * @return object mysql result used by smarty assign_rs |
||
| 93 | */ |
||
| 94 | View Code Duplication | private function cacheRs($args = null) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * eventRs executes the database statements for type "event" |
||
| 144 | * |
||
| 145 | * @param array $args numeric array containing the parameter for "sql_slave" |
||
| 146 | * @return object mysql result used by smarty assign_rs |
||
| 147 | */ |
||
| 148 | View Code Duplication | private function eventRs($args = null) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * ratingDays returns the number of days used for top rating calculation |
||
| 196 | * |
||
| 197 | * @return int |
||
| 198 | */ |
||
| 199 | public function ratingDays() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * ratingRs executes the database statements for type "rating" |
||
| 216 | * |
||
| 217 | * @param array $args numeric array containing the parameter for "sql_slave" |
||
| 218 | * @return object mysql result used by smarty assign_rs |
||
| 219 | */ |
||
| 220 | View Code Duplication | private function ratingRs($args = null) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * blogFeed executes the RSSParser for type "blog" |
||
| 274 | * |
||
| 275 | * @param int $items number of feeditems to parse from feed (RSSParser) |
||
| 276 | * @param string $url url of the feed to parse (RSSParser) |
||
| 277 | * @param int $timeout maximum seconds to wait for the requested page |
||
| 278 | * @param bool $includeText ???following??? add table-tag? |
||
| 279 | * @return string HTML string used for smarty assign method |
||
| 280 | */ |
||
| 281 | View Code Duplication | private function blogFeed($items = null, $url = null, $timeout = null, $includeText = null) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * forumFeed executes the RSSParser for type "forum" |
||
| 306 | * |
||
| 307 | * @param int $items number of feeditems to parse from feed (RSSParser) |
||
| 308 | * @param string $url url of the feed to parse (RSSParser) |
||
| 309 | * @param int $timeout maximum seconds to wait for the requested page |
||
| 310 | * @param bool $includeText ???following??? add table-tag? |
||
| 311 | * @return string HTML string used for smarty assign method |
||
| 312 | */ |
||
| 313 | View Code Duplication | private function forumFeed($items = null, $url = null, $timeout = null, $includeText = null) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * wikiFeed executes the RSSParser for type "wiki" |
||
| 337 | * |
||
| 338 | * @param int $items number of feeditems to parse from feed (RSSParser) |
||
| 339 | * @param string $url url of the feed to parse (RSSParser) |
||
| 340 | * @param bool $includeText ???following??? add table-tag? |
||
| 341 | * @param int $timeout maximum seconds to wait for the requested page |
||
| 342 | * @return string HTML string used for smarty assign method |
||
| 343 | */ |
||
| 344 | View Code Duplication | private function wikiFeed($items = null, $url = null, $timeout = null, $includeText = null) |
|
| 367 | } |
||
| 368 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.