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 |
||
| 32 | class PhpFilesystemAdapter implements PhpFilesystemAdapterInterface |
||
| 33 | { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Creates a new directroy. |
||
| 37 | * |
||
| 38 | * @param string $pathname The directory path |
||
| 39 | * @param integer $mode The mode is 0700 by default, which means the widest possible access |
||
| 40 | * |
||
| 41 | * @return boolean TRUE on success, else FALSE |
||
| 42 | * @link http://php.net/mkdir |
||
| 43 | */ |
||
| 44 | public function mkdir($pathname, $mode = 0755) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Query whether or not the passed filename exists. |
||
| 51 | * |
||
| 52 | * @param string $filename The filename to query |
||
| 53 | * |
||
| 54 | * @return boolean TRUE if the passed filename exists, else FALSE |
||
| 55 | * @link http://php.net/is_file |
||
| 56 | */ |
||
| 57 | public function isFile($filename) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Tells whether the filename is a directory. |
||
| 64 | * |
||
| 65 | * @param string $filename Path to the file |
||
| 66 | * |
||
| 67 | * @return TRUE if the filename exists and is a directory, else FALSE |
||
| 68 | * @link http://php.net/is_dir |
||
| 69 | */ |
||
| 70 | public function isDir($filename) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Creates an empty file with the passed filename. |
||
| 77 | * |
||
| 78 | * @param string $filename The name of the file to create |
||
| 79 | * |
||
| 80 | * @return boolean TRUE if the file can be created, else FALSE |
||
| 81 | */ |
||
| 82 | public function touch($filename) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Renames a file or directory. |
||
| 89 | * |
||
| 90 | * @param string $oldname The old name |
||
| 91 | * @param string $newname The new name |
||
| 92 | * |
||
| 93 | * @return boolean TRUE on success, else FALSE |
||
| 94 | * @link http://php.net/rename |
||
| 95 | */ |
||
| 96 | public function rename($oldname, $newname) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Writes the passed data to file with the passed name. |
||
| 103 | * |
||
| 104 | * @param string $filename The name of the file to write the data to |
||
| 105 | * @param string $data The data to write to the file |
||
| 106 | * |
||
| 107 | * @return number The number of bytes written to the file |
||
| 108 | * @link http://php.net/file_put_contents |
||
| 109 | */ |
||
| 110 | public function write($filename, $data) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Delete the file with the passed name. |
||
| 117 | * |
||
| 118 | * @param string $filename The name of the file to be deleted |
||
| 119 | * |
||
| 120 | * @return boolean TRUE on success, else FALSE |
||
| 121 | */ |
||
| 122 | public function delete($filename) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Copy's a file from source to destination. |
||
| 129 | * |
||
| 130 | * @param string $src The source file |
||
| 131 | * @param string $dest The destination file |
||
| 132 | * |
||
| 133 | * @return boolean TRUE on success, else FALSE |
||
| 134 | * @link http://php.net/copy |
||
| 135 | */ |
||
| 136 | public function copy($src, $dest) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * List the filenames of a directory. |
||
| 143 | * |
||
| 144 | * @param string $directory The directory to list |
||
| 145 | * @param boolean $recursive Whether to list recursively |
||
| 146 | * |
||
| 147 | * @return array A list of filenames |
||
| 148 | */ |
||
| 149 | public function listContents($directory = '', $recursive = false) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Removes the passed directory recursively. |
||
| 171 | * |
||
| 172 | * @param string $src Name of the directory to remove |
||
| 173 | * @param boolean $recursive TRUE if the directory has to be deleted recursive, else FALSE |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | * @throws \Exception Is thrown, if the directory can not be removed |
||
| 177 | */ |
||
| 178 | View Code Duplication | public function removeDir($src, $recursive = false) |
|
| 204 | } |
||
| 205 |
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.