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 |
||
31 | class Redis extends Cache implements IMemcacheTTL { |
||
32 | /** |
||
33 | * @var \Redis $cache |
||
34 | */ |
||
35 | private static $cache = null; |
||
36 | |||
37 | public function __construct($prefix = '') { |
||
43 | |||
44 | /** |
||
45 | * entries in redis get namespaced to prevent collisions between ownCloud instances and users |
||
46 | */ |
||
47 | protected function getNameSpace() { |
||
50 | |||
51 | public function get($key) { |
||
59 | |||
60 | public function set($key, $value, $ttl = 0) { |
||
67 | |||
68 | public function hasKey($key) { |
||
71 | |||
72 | public function remove($key) { |
||
79 | |||
80 | public function clear($prefix = '') { |
||
89 | |||
90 | /** |
||
91 | * Set a value in the cache if it's not already stored |
||
92 | * |
||
93 | * @param string $key |
||
94 | * @param mixed $value |
||
95 | * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
||
96 | * @return bool |
||
97 | */ |
||
98 | public function add($key, $value, $ttl = 0) { |
||
105 | |||
106 | /** |
||
107 | * Increase a stored number |
||
108 | * |
||
109 | * @param string $key |
||
110 | * @param int $step |
||
111 | * @return int | bool |
||
112 | */ |
||
113 | public function inc($key, $step = 1) { |
||
116 | |||
117 | /** |
||
118 | * Decrease a stored number |
||
119 | * |
||
120 | * @param string $key |
||
121 | * @param int $step |
||
122 | * @return int | bool |
||
123 | */ |
||
124 | public function dec($key, $step = 1) { |
||
130 | |||
131 | /** |
||
132 | * Compare and set |
||
133 | * |
||
134 | * @param string $key |
||
135 | * @param mixed $old |
||
136 | * @param mixed $new |
||
137 | * @return bool |
||
138 | */ |
||
139 | public function cas($key, $old, $new) { |
||
153 | |||
154 | /** |
||
155 | * Compare and delete |
||
156 | * |
||
157 | * @param string $key |
||
158 | * @param mixed $old |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function cad($key, $old) { |
||
172 | |||
173 | public function setTTL($key, $ttl) { |
||
176 | |||
177 | static public function isAvailable() { |
||
180 | } |
||
181 | |||
182 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.