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 |
||
| 40 | class InfectionRaport { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * id |
||
| 44 | * @Id @Column(type="integer") @GeneratedValue |
||
| 45 | */ |
||
| 46 | public $id; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * $dataRaportu datetime |
||
| 50 | * @Column(type="datetime") * |
||
| 51 | */ |
||
| 52 | public $dataRaportu; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * $dataPrzeslania datetime |
||
| 56 | * @Column(type="datetime") * |
||
| 57 | */ |
||
| 58 | public $dataPrzeslania; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @ManyToOne(targetEntity="Hospitalplugin\Entities\Ward") |
||
| 62 | * @JoinColumn(name="oddzialId", referencedColumnName="id") |
||
| 63 | */ |
||
| 64 | public $ward; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @ManyToOne(targetEntity="Hospitalplugin\Entities\User") |
||
| 68 | * @JoinColumn(name="userId", referencedColumnName="id") |
||
| 69 | */ |
||
| 70 | protected $user; |
||
| 71 | |||
| 72 | function __construct($args) { |
||
|
|
|||
| 73 | View Code Duplication | foreach ( $args as $key => $value ) { |
|
| 74 | if ($key == 'dataRaportu') { |
||
| 75 | $value = new \DateTime ( $value ); |
||
| 76 | } |
||
| 77 | call_user_func ( array ( |
||
| 78 | $this, |
||
| 79 | 'set' . $key |
||
| 80 | ), $value ); |
||
| 81 | } |
||
| 82 | $this->dataPrzeslania = new \DateTime (); |
||
| 83 | echo '<h3><div class="alert alert-primary">Dziękuję za przesłanie raportu!</div></h3>'; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * getId |
||
| 88 | * |
||
| 89 | * @return id |
||
| 90 | */ |
||
| 91 | public function getId() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * sets id |
||
| 97 | * |
||
| 98 | * @param int $id |
||
| 99 | * ID |
||
| 100 | * |
||
| 101 | * @return \Hospitalplugin\Entities\Patient |
||
| 102 | */ |
||
| 103 | public function setId($id) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * toString |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function toString() { |
||
| 114 | $txt = $this->getId (); |
||
| 115 | $data = $this->getDataRaportu (); |
||
| 116 | if ($data instanceof \DateTime) { |
||
| 117 | $txt .= $this->getDataRaportu ()->format ( "Y-m-d" ); |
||
| 118 | } else { |
||
| 119 | $txt .= $this->getDataRaportu (); |
||
| 120 | } |
||
| 121 | return $txt; |
||
| 122 | } |
||
| 123 | public function getDataRaportu() { |
||
| 130 | |||
| 131 | public function getDataPrzeslania() { |
||
| 152 | |||
| 153 | |||
| 154 | } |
||
| 155 |
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.