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 |
||
| 42 | class EntityCollection extends RootCollection implements IProperties { |
||
| 43 | const PROPERTY_NAME_READ_MARKER = '{http://owncloud.org/ns}readMarker'; |
||
| 44 | |||
| 45 | /** @var string */ |
||
| 46 | protected $id; |
||
| 47 | |||
| 48 | /** @var ILogger */ |
||
| 49 | protected $logger; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string $id |
||
| 53 | * @param string $name |
||
| 54 | * @param ICommentsManager $commentsManager |
||
| 55 | * @param IUserManager $userManager |
||
| 56 | * @param IUserSession $userSession |
||
| 57 | * @param ILogger $logger |
||
| 58 | */ |
||
| 59 | public function __construct( |
||
| 80 | |||
| 81 | /** |
||
| 82 | * returns the ID of this entity |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public function getId() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns a specific child node, referenced by its name |
||
| 92 | * |
||
| 93 | * This method must throw Sabre\DAV\Exception\NotFound if the node does not |
||
| 94 | * exist. |
||
| 95 | * |
||
| 96 | * @param string $name |
||
| 97 | * @return \Sabre\DAV\INode |
||
| 98 | * @throws NotFound |
||
| 99 | */ |
||
| 100 | View Code Duplication | function getChild($name) { |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Returns an array with all the child nodes |
||
| 117 | * |
||
| 118 | * @return \Sabre\DAV\INode[] |
||
| 119 | */ |
||
| 120 | function getChildren() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns an array of comment nodes. Result can be influenced by offset, |
||
| 126 | * limit and date time parameters. |
||
| 127 | * |
||
| 128 | * @param int $limit |
||
| 129 | * @param int $offset |
||
| 130 | * @param \DateTime|null $datetime |
||
| 131 | * @return CommentNode[] |
||
| 132 | */ |
||
| 133 | function findChildren($limit = 0, $offset = 0, \DateTime $datetime = null) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Checks if a child-node with the specified name exists |
||
| 150 | * |
||
| 151 | * @param string $name |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | function childExists($name) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Sets the read marker to the specified date for the logged in user |
||
| 165 | * |
||
| 166 | * @param \DateTime $value |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function setReadMarker($value) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @inheritdoc |
||
| 178 | */ |
||
| 179 | function propPatch(PropPatch $propPatch) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @inheritdoc |
||
| 185 | */ |
||
| 186 | function getProperties($properties) { |
||
| 194 | } |
||
| 195 | |||
| 196 |
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.