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 |
||
| 17 | class Psr16Adapter implements CacheInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var ExtendedCacheItemPoolInterface |
||
| 21 | */ |
||
| 22 | protected $internalCacheInstance; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Psr16Adapter constructor. |
||
| 26 | * @param $driver |
||
| 27 | * @param array $config |
||
| 28 | * @throws phpFastCacheDriverCheckException |
||
| 29 | */ |
||
| 30 | public function __construct($driver, array $config = []) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param string $key |
||
| 37 | * @param null $default |
||
| 38 | * @return mixed|null |
||
| 39 | * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
||
| 40 | */ |
||
| 41 | public function get($key, $default = null) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $key |
||
| 57 | * @param mixed $value |
||
| 58 | * @param null $ttl |
||
| 59 | * @return bool |
||
| 60 | * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
||
| 61 | */ |
||
| 62 | public function set($key, $value, $ttl = null) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $key |
||
| 79 | * @return bool |
||
| 80 | * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
||
| 81 | */ |
||
| 82 | View Code Duplication | public function delete($key) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @return bool |
||
| 93 | * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
||
| 94 | */ |
||
| 95 | public function clear() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string[] $keys |
||
| 106 | * @param null $default |
||
| 107 | * @return array |
||
| 108 | * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
||
| 109 | */ |
||
| 110 | public function getMultiple($keys, $default = null) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string[] $values |
||
| 123 | * @param null|int|\DateInterval $ttl |
||
| 124 | * @return bool |
||
| 125 | * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
||
| 126 | */ |
||
| 127 | public function setMultiple($values, $ttl = null) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param string[] $keys |
||
| 143 | * @return bool |
||
| 144 | * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function deleteMultiple($keys) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $key |
||
| 157 | * @return bool |
||
| 158 | * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
||
| 159 | */ |
||
| 160 | public function has($key) |
||
| 168 | } |
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.