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 |
||
| 25 | class SentryLogger |
||
| 26 | { |
||
| 27 | use Configurable; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var SentryAdaptor |
||
| 31 | */ |
||
| 32 | public $client = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Stipulates what gets shown in the Sentry UI, should some metric not be |
||
| 36 | * available for any reason. |
||
| 37 | * |
||
| 38 | * @const string |
||
| 39 | */ |
||
| 40 | const SLW_NOOP = 'Unavailable'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * A static constructor as per {@link Zend_Log_FactoryInterface}. |
||
| 44 | * |
||
| 45 | * @param array $config An array of optional additional configuration for |
||
| 46 | * passing custom information to Sentry. See the README |
||
| 47 | * for more detail. |
||
| 48 | * @return SentryLogger |
||
| 49 | */ |
||
| 50 | public static function factory(array $config = []) : SentryLogger |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return SentryAdaptor |
||
| 80 | */ |
||
| 81 | public function getAdaptor() : SentryAdaptor |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Returns a default environment when one isn't passed to the factory() |
||
| 88 | * method. |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function defaultEnv() : string |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns a default set of additional "tags" we wish to send to Sentry. |
||
| 99 | * By default, Sentry reports on several mertrics, and we're already sending |
||
| 100 | * {@link Member} data. But there are additional data that would be useful |
||
| 101 | * for debugging via the Sentry UI. |
||
| 102 | * |
||
| 103 | * These data can augment that which is sent to Sentry at setup |
||
| 104 | * time in _config.php. See the README for more detail. |
||
| 105 | * |
||
| 106 | * N.b. Tags can be used to group messages within the Sentry UI itself, so there |
||
| 107 | * should only be "static" data being sent, not something that can drastically |
||
| 108 | * or minutely change, such as memory usage for example. |
||
| 109 | * |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | public function defaultTags() : array |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Returns a default set of extra data to show upon selecting a message for |
||
| 124 | * analysis in the Sentry UI. This can augment the data sent to Sentry at setup |
||
| 125 | * time in _config.php as well as at runtime when calling SS_Log itself. |
||
| 126 | * See the README for more detail. |
||
| 127 | * |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | public function defaultExtra() : array |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Return the version of $pkg taken from composer.lock. |
||
| 139 | * |
||
| 140 | * @param string $pkg e.g. "silverstripe/framework" |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function getPackageInfo(string $pkg) : string |
||
| 161 | |||
| 162 | /** |
||
| 163 | * What sort of request is this? (A harder question to answer than you might |
||
| 164 | * think: http://stackoverflow.com/questions/6275363/what-is-the-correct-terminology-for-a-non-ajax-request) |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getRequestType() : string |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Return peak memory usage. |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function getPeakMemory() : string |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Basic User-Agent check and return. |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getUserAgent() : string |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Basic request method check and return. |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public function getReqMethod() : string |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function getSAPI() : string |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Returns the client IP address which originated this request. |
||
| 230 | * Lifted and modified from SilverStripe 3's SS_HTTPRequest. |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function getIP() : string |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Returns a default set of additional data specific to the user's part in |
||
| 272 | * the request. |
||
| 273 | * |
||
| 274 | * @param mixed Member|null $member |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | View Code Duplication | public function defaultUser(Member $member = null) : array |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Generate a cleaned-up backtrace of the event that got us here. |
||
| 292 | * |
||
| 293 | * @param array $record |
||
| 294 | * @return array |
||
| 295 | * @todo Unused in sentry-sdk 2.0?? |
||
| 296 | */ |
||
| 297 | View Code Duplication | public static function backtrace(array $record) : array |
|
| 334 | |||
| 335 | } |
||
| 336 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: