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 namespace Comodojo\Cache\Drivers; |
||
23 | class FilesystemGhost extends FilesystemXattr { |
||
24 | |||
25 | const DRIVER_NAME = "filesystem-ghost"; |
||
26 | |||
27 | // protected $cache_folder; |
||
28 | |||
29 | // public function __construct(array $configuration) { |
||
|
|||
30 | // |
||
31 | // $this->cache_folder = $configuration['cache-folder']; |
||
32 | // |
||
33 | // if ( $this->test() === false ) throw new Exception("Folder ".$this->cache_folder." not writable"); |
||
34 | // |
||
35 | // } |
||
36 | |||
37 | // public function test() { |
||
38 | // |
||
39 | // return file_exists($this->cache_folder) && is_writable($this->cache_folder); |
||
40 | // |
||
41 | // } |
||
42 | |||
43 | // public function get($key, $namespace) { |
||
44 | // |
||
45 | // if ( $this->has($key, $namespace) ) { |
||
46 | // |
||
47 | // $cacheFile = $this->cache_folder."$key-$namespace.cache"; |
||
48 | // |
||
49 | // $data = @file_get_contents($cacheFile); |
||
50 | // |
||
51 | // if ( $data === false ) throw new Exception("Error reading cache content $cacheFile"); |
||
52 | // |
||
53 | // return $data; |
||
54 | // |
||
55 | // } |
||
56 | // |
||
57 | // return null; |
||
58 | // |
||
59 | // } |
||
60 | |||
61 | 28 | public function set($key, $namespace, $value, $ttl = null) { |
|
83 | |||
84 | 7 | public function delete($key, $namespace) { |
|
97 | |||
98 | 11 | View Code Duplication | public function clear($namespace = null) { |
99 | |||
100 | 11 | $result = []; |
|
101 | |||
102 | 11 | if ( $namespace === null ) { |
|
103 | |||
104 | 9 | $file_list = glob($this->cache_folder."*.{cache,expire}", GLOB_BRACE); |
|
105 | |||
106 | 9 | } else { |
|
107 | |||
108 | 2 | $file_list = glob($this->cache_folder."*-$namespace.{cache,expire}", GLOB_BRACE); |
|
109 | |||
110 | } |
||
111 | |||
112 | 11 | if ( empty($file_list) ) return true; |
|
113 | |||
114 | 5 | foreach ( $file_list as $file ) $result[] = unlink($file); |
|
115 | |||
116 | 5 | return !in_array(false, $result); |
|
117 | |||
118 | } |
||
119 | |||
120 | // public function getMultiple(array $keys, $namespace) { |
||
121 | // |
||
122 | // $result = []; |
||
123 | // |
||
124 | // foreach ($keys as $key) { |
||
125 | // $result[$key] = $this->get($key, $namespace); |
||
126 | // } |
||
127 | // |
||
128 | // return $result; |
||
129 | // |
||
130 | // } |
||
131 | // |
||
132 | // public function setMultiple(array $key_values, $namespace, $ttl = null) { |
||
133 | // |
||
134 | // $result = []; |
||
135 | // |
||
136 | // foreach ($keys as $key => $value) { |
||
137 | // $result[] = $this->set($key, $namespace, $value, $ttl); |
||
138 | // } |
||
139 | // |
||
140 | // return !in_array(false, $result); |
||
141 | // |
||
142 | // } |
||
143 | // |
||
144 | // public function deleteMultiple(array $keys, $namespace) { |
||
145 | // |
||
146 | // $result = []; |
||
147 | // |
||
148 | // foreach ($keys as $key) { |
||
149 | // $result[] = $this->delete($key, $namespace); |
||
150 | // } |
||
151 | // |
||
152 | // return !in_array(false, $result); |
||
153 | // |
||
154 | // } |
||
155 | |||
156 | 36 | public function has($key, $namespace) { |
|
181 | |||
182 | // public function stats() { |
||
183 | // |
||
184 | // return ['objects' => count(glob($this->cache_folder."*.cache"))]; |
||
185 | // |
||
186 | // } |
||
187 | |||
188 | } |
||
189 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.