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 |
||
| 58 | class Patient { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * |
||
| 62 | * @var string $typ typ pacjenta |
||
| 63 | */ |
||
| 64 | protected $typ = ""; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * id |
||
| 68 | * @Id @Column(type="integer") @GeneratedValue |
||
| 69 | */ |
||
| 70 | public $id; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * name |
||
| 74 | * @Column(type="string", length=50) |
||
| 75 | */ |
||
| 76 | public $name; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @Column(type="string", length=11) |
||
| 80 | */ |
||
| 81 | public $pesel; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @Column(type="integer") * |
||
| 85 | */ |
||
| 86 | public $numerHistorii; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * $dataKategoryzacji datetime |
||
| 90 | * @Column(type="datetime") * |
||
| 91 | */ |
||
| 92 | public $dataKategoryzacji; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @Column(type="integer") * |
||
| 96 | */ |
||
| 97 | protected $oddzialId; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @Column(columnDefinition="TINYINT(4) NOT NULL DEFAULT 0") |
||
| 101 | */ |
||
| 102 | public $kategoriaPacjenta = 0; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @ManyToOne(targetEntity="Hospitalplugin\Entities\User") |
||
| 106 | * @JoinColumn(name="userId", referencedColumnName="id", nullable=true) |
||
| 107 | * @Column(columnDefinition="INT(11) NOT NULL DEFAULT 0") |
||
| 108 | */ |
||
| 109 | public $user; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Constructor |
||
| 113 | * |
||
| 114 | * @param unknown $args |
||
| 115 | */ |
||
| 116 | function __construct($args) { |
||
|
|
|||
| 117 | if (! isset ( $args ) || empty ( $args )) { |
||
| 118 | return; |
||
| 119 | } |
||
| 120 | View Code Duplication | foreach ( $args as $key => $value ) { |
|
| 121 | if ($key == 'dataKategoryzacji') { |
||
| 122 | $value = new \DateTime ( $value . ' 12:00:00' ); |
||
| 123 | } |
||
| 124 | call_user_func ( array ( |
||
| 125 | $this, |
||
| 126 | 'set' . $key |
||
| 127 | ), $value ); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * getId |
||
| 133 | * |
||
| 134 | * @return id |
||
| 135 | */ |
||
| 136 | public function getId() { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * sets id |
||
| 142 | * |
||
| 143 | * @param int $id |
||
| 144 | * ID |
||
| 145 | * |
||
| 146 | * @return \Hospitalplugin\Entities\Patient |
||
| 147 | */ |
||
| 148 | public function setId($id) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * getName |
||
| 155 | * |
||
| 156 | * @return name |
||
| 157 | */ |
||
| 158 | public function getName() { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * setName |
||
| 164 | * |
||
| 165 | * @param string $name |
||
| 166 | * string name |
||
| 167 | * |
||
| 168 | * @return Patient |
||
| 169 | */ |
||
| 170 | public function setName($name) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * getDataKategoryzacji |
||
| 177 | * |
||
| 178 | * @return \DateTime dataKategoryzacji |
||
| 179 | */ |
||
| 180 | public function getDataKategoryzacji() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * setDataKategoryzacji |
||
| 186 | * |
||
| 187 | * @param \DateTime $dataKategoryzacji |
||
| 188 | * data kategoryzyzacji |
||
| 189 | * |
||
| 190 | * @return Patient |
||
| 191 | */ |
||
| 192 | public function setDataKategoryzacji(\DateTime $dataKategoryzacji) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * getOddzialId |
||
| 199 | * |
||
| 200 | * @return oddzialId |
||
| 201 | */ |
||
| 202 | public function getOddzialId() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * setOddzialId |
||
| 208 | * |
||
| 209 | * @param int $oddzialId |
||
| 210 | * oddzial id |
||
| 211 | * |
||
| 212 | * @return Patient |
||
| 213 | */ |
||
| 214 | public function setOddzialId($oddzialId) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * |
||
| 221 | * @param Ward $ward |
||
| 222 | */ |
||
| 223 | public function setWard($ward) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * getOddzialId |
||
| 230 | * |
||
| 231 | * @return oddzialId |
||
| 232 | */ |
||
| 233 | public function getNumerHistorii() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * setOddzialId |
||
| 239 | * |
||
| 240 | * @param int $oddzialId |
||
| 241 | * oddzial id |
||
| 242 | * |
||
| 243 | * @return Patient |
||
| 244 | */ |
||
| 245 | public function setNumerHistorii($numerHistorii) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * getPesel |
||
| 252 | * |
||
| 253 | * @return pesel |
||
| 254 | */ |
||
| 255 | public function getPesel() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * setPesel |
||
| 261 | * |
||
| 262 | * @param string $pesel |
||
| 263 | * nr pesel |
||
| 264 | * |
||
| 265 | * @return Patient |
||
| 266 | */ |
||
| 267 | public function setPesel($pesel) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * getKategoriaPacjenta |
||
| 274 | * |
||
| 275 | * @return kategoriaPacjenta |
||
| 276 | */ |
||
| 277 | public function getKategoriaPacjenta() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * setKategoriaPacjenta |
||
| 283 | * |
||
| 284 | * @param int $kategoriaPacjenta |
||
| 285 | * kategoriaPacjenta |
||
| 286 | * |
||
| 287 | * @return Patient |
||
| 288 | */ |
||
| 289 | public function setKategoriaPacjenta($kategoriaPacjenta) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * getTyp |
||
| 296 | * |
||
| 297 | * @return typ |
||
| 298 | */ |
||
| 299 | public function getTyp() { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * setTyp |
||
| 305 | * |
||
| 306 | * @param string $typ |
||
| 307 | * |
||
| 308 | * @return \Hospitalplugin\Entities\Patient |
||
| 309 | */ |
||
| 310 | public function setTyp($typ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * getUser |
||
| 317 | */ |
||
| 318 | public function getUser() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * |
||
| 324 | * @param User $user |
||
| 325 | */ |
||
| 326 | public function setUser($user) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * toString |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | public function toString() { |
||
| 337 | $txt = $this->getName (); |
||
| 338 | $txt .= $this->getPesel (); |
||
| 339 | $txt .= "id:"; |
||
| 340 | $txt .= $this->getId (); |
||
| 341 | $txt .= "oid:"; |
||
| 342 | $txt .= $this->getOddzialId (); |
||
| 343 | $txt .= "d:"; |
||
| 344 | $data = $this->getDataKategoryzacji (); |
||
| 345 | if ($data instanceof \DateTime) { |
||
| 346 | $txt .= $this->getDataKategoryzacji ()->format ( "Y-m-d" ); |
||
| 347 | } else { |
||
| 348 | $txt .= $this->getDataKategoryzacji (); |
||
| 349 | } |
||
| 350 | return $txt; |
||
| 351 | } |
||
| 352 | public function __toString() { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * getFields |
||
| 358 | * |
||
| 359 | * @return multitype:string |
||
| 360 | */ |
||
| 361 | protected static function getFields() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * toDatatablesJSONString |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | public function toDatatablesJSONString() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * String with comma separated values. |
||
| 398 | * |
||
| 399 | * @deprecated JSON used |
||
| 400 | * @param unknown $patient |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | public function toDatatablesString() { |
||
| 421 | } |
||
| 422 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.