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 |
||
| 23 | class Message implements MessageInterface |
||
| 24 | { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var HeaderInterface[] |
||
| 28 | */ |
||
| 29 | protected $headers; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var MessageBodyInterface |
||
| 33 | */ |
||
| 34 | protected $body; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Is the message valid? |
||
| 38 | * |
||
| 39 | * If we don't have any From addresses, we're invalid, according |
||
| 40 | * to RFC2822. |
||
| 41 | * |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | 2 | public function isValid() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Access headers collection |
||
| 53 | * |
||
| 54 | * Lazy-loads if not already attached. |
||
| 55 | * |
||
| 56 | * @return HeaderInterface[] |
||
| 57 | */ |
||
| 58 | 26 | public function getHeaders() |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Adds an header with provided value |
||
| 68 | * |
||
| 69 | * If key already exists it will be overridden |
||
| 70 | * |
||
| 71 | * @param string $name |
||
| 72 | * @param HeaderInterface $header |
||
| 73 | * |
||
| 74 | * @return MessageInterface |
||
| 75 | */ |
||
| 76 | 12 | public function addHeader($name, HeaderInterface $header) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Set (overwrite) From addresses |
||
| 86 | * |
||
| 87 | * @param string $emailAddress |
||
| 88 | * @param string|null $name |
||
| 89 | * |
||
| 90 | * @return MessageInterface |
||
| 91 | */ |
||
| 92 | 6 | View Code Duplication | public function setFrom($emailAddress, $name = null) |
| 101 | |||
| 102 | /** |
||
| 103 | * Access the address list of the From header. |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 4 | public function getFromAddressList() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Overwrite the address list in the To recipients |
||
| 116 | * |
||
| 117 | * @param string $email |
||
| 118 | * @param string|null $name |
||
| 119 | * |
||
| 120 | * @return MessageInterface |
||
| 121 | */ |
||
| 122 | 2 | public function setTo($email, $name = null) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Access the address list of the To header. |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | 6 | public function getToAddressList() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Add one or more addresses to the To recipients |
||
| 144 | * |
||
| 145 | * Appends to the list. |
||
| 146 | * |
||
| 147 | * @param string $email |
||
| 148 | * @param null|string $name |
||
| 149 | * |
||
| 150 | * @return MessageInterface |
||
| 151 | */ |
||
| 152 | 8 | View Code Duplication | public function addTo($email, $name = null) |
| 161 | |||
| 162 | /** |
||
| 163 | * Add a "Cc" address |
||
| 164 | * |
||
| 165 | * @param string $email |
||
| 166 | * @param null|string $name |
||
| 167 | * |
||
| 168 | * @return MessageInterface |
||
| 169 | */ |
||
| 170 | 2 | View Code Duplication | public function addCc($email, $name = null) |
| 177 | |||
| 178 | /** |
||
| 179 | * Add a "Bcc" address |
||
| 180 | * |
||
| 181 | * @param string $email |
||
| 182 | * @param null|string $name |
||
| 183 | * |
||
| 184 | * @return MessageInterface |
||
| 185 | */ |
||
| 186 | 2 | View Code Duplication | public function addBcc($email, $name = null) |
| 193 | |||
| 194 | /** |
||
| 195 | * Add one or more addresses to the Reply-To recipients |
||
| 196 | * |
||
| 197 | * Appends to the list. |
||
| 198 | * |
||
| 199 | * @param string $email |
||
| 200 | * @param null|string $name |
||
| 201 | * |
||
| 202 | * @return MessageInterface |
||
| 203 | */ |
||
| 204 | 2 | View Code Duplication | public function addReplyTo($email, $name = null) |
| 211 | |||
| 212 | /** |
||
| 213 | * Set the message subject header value |
||
| 214 | * |
||
| 215 | * @param string $subject |
||
| 216 | * |
||
| 217 | * @return MessageInterface |
||
| 218 | */ |
||
| 219 | 6 | public function setSubject($subject) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Get the message subject header value |
||
| 228 | * |
||
| 229 | * @return null|string |
||
| 230 | */ |
||
| 231 | 4 | public function getSubject() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Set the message body |
||
| 239 | * |
||
| 240 | * @param MessageBodyInterface $body |
||
| 241 | * |
||
| 242 | * @return MessageInterface |
||
| 243 | */ |
||
| 244 | 8 | public function setBody(MessageBodyInterface $body) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Get the string-serialized message body text |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | 6 | public function getBodyText() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Gets the header with provided name. |
||
| 267 | * |
||
| 268 | * If no header was set with that name, the default value will be |
||
| 269 | * assign to a new header and returned. |
||
| 270 | * |
||
| 271 | * @param string $name |
||
| 272 | * @param mixed $default |
||
| 273 | * |
||
| 274 | * @return HeaderInterface |
||
| 275 | */ |
||
| 276 | 20 | protected function getHeader($name, $default = '') |
|
| 285 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..