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 |
||
| 10 | trait LoggerProvider |
||
| 11 | { |
||
| 12 | public function loggerAdapterProvider() |
||
| 13 | { |
||
| 14 | return [ |
||
| 15 | ["debug"], |
||
| 16 | ["info"], |
||
| 17 | ["notice"], |
||
| 18 | ["warn"], |
||
| 19 | ["warning"], |
||
| 20 | ["error"], |
||
| 21 | ["critical"], |
||
| 22 | ["alert"], |
||
| 23 | ["emergency"], |
||
| 24 | ["fatal"] |
||
| 25 | ]; |
||
| 26 | } |
||
| 27 | |||
| 28 | public function loggerAdapterWithPlaceholderProvider() |
||
| 29 | { |
||
| 30 | return [ |
||
| 31 | ["debug", "log message for debug.", "log message for { level }.", ["level" => "debug"]], |
||
| 32 | ["debug", "log message for debug.", "log message for {level }.", ["level" => "debug"]], |
||
| 33 | ["debug", "log message for debug.", "log message for { level}.", ["level" => "debug"]], |
||
| 34 | ["debug", "log message for debug.", "log message for {level}.", ["level" => "debug"]] |
||
| 35 | ]; |
||
| 36 | } |
||
| 37 | |||
| 38 | public function logLevelDebugProvider() |
||
| 39 | { |
||
| 40 | return [ |
||
| 41 | ["debug", true], |
||
| 42 | ["info", true], |
||
| 43 | ["notice", true], |
||
| 44 | ["warn", true], |
||
| 45 | ["warning", true], |
||
| 46 | ["error", true], |
||
| 47 | ["critical", true], |
||
| 48 | ["alert", true], |
||
| 49 | ["emergency", true], |
||
| 50 | ["fatal", true] |
||
| 51 | ]; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function logLevelInfoProvider() |
||
| 55 | { |
||
| 56 | return [ |
||
| 57 | ["debug", false], |
||
| 58 | ["info", true], |
||
| 59 | ["notice", true], |
||
| 60 | ["warn", true], |
||
| 61 | ["warning", true], |
||
| 62 | ["error", true], |
||
| 63 | ["critical", true], |
||
| 64 | ["alert", true], |
||
| 65 | ["emergency", true], |
||
| 66 | ["fatal", true] |
||
| 67 | ]; |
||
| 68 | } |
||
| 69 | |||
| 70 | public function logLevelNoticeProvider() |
||
| 71 | { |
||
| 72 | return [ |
||
| 73 | ["debug", false], |
||
| 74 | ["info", false], |
||
| 75 | ["notice", true], |
||
| 76 | ["warn", true], |
||
| 77 | ["warning", true], |
||
| 78 | ["error", true], |
||
| 79 | ["critical", true], |
||
| 80 | ["alert", true], |
||
| 81 | ["emergency", true], |
||
| 82 | ["fatal", true] |
||
| 83 | ]; |
||
| 84 | } |
||
| 85 | |||
| 86 | public function logLevelWarnProvider() |
||
| 87 | { |
||
| 88 | return [ |
||
| 89 | ["debug", false], |
||
| 90 | ["info", false], |
||
| 91 | ["notice", false], |
||
| 92 | ["warn", true], |
||
| 93 | ["warning", true], |
||
| 94 | ["error", true], |
||
| 95 | ["critical", true], |
||
| 96 | ["alert", true], |
||
| 97 | ["emergency", true], |
||
| 98 | ["fatal", true] |
||
| 99 | ]; |
||
| 100 | } |
||
| 101 | |||
| 102 | public function logLevelWarningProvider() |
||
| 103 | { |
||
| 104 | return [ |
||
| 105 | ["debug", false], |
||
| 106 | ["info", false], |
||
| 107 | ["notice", false], |
||
| 108 | ["warn", true], |
||
| 109 | ["warning", true], |
||
| 110 | ["error", true], |
||
| 111 | ["critical", true], |
||
| 112 | ["alert", true], |
||
| 113 | ["emergency", true], |
||
| 114 | ["fatal", true] |
||
| 115 | ]; |
||
| 116 | } |
||
| 117 | |||
| 118 | public function logLevelErrorProvider() |
||
| 119 | { |
||
| 120 | return [ |
||
| 121 | ["debug", false], |
||
| 122 | ["info", false], |
||
| 123 | ["notice", false], |
||
| 124 | ["warn", false], |
||
| 125 | ["warning", false], |
||
| 126 | ["error", true], |
||
| 127 | ["critical", true], |
||
| 128 | ["alert", true], |
||
| 129 | ["emergency", true], |
||
| 130 | ["fatal", true] |
||
| 131 | ]; |
||
| 132 | } |
||
| 133 | |||
| 134 | public function logLevelCriticalProvider() |
||
| 135 | { |
||
| 136 | return [ |
||
| 137 | ["debug", false], |
||
| 138 | ["info", false], |
||
| 139 | ["notice", false], |
||
| 140 | ["warn", false], |
||
| 141 | ["warning", false], |
||
| 142 | ["error", false], |
||
| 143 | ["critical", true], |
||
| 144 | ["alert", true], |
||
| 145 | ["emergency", true], |
||
| 146 | ["fatal", true] |
||
| 147 | ]; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function logLevelAlertProvider() |
||
| 151 | { |
||
| 152 | return [ |
||
| 153 | ["debug", false], |
||
| 154 | ["info", false], |
||
| 155 | ["notice", false], |
||
| 156 | ["warn", false], |
||
| 157 | ["warning", false], |
||
| 158 | ["error", false], |
||
| 159 | ["critical", false], |
||
| 160 | ["alert", true], |
||
| 161 | ["emergency", true], |
||
| 162 | ["fatal", true] |
||
| 163 | ]; |
||
| 164 | } |
||
| 165 | |||
| 166 | public function logLevelEmergencyProvider() |
||
| 167 | { |
||
| 168 | return [ |
||
| 169 | ["debug", false], |
||
| 170 | ["info", false], |
||
| 171 | ["notice", false], |
||
| 172 | ["warn", false], |
||
| 173 | ["warning", false], |
||
| 174 | ["error", false], |
||
| 175 | ["critical", false], |
||
| 176 | ["alert", false], |
||
| 177 | ["emergency", true], |
||
| 178 | ["fatal", true] |
||
| 179 | ]; |
||
| 180 | } |
||
| 181 | |||
| 182 | public function logLevelFatalProvider() |
||
| 183 | { |
||
| 184 | return [ |
||
| 185 | ["debug", false], |
||
| 186 | ["info", false], |
||
| 187 | ["notice", false], |
||
| 188 | ["warn", false], |
||
| 189 | ["warning", false], |
||
| 190 | ["error", false], |
||
| 191 | ["critical", false], |
||
| 192 | ["alert", false], |
||
| 193 | ["emergency", false], |
||
| 194 | ["fatal", true] |
||
| 195 | ]; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function loggerFormatterProvider() |
||
| 199 | { |
||
| 200 | return [ |
||
| 201 | ["log.test3_1.ini", "message", "message"], |
||
| 202 | ["log.test3_2.ini", "message", "[debug] message"], |
||
| 203 | ["log.test3_3.ini", "message", "[DEBUG] message"], |
||
| 204 | ["log.test3_4.ini", "message", "[debug ] message"], |
||
| 205 | ["log.test3_5.ini", "message", "[DEBUG ] message"], |
||
| 206 | ["log.test3_6.ini", "message", "[webstream.logtest] message"] |
||
| 207 | ]; |
||
| 208 | } |
||
| 209 | |||
| 210 | public function loggerFormatterDateTimeProvider() |
||
| 211 | { |
||
| 212 | return [ |
||
| 213 | ["log.test4_1.ini", "/(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})/", "message", "message"], |
||
| 214 | ["log.test4_2.ini", "/(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3})/", "message", "message"], |
||
| 215 | ["log.test4_3.ini", "/(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})/", "message", " message"], |
||
| 216 | ["log.test4_4.ini", "/(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3})/", "message", " message"] |
||
| 217 | ]; |
||
| 218 | } |
||
| 219 | |||
| 220 | public function writeTimingProvider() |
||
| 221 | { |
||
| 222 | return [ |
||
| 223 | [true, "b", "a", "a", "a".PHP_EOL."b".PHP_EOL."a".PHP_EOL], |
||
| 224 | [false,"b", "a", "a", "b".PHP_EOL."a".PHP_EOL."a".PHP_EOL] |
||
| 225 | ]; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function unRotateByCycleProvider() |
||
| 229 | { |
||
| 230 | $day_of_year = 24 * 365; |
||
| 231 | $year = date("Y"); |
||
| 232 | if (($year % 4 === 0 && $year % 100 !== 0) || $year % 400 === 0) { |
||
| 233 | $day_of_year = 24 * 366; |
||
| 234 | } |
||
| 235 | |||
| 236 | return [ |
||
| 237 | ["log.test6.day.ini", 1], |
||
| 238 | ["log.test6.day.ini", 23], |
||
| 239 | ["log.test6.week.ini", 24], |
||
| 240 | ["log.test6.week.ini", 24 * 7 - 1], |
||
| 241 | ["log.test6.month.ini", 24 * intval(date("t", time())) - 1], |
||
| 242 | ["log.test6.year.ini", $day_of_year - 1] |
||
| 243 | ]; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function rotateByCycleProvider() |
||
| 247 | { |
||
| 248 | $day_of_month = intval(date("t", time())); |
||
| 249 | $day_of_year = 24 * 365; |
||
| 250 | $year = date("Y"); |
||
| 251 | if (($year % 4 === 0 && $year % 100 !== 0) || $year % 400 === 0) { |
||
| 252 | $day_of_year = 24 * 366; |
||
| 253 | } |
||
| 254 | |||
| 255 | return [ |
||
| 256 | ["log.test6.day.ini", 24], |
||
| 257 | ["log.test6.day.ini", 25], |
||
| 258 | ["log.test6.week.ini", 24 * 7], |
||
| 259 | ["log.test6.week.ini", 24 * 7 + 1], |
||
| 260 | ["log.test6.month.ini", 24 * $day_of_month], |
||
| 261 | ["log.test6.month.ini", 24 * $day_of_month + 1], |
||
| 262 | ["log.test6.year.ini", $day_of_year], |
||
| 263 | ["log.test6.year.ini", $day_of_year + 1] |
||
| 264 | ]; |
||
| 265 | } |
||
| 266 | } |
||
| 267 |