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 |
||
4 | class Monitoring extends Base { |
||
5 | protected $table = 'monitoring'; |
||
6 | |||
7 | /** |
||
8 | * Store Uptime Robot status information as JSON in settings table |
||
9 | * @param none |
||
10 | * @return bool true on success, false on error |
||
11 | **/ |
||
12 | public function storeUptimeRobotStatus() { |
||
41 | |||
42 | /** |
||
43 | * Fetch Uptime Robot Status from settings table |
||
44 | * @param none |
||
45 | * @return array Data on success, false on failure |
||
46 | **/ |
||
47 | public function getUptimeRobotStatus() { |
||
52 | |||
53 | /** |
||
54 | * Check that our cron is currently activated |
||
55 | * @param name string Cronjob name |
||
56 | * @return bool true or false |
||
57 | **/ |
||
58 | public function isDisabled($name) { |
||
62 | |||
63 | /** |
||
64 | * Get the timestamp that last time a cronjob started |
||
65 | * @param name string Cronjob name |
||
66 | * @return int unix timestamp of last time the cronjob started |
||
67 | **/ |
||
68 | public function getLastCronStarted($name) { |
||
72 | |||
73 | /** |
||
74 | * Fetch a value from our table |
||
75 | * @param name string Setting name |
||
76 | * @return value string Value |
||
77 | **/ |
||
78 | public function getStatus($name) { |
||
86 | |||
87 | /** |
||
88 | * Insert or update a setting |
||
89 | * @param name string Name of the variable |
||
90 | * @param value string Variable value |
||
91 | * @return bool |
||
92 | **/ |
||
93 | View Code Duplication | public function setStatus($name, $type, $value) { |
|
104 | |||
105 | /** |
||
106 | * Start a cronjob, mark various fields properly |
||
107 | * @param cron_name string Cronjob name |
||
108 | **/ |
||
109 | public function startCronjob($cron_name) { |
||
119 | |||
120 | /** |
||
121 | * End cronjob with an error message |
||
122 | * @param cron_name string Cronjob Name |
||
123 | * @param msgCode string Message code as stored in error_codes array |
||
124 | * @param exitCode int Exit code to pass on to exit function and monitor report |
||
125 | * @param fatal boolean Should we exit out entirely |
||
126 | * @return none |
||
127 | **/ |
||
128 | public function endCronjob($cron_name, $msgCode, $exitCode=0, $fatal=false, $mail=true) { |
||
148 | } |
||
149 | |||
157 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.