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 |
||
| 16 | class PersistenceLogger |
||
| 17 | { |
||
| 18 | const NAME = 'PersistenceLogger'; |
||
| 19 | |||
| 20 | /** @var bool */ |
||
| 21 | protected $logCalls = true; |
||
| 22 | |||
| 23 | /** @var array */ |
||
| 24 | protected $misses = []; |
||
| 25 | |||
| 26 | /** @var array */ |
||
| 27 | protected $hits = []; |
||
| 28 | |||
| 29 | /** @var array */ |
||
| 30 | protected $unCachedHandlers = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param bool $logCalls Flag to enable logging of calls or not, should be disabled in prod |
||
| 34 | */ |
||
| 35 | public function __construct($logCalls = true) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Log cache misses and SPI calls with method name and arguments. |
||
| 42 | * |
||
| 43 | * @param string $method |
||
| 44 | * @param array $arguments |
||
| 45 | */ |
||
| 46 | View Code Duplication | public function logCall($method, array $arguments = []) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Log a Cache hit, which means further SPI calls are not needed. |
||
| 62 | * |
||
| 63 | * @param string $method |
||
| 64 | * @param array $arguments |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function logCacheHit($method, array $arguments = []) |
|
| 79 | |||
| 80 | private function getCacheCallData($method, array $arguments, array $trimmedBacktrace) |
||
| 88 | |||
| 89 | private function getSimpleCallTrace(array $backtrace): array |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Log uncached handler being loaded. |
||
| 111 | * |
||
| 112 | * @param string $handler |
||
| 113 | */ |
||
| 114 | public function logUnCachedHandler($handler) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getName() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @deprecated since 7.3, count the hits or misses instead. |
||
| 132 | * @return int |
||
| 133 | */ |
||
| 134 | public function getCount() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function isCallsLoggingEnabled() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @deprecated Since 7.3, use getCacheMisses() |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function getCalls() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | public function getCacheMisses() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | public function getCacheHits() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | public function getLoadedUnCachedHandlers() |
||
| 179 | } |
||
| 180 |