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:
Complex classes like BigFileCache often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BigFileCache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class BigFileCache implements CacheInterface { |
||
30 | |||
31 | /** |
||
32 | * The default time to live of elements stored in the session (in seconds). |
||
33 | * Please note that if the session is flushed, all the elements of the cache will disapear anyway. |
||
34 | * If empty, the time to live will be the time of the session. |
||
35 | * |
||
36 | * @Property |
||
37 | * @var int |
||
38 | */ |
||
39 | private $defaultTimeToLive; |
||
40 | |||
41 | /** |
||
42 | * The logger used to trace the cache activity. |
||
43 | * Supports both PSR3 compatible logger and old Mouf logger for compatibility reasons. |
||
44 | * |
||
45 | * @var LoggerInterface|LogInterface |
||
46 | */ |
||
47 | private $log; |
||
48 | |||
49 | /** |
||
50 | * The directory the files are stored in. |
||
51 | * If none is specified, they are stored in the "filecache" directory. |
||
52 | * The directory must end with a trailing "/". |
||
53 | * |
||
54 | * @Property |
||
55 | * @var string |
||
56 | */ |
||
57 | private $cacheDirectory; |
||
58 | |||
59 | /** |
||
60 | * Whether the directory is relative to the system temp directory or not. |
||
61 | * |
||
62 | * @Property |
||
63 | * @var boolean |
||
64 | */ |
||
65 | private $relativeToSystemTempDirectory = true; |
||
66 | |||
67 | private $hashDepth = 2; |
||
68 | |||
69 | /** |
||
70 | * @param int $defaultTimeToLive |
||
71 | * @param LoggerInterface|LogInterface $log |
||
72 | * @param string $cacheDirectory |
||
73 | * @param boolean $relativeToSystemTempDirectory |
||
74 | * @param int $hashDepth |
||
|
|||
75 | */ |
||
76 | function __construct($defaultTimeToLive, $log, $cacheDirectory, $relativeToSystemTempDirectory) |
||
83 | |||
84 | /** |
||
85 | * Sets the length of the subdirectory. |
||
86 | * See Class documentation for more details |
||
87 | * |
||
88 | * @param int $hashDepth |
||
89 | */ |
||
90 | public function setHashDepth($hashDepth) |
||
97 | |||
98 | |||
99 | |||
100 | |||
101 | /** |
||
102 | * Returns the cached value for the key passed in parameter. |
||
103 | * |
||
104 | * @param string $key |
||
105 | * @return mixed |
||
106 | */ |
||
107 | public function get($key) { |
||
156 | |||
157 | /** |
||
158 | * Sets the value in the cache. |
||
159 | * |
||
160 | * @param string $key The key of the value to store |
||
161 | * @param mixed $value The value to store |
||
162 | * @param float $timeToLive The time to live of the cache, in seconds. |
||
163 | */ |
||
164 | public function set($key, $value, $timeToLive = null) { |
||
202 | |||
203 | /** |
||
204 | * Removes the object whose key is $key from the cache. |
||
205 | * |
||
206 | * @param string $key The key of the object |
||
207 | */ |
||
208 | View Code Duplication | public function purge($key) { |
|
221 | |||
222 | /** |
||
223 | * Removes all the objects from the cache. |
||
224 | * |
||
225 | */ |
||
226 | public function purgeAll() { |
||
236 | |||
237 | /** |
||
238 | * @param mixed $key : if set, this function will return the cache directory WITH subfolder |
||
239 | * @return string |
||
240 | */ |
||
241 | protected function getDirectory($key = null) { |
||
261 | |||
262 | protected function getFileName($key) { |
||
279 | |||
280 | private static function rrmdir($dir) { |
||
294 | } |
||
295 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.