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