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 |
||
2 | class cache { |
||
3 | 48 | public function __construct($cache_config) { |
|
4 | 48 | debug("cache([array])","store"); |
|
5 | // init cache store |
||
6 | 48 | $inst_store = $cache_config["dbms"]."store"; |
|
7 | 48 | include_once($cache_config["code"]."stores/$inst_store.phtml"); |
|
8 | 48 | $this->cachestore=new $inst_store($cache_config["root"], $cache_config); |
|
|
|||
9 | 48 | } |
|
10 | |||
11 | 12 | public function save($filename, $objectChain, $templateChain) { |
|
12 | if (!is_array($objectChain)) { |
||
13 | return false; |
||
14 | } |
||
15 | if (!is_array($templateChain)) { |
||
16 | return false; |
||
17 | } |
||
18 | if ( !$this->cachestore->exists('/') ) { |
||
19 | $this->cachestore->save( '/', 'pobject', new object ); |
||
20 | } |
||
21 | |||
22 | $data = new object; |
||
23 | $data->filename = $filename; |
||
24 | $data->objectChain = $objectChain; |
||
25 | $data->templateChain = $templateChain; |
||
26 | |||
27 | $properties = array(); |
||
28 | $properties["objectref"] = array(); |
||
29 | $properties["template"] = array(); |
||
30 | |||
31 | foreach ($objectChain as $id => $value) { |
||
32 | $properties["objectref"][] = array("name" => "id", "value" => $id); |
||
33 | } |
||
34 | foreach ($templateChain as $id => $template) { |
||
35 | foreach ($template as $name => $value) { |
||
36 | foreach ($value as $type => $dummy) { |
||
37 | $properties["template"][] = array("name" => "name", "value" => $id . ":" . $type . ":" . $name); |
||
38 | } |
||
39 | } |
||
40 | 12 | } |
|
41 | |||
42 | $this->cachestore->save("/" . md5($filename) . "/", "pcache", $data, $properties); |
||
43 | |||
44 | // $onsave = $this->onTemplateSaved("1944", "ppage", "ppage.view.div1.html.any"); |
||
45 | } |
||
46 | |||
47 | 24 | View Code Duplication | public function onTemplateSaved($id, $type, $name) { |
48 | 24 | $query = "template.value='$id:$type:$name' order by none"; |
|
49 | 24 | $objects = $this->cachestore->find("/", $query, 0, 0); |
|
50 | |||
51 | $template = function($object) { |
||
52 | return $object->data->filename; |
||
53 | 24 | }; |
|
54 | |||
55 | 24 | $result = $this->cachestore->call($template,array(),$objects); |
|
56 | 24 | $result = array_unique($result); |
|
57 | |||
58 | 24 | foreach ($result as $filename) { |
|
59 | $this->invalidate($filename); |
||
60 | 18 | } |
|
61 | 24 | } |
|
62 | |||
63 | 48 | View Code Duplication | public function onObjectSaved($id) { |
64 | 48 | $query = "objectref.value='$id' order by none"; |
|
65 | 48 | $objects = $this->cachestore->find("/", $query, 0, 0); |
|
66 | |||
67 | 48 | $template = function($object) { |
|
68 | return $object->data->filename; |
||
69 | 48 | }; |
|
70 | |||
71 | 48 | $result = $this->cachestore->call($template,array(),$objects); |
|
72 | 48 | $result = array_unique($result); |
|
73 | |||
74 | 48 | foreach ($result as $filename) { |
|
75 | $this->invalidate($filename); |
||
76 | 36 | } |
|
77 | 48 | } |
|
78 | |||
79 | public function invalidate($filename) { |
||
80 | global $store; |
||
81 | $absFilename = $store->get_config("files")."cache/".$filename; |
||
82 | $stamp = time(); |
||
83 | |||
84 | if (file_exists($absFilename)) { |
||
85 | if (filemtime($absFilename) > $stamp + 2) { // do not touch file which will expire soon |
||
86 | touch($absFilename, $stamp + 1); // set mtime to now; this means the cache image is now invalid; |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | |||
91 | public function delete($filename) { |
||
94 | } |
||
95 | ?> |
||
96 |
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: