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 |
||
| 18 | class Ttl_Cache { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The cache name. |
||
| 22 | * |
||
| 23 | * @var string $name The cache name. |
||
| 24 | * @access private |
||
| 25 | * @since 3.21.2 |
||
| 26 | */ |
||
| 27 | private $name; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The TTL of cached responses in seconds. |
||
| 31 | * |
||
| 32 | * @var int $ttl The TTL in seconds. |
||
| 33 | * @access private |
||
| 34 | * @since 3.21.2 |
||
| 35 | */ |
||
| 36 | private $ttl; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The cache dir where the cached data is written. |
||
| 40 | * |
||
| 41 | * @since 3.21.2 |
||
| 42 | * @access private |
||
| 43 | * @var string $cache_dir The cache dir where the cached responses are written. |
||
| 44 | */ |
||
| 45 | private $cache_dir; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * A {@link Wordlift_Log_Service} instance. |
||
| 49 | * |
||
| 50 | * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
||
| 51 | * @access private |
||
| 52 | * @since 3.21.2 |
||
| 53 | */ |
||
| 54 | private $log; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private static $caches = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Create a {@link Ttl_Cache} with the specified TTL, default 900 secs. |
||
| 63 | * |
||
| 64 | * @param string $name The cache name. |
||
| 65 | * @param int $ttl The cache TTL, default 900 secs. |
||
| 66 | * |
||
| 67 | * @since 3.21.2 |
||
| 68 | */ |
||
| 69 | public function __construct( $name, $ttl = 900 ) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get the root cache folder. |
||
| 92 | * |
||
| 93 | * This is useful to introduce a cache cleaning procedure which will scan and delete older stale cache files. |
||
| 94 | * |
||
| 95 | * @return string The root cache folder. |
||
| 96 | * @since 3.22.5 |
||
| 97 | */ |
||
| 98 | public static function get_cache_folder() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get the cached data for the specified key. |
||
| 111 | * |
||
| 112 | * @param mixed $key A serializable key. |
||
| 113 | * |
||
| 114 | * @return mixed|null |
||
| 115 | * @since 3.21.2 |
||
| 116 | */ |
||
| 117 | public function get( $key ) { |
||
| 132 | |||
| 133 | public function put( $key, $data ) { |
||
| 142 | |||
| 143 | public function flush() { |
||
| 153 | |||
| 154 | public static function flush_all() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get the full path for the given `$hash`. The file is not checked for its existence. |
||
| 165 | * |
||
| 166 | * @param string $hash A file hash. |
||
| 167 | * |
||
| 168 | * @return string The full path to the file. |
||
| 169 | * @since 3.21.2 |
||
| 170 | */ |
||
| 171 | private function get_path( $hash ) { |
||
| 175 | |||
| 176 | private function get_filename( $key ) { |
||
| 184 | |||
| 185 | } |
||
| 186 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.