| Conditions | 14 |
| Paths | 1025 |
| Total Lines | 50 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 76 | public function save($data, $id_intervention = null) |
||
| 77 | { |
||
| 78 | $sinistre = $this->sinistre_repository->find($data['sinistre']); |
||
| 79 | |||
| 80 | if (empty($sinistre)) { |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | |||
| 84 | $intervention = empty($id_intervention) ? new Intervention($sinistre) : $this->intervention_repository->find($id_intervention); |
||
| 85 | |||
| 86 | if (!empty($data['precision'])) { |
||
| 87 | $intervention->setPrecision($data['precision']); |
||
| 88 | } |
||
| 89 | |||
| 90 | if (!empty($data['observations'])) { |
||
| 91 | $intervention->setObservations($data['observations']); |
||
| 92 | } |
||
| 93 | |||
| 94 | if (!empty($data['updated'])) { |
||
| 95 | $intervention->setUpdated($data['updated']); |
||
| 96 | } |
||
| 97 | |||
| 98 | if (!empty($data['ended'])) { |
||
| 99 | $intervention->setEnded($data['ended']); |
||
| 100 | } |
||
| 101 | |||
| 102 | if (!empty($data['sinistre'])) { |
||
| 103 | $intervention->setSinistre($sinistre); |
||
| 104 | } |
||
| 105 | |||
| 106 | if (!empty($data['coordinates']) && is_array($data['coordinates']) && count($data['coordinates']) == 2) { |
||
| 107 | $intervention->setCoordinates(new Coordinates($data['coordinates'][0], $data['coordinates'][1])); |
||
| 108 | } |
||
| 109 | |||
| 110 | if (!empty($data['address'])) { |
||
| 111 | $intervention->setAddress($data['address']); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (array_key_exists('important', $data)) { |
||
| 115 | $intervention->setImportant($data['important'] === true); |
||
| 116 | } |
||
| 117 | |||
| 118 | if (!empty($data['commune'])) { |
||
| 119 | $intervention->setCommune($this->commune_repository->find($data['commune'])); |
||
| 120 | } |
||
| 121 | |||
| 122 | $this->intervention_repository->save($intervention); |
||
| 123 | |||
| 124 | return $intervention; |
||
| 125 | } |
||
| 126 | |||
| 147 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: